mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-08 07:58:40 +00:00
p2p/discover: simplify forwarder cleanup with context.WithCancel
This commit is contained in:
parent
a5844b48e4
commit
f82b169b84
1 changed files with 4 additions and 6 deletions
|
|
@ -753,10 +753,10 @@ func (tab *Table) deleteNode(n *enode.Node) {
|
||||||
|
|
||||||
// waitForNodes blocks until the table contains at least n nodes.
|
// waitForNodes blocks until the table contains at least n nodes.
|
||||||
func (tab *Table) waitForNodes(ctx context.Context, n int) error {
|
func (tab *Table) waitForNodes(ctx context.Context, n int) error {
|
||||||
// done lets the forwarder goroutine exit when waitForNodes returns
|
// Wrap ctx so the forwarder goroutine exits when waitForNodes returns,
|
||||||
// successfully (without ctx cancellation or table close).
|
// regardless of whether the caller's ctx is canceled.
|
||||||
done := make(chan struct{})
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
defer close(done)
|
defer cancel()
|
||||||
|
|
||||||
// Set up a notification channel that gets unblocked when there was any activity on
|
// Set up a notification channel that gets unblocked when there was any activity on
|
||||||
// the table. Ultimately this reads from the table's nodeFeed, but can't use the feed
|
// the table. Ultimately this reads from the table's nodeFeed, but can't use the feed
|
||||||
|
|
@ -782,8 +782,6 @@ func (tab *Table) waitForNodes(ctx context.Context, n int) error {
|
||||||
case <-tab.closeReq:
|
case <-tab.closeReq:
|
||||||
notifyErr = errClosed
|
notifyErr = errClosed
|
||||||
return
|
return
|
||||||
case <-done:
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue