Commit 113c44fe authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

listen: conn fate sharing

parent a4e49234
...@@ -27,6 +27,9 @@ type listener struct { ...@@ -27,6 +27,9 @@ type listener struct {
// Peerstore is the set of peers we know about locally // Peerstore is the set of peers we know about locally
peers peer.Peerstore peers peer.Peerstore
// Context for children Conn
ctx context.Context
// embedded ContextCloser // embedded ContextCloser
ContextCloser ContextCloser
} }
...@@ -54,13 +57,13 @@ func (l *listener) listen() { ...@@ -54,13 +57,13 @@ func (l *listener) listen() {
handle := func(maconn manet.Conn) { handle := func(maconn manet.Conn) {
defer func() { <-sem }() // release defer func() { <-sem }() // release
c, err := newSingleConn(l.Context(), l.local, nil, maconn) c, err := newSingleConn(l.ctx, l.local, nil, maconn)
if err != nil { if err != nil {
log.Error("Error accepting connection: %v", err) log.Error("Error accepting connection: %v", err)
return return
} }
sc, err := newSecureConn(l.Context(), c, l.peers) sc, err := newSecureConn(l.ctx, c, l.peers)
if err != nil { if err != nil {
log.Error("Error securing connection: %v", err) log.Error("Error securing connection: %v", err)
return return
...@@ -130,9 +133,14 @@ func Listen(ctx context.Context, addr ma.Multiaddr, local *peer.Peer, peers peer ...@@ -130,9 +133,14 @@ func Listen(ctx context.Context, addr ma.Multiaddr, local *peer.Peer, peers peer
local: local, local: local,
conns: make(chan Conn, chansize), conns: make(chan Conn, chansize),
chansize: chansize, chansize: chansize,
ctx: ctx,
} }
l.ContextCloser = NewContextCloser(ctx, l.close) // need a separate context to use for the context closer.
// This is because the parent context will be given to all connections too,
// and if we close the listener, the connections shouldn't share the fate.
ctx2, _ := context.WithCancel(ctx)
l.ContextCloser = NewContextCloser(ctx2, l.close)
go l.listen() go l.listen()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment