listen: conn fate sharing
Juan Batiz-Benet committed
Oct 19, 2014 at 03:51 UTC
113c44fe193c71a73a52fb46902e40d7bfe6f770
1 file changed
+11
-3
net/conn/listen.go
+11
-3
@@ -27,6 +27,9 @@ type listener struct {
27
// Peerstore is the set of peers we know about locally
28
peers peer.Peerstore
29
30
+ // Context for children Conn
31
+ ctx context.Context
32
+
33
// embedded ContextCloser
34
ContextCloser
35
}
@@ -54,13 +57,13 @@ func (l *listener) listen() {
57
handle := func(maconn manet.Conn) {
58
defer func() { <-sem }() // release
59
57
- c, err := newSingleConn(l.Context(), l.local, nil, maconn)
60
+ c, err := newSingleConn(l.ctx, l.local, nil, maconn)
61
if err != nil {
62
log.Error("Error accepting connection: %v", err)
63
return
64
}
65
63
- sc, err := newSecureConn(l.Context(), c, l.peers)
66
+ sc, err := newSecureConn(l.ctx, c, l.peers)
67
if err != nil {
68
log.Error("Error securing connection: %v", err)
69
return
@@ -130,9 +133,14 @@ func Listen(ctx context.Context, addr ma.Multiaddr, local *peer.Peer, peers peer
133
local: local,
134
conns: make(chan Conn, chansize),
135
chansize: chansize,
136
+ ctx: ctx,
137
}
138
135
- l.ContextCloser = NewContextCloser(ctx, l.close)
139
+ // need a separate context to use for the context closer.
140
+ // This is because the parent context will be given to all connections too,
141
+ // and if we close the listener, the connections shouldn't share the fate.
142
+ ctx2, _ := context.WithCancel(ctx)
143
+ l.ContextCloser = NewContextCloser(ctx2, l.close)
144
145
go l.listen()
146