Skip to content

Commit 7e210bc

Browse files
ucwongRoberto Bayardo
authored and
Roberto Bayardo
committed
eth/downloader, eth/filters: use defer to call Unsubscribe (ethereum#28762)
1 parent 3324a90 commit 7e210bc

File tree

2 files changed

+5
-8
lines changed

2 files changed

+5
-8
lines changed

eth/downloader/api.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,15 @@ func (api *DownloaderAPI) Syncing(ctx context.Context) (*rpc.Subscription, error
101101
go func() {
102102
statuses := make(chan interface{})
103103
sub := api.SubscribeSyncStatus(statuses)
104+
defer sub.Unsubscribe()
104105

105106
for {
106107
select {
107108
case status := <-statuses:
108109
notifier.Notify(rpcSub.ID, status)
109110
case <-rpcSub.Err():
110-
sub.Unsubscribe()
111111
return
112112
case <-notifier.Closed():
113-
sub.Unsubscribe()
114113
return
115114
}
116115
}

eth/filters/api.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ func (api *FilterAPI) NewPendingTransactions(ctx context.Context, fullTx *bool)
159159
go func() {
160160
txs := make(chan []*types.Transaction, 128)
161161
pendingTxSub := api.events.SubscribePendingTxs(txs)
162+
defer pendingTxSub.Unsubscribe()
163+
162164
chainConfig := api.sys.backend.ChainConfig()
163165

164166
for {
@@ -176,10 +178,8 @@ func (api *FilterAPI) NewPendingTransactions(ctx context.Context, fullTx *bool)
176178
}
177179
}
178180
case <-rpcSub.Err():
179-
pendingTxSub.Unsubscribe()
180181
return
181182
case <-notifier.Closed():
182-
pendingTxSub.Unsubscribe()
183183
return
184184
}
185185
}
@@ -233,16 +233,15 @@ func (api *FilterAPI) NewHeads(ctx context.Context) (*rpc.Subscription, error) {
233233
go func() {
234234
headers := make(chan *types.Header)
235235
headersSub := api.events.SubscribeNewHeads(headers)
236+
defer headersSub.Unsubscribe()
236237

237238
for {
238239
select {
239240
case h := <-headers:
240241
notifier.Notify(rpcSub.ID, h)
241242
case <-rpcSub.Err():
242-
headersSub.Unsubscribe()
243243
return
244244
case <-notifier.Closed():
245-
headersSub.Unsubscribe()
246245
return
247246
}
248247
}
@@ -267,6 +266,7 @@ func (api *FilterAPI) Logs(ctx context.Context, crit FilterCriteria) (*rpc.Subsc
267266
if err != nil {
268267
return nil, err
269268
}
269+
defer logsSub.Unsubscribe()
270270

271271
go func() {
272272
for {
@@ -277,10 +277,8 @@ func (api *FilterAPI) Logs(ctx context.Context, crit FilterCriteria) (*rpc.Subsc
277277
notifier.Notify(rpcSub.ID, &log)
278278
}
279279
case <-rpcSub.Err(): // client send an unsubscribe request
280-
logsSub.Unsubscribe()
281280
return
282281
case <-notifier.Closed(): // connection dropped
283-
logsSub.Unsubscribe()
284282
return
285283
}
286284
}

0 commit comments

Comments
 (0)