@cryptotaxi247 / kubo / commits / 6a1d70973

p2p: turns out we need half-open streams

License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>

Łukasz Magiera committed May 22, 2018 at 17:50 UTC 6a1d709733aac49eb11be82ba92bbd955443c44b
2 files changed +16 -3
p2p/outbound.go
+1
@@ -44,6 +44,7 @@ func (p2p *P2P) Dial(ctx context.Context, peer peer.ID, proto string, bindAddr m
44 listener: maListener,
45 }
46
47 + p2p.Listeners.Register(listener)
48 go listener.acceptConns()
49
50 return listener, nil
p2p/stream.go
+15 -3
@@ -27,7 +27,15 @@ type Stream struct {
27 Registry *StreamRegistry
28 }
29
30 -// Reset closes stream endpoints and deregisters it
30 +// Close closes stream endpoints and deregisters it
31 +func (s *Stream) Close() error {
32 + s.Local.Close()
33 + s.Remote.Close()
34 + s.Registry.Deregister(s.Id)
35 + return nil
36 +}
37 +
38 +// Rest closes stream endpoints and deregisters it
39 func (s *Stream) Reset() error {
40 s.Local.Close()
41 s.Remote.Reset()
@@ -42,8 +50,12 @@ func (s *Stream) startStreaming() {
50 }()
51
52 go func() {
45 - io.Copy(s.Remote, s.Local)
46 - s.Reset()
53 + _, err := io.Copy(s.Remote, s.Local)
54 + if err != nil {
55 + s.Reset()
56 + } else {
57 + s.Close()
58 + }
59 }()
60 }
61