Browse Source

p2p: use stopCtx when dialing peers in Router (#5983)

This ensures we don't leak dial goroutines when shutting down the router.
pull/5990/head
Erik Grinaker 4 years ago
committed by GitHub
parent
commit
06de7459c9
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 5 deletions
  1. +6
    -5
      p2p/router.go

+ 6
- 5
p2p/router.go View File

@ -2,6 +2,7 @@ package p2p
import (
"context"
"errors"
"fmt"
"io"
"sync"
@ -294,8 +295,10 @@ func (r *Router) dialPeers() {
}
go func() {
conn, err := r.dialPeer(address)
if err != nil {
conn, err := r.dialPeer(ctx, address)
if errors.Is(err, context.Canceled) {
return
} else if err != nil {
r.logger.Error("failed to dial peer", "peer", peerID)
if err = r.peerManager.DialFailed(peerID, address); err != nil {
r.logger.Error("failed to report dial failure", "peer", peerID, "err", err)
@ -331,9 +334,7 @@ func (r *Router) dialPeers() {
}
// dialPeer attempts to connect to a peer.
func (r *Router) dialPeer(address PeerAddress) (Connection, error) {
ctx := context.Background()
func (r *Router) dialPeer(ctx context.Context, address PeerAddress) (Connection, error) {
resolveCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()


Loading…
Cancel
Save