Skip to content

Commit

Permalink
eth/downloader, eth/filters: use defer to call Unsubscribe (#28762)
Browse files Browse the repository at this point in the history
  • Loading branch information
ucwong committed Jan 5, 2024
1 parent e3eeb64 commit 877d094
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
3 changes: 1 addition & 2 deletions eth/downloader/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,15 @@ func (api *DownloaderAPI) Syncing(ctx context.Context) (*rpc.Subscription, error
go func() {
statuses := make(chan interface{})
sub := api.SubscribeSyncStatus(statuses)
defer sub.Unsubscribe()

for {
select {
case status := <-statuses:
notifier.Notify(rpcSub.ID, status)
case <-rpcSub.Err():
sub.Unsubscribe()
return
case <-notifier.Closed():
sub.Unsubscribe()
return
}
}
Expand Down
10 changes: 4 additions & 6 deletions eth/filters/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ func (api *FilterAPI) NewPendingTransactions(ctx context.Context, fullTx *bool)
go func() {
txs := make(chan []*types.Transaction, 128)
pendingTxSub := api.events.SubscribePendingTxs(txs)
defer pendingTxSub.Unsubscribe()

chainConfig := api.sys.backend.ChainConfig()

This comment has been minimized.

Copy link
@wjmelements

wjmelements Jan 16, 2024

Contributor

It looks like chainConfig is unused


for {
Expand All @@ -176,10 +178,8 @@ func (api *FilterAPI) NewPendingTransactions(ctx context.Context, fullTx *bool)
}
}
case <-rpcSub.Err():
pendingTxSub.Unsubscribe()
return
case <-notifier.Closed():
pendingTxSub.Unsubscribe()
return
}
}
Expand Down Expand Up @@ -233,16 +233,15 @@ func (api *FilterAPI) NewHeads(ctx context.Context) (*rpc.Subscription, error) {
go func() {
headers := make(chan *types.Header)
headersSub := api.events.SubscribeNewHeads(headers)
defer headersSub.Unsubscribe()

for {
select {
case h := <-headers:
notifier.Notify(rpcSub.ID, h)
case <-rpcSub.Err():
headersSub.Unsubscribe()
return
case <-notifier.Closed():
headersSub.Unsubscribe()
return
}
}
Expand All @@ -267,6 +266,7 @@ func (api *FilterAPI) Logs(ctx context.Context, crit FilterCriteria) (*rpc.Subsc
if err != nil {
return nil, err
}
defer logsSub.Unsubscribe()

go func() {
for {
Expand All @@ -277,10 +277,8 @@ func (api *FilterAPI) Logs(ctx context.Context, crit FilterCriteria) (*rpc.Subsc
notifier.Notify(rpcSub.ID, &log)
}
case <-rpcSub.Err(): // client send an unsubscribe request
logsSub.Unsubscribe()
return
case <-notifier.Closed(): // connection dropped
logsSub.Unsubscribe()
return
}
}
Expand Down

0 comments on commit 877d094

Please sign in to comment.