Skip to content

Commit

Permalink
Ensure cancellation errors are returned
Browse files Browse the repository at this point in the history
  • Loading branch information
sergerad committed Jun 17, 2023
1 parent be98ca8 commit ce82550
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions blocktracker/blocktracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,17 +370,34 @@ func NewSubscriptionBlockTracker(client *jsonrpc.Client) (*SubscriptionBlockTrac

// Track implements the BlockTracker interface
// This can take a long time so should be run concurrently.
func (s *SubscriptionBlockTracker) Track(ctx context.Context, handle func(block *ethgo.Block) error) error {
// Note that the error return variable must be named so that subscription cancellation errors can be returned in defer.
func (s *SubscriptionBlockTracker) Track(ctx context.Context, handle func(block *ethgo.Block) error) (err error) {
data := make(chan []byte)
defer close(data)

cancel, err := s.client.Subscribe("newHeads", func(b []byte) {
data <- b
})
// Subscribe with a callback the depends on context
callback := func(b []byte) {
select {
case data <- b:
case <-ctx.Done():
}
}
cancel, err := s.client.Subscribe("newHeads", callback)
if err != nil {
return err
}
defer cancel()

// Ensure subscription cancellation errors are returned
defer func() {
if cerr := cancel(); cerr != nil {
// Return variable is named so we can assign it here
if err == nil {
err = cerr
return
}
err = fmt.Errorf("failed to cancel: %s, after error %w", cerr.Error(), err)
}
}()

for {
select {
Expand Down

0 comments on commit ce82550

Please sign in to comment.