Skip to content

Commit

Permalink
Stop events lis if nothing received for too long
Browse files Browse the repository at this point in the history
  • Loading branch information
khssnv committed Sep 16, 2024
1 parent 58ea562 commit d4a3c0e
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion blockchain/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package blockchain
import (
"context"
"sync"
"time"

gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4"
"github.com/centrifuge/go-substrate-rpc-client/v4/registry"
Expand Down Expand Up @@ -183,7 +184,8 @@ func (c *Client) ListenEvents(

// Invoke listeners.
g.Go(func() error {
for blockEvents := range eventsC {
select {
case blockEvents := <-eventsC:
for callback := range c.eventsListeners {
err := (*callback)(blockEvents.Events, blockEvents.Number, blockEvents.Hash)
if err != nil {
Expand All @@ -197,6 +199,11 @@ func (c *Client) ListenEvents(
return err
}
}
// Watchdog for the websocket. It silently hangs sometimes with no error nor new events. In
// all Cere blockchain runtimes we have `pallet-timestamp` which makes at least one event
// (System.ExtrinsicSuccess for the timestamp.set extrinsic) per block.
case <-time.After(60 * time.Second):
return context.DeadlineExceeded
}

return ctx.Err()
Expand Down

0 comments on commit d4a3c0e

Please sign in to comment.