Skip to content

Commit

Permalink
Merge pull request #106 from Cerebellum-Network/feature/websocket-wat…
Browse files Browse the repository at this point in the history
…chdog

Websocket hanging watchdog
  • Loading branch information
khssnv committed Sep 16, 2024
2 parents 58ea562 + 718583b commit 0dcc558
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 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 All @@ -16,6 +17,9 @@ import (
"github.com/cerebellum-network/cere-ddc-sdk-go/blockchain/pallets"
)

// Stop events listening when no new events received for this time.
const EventsListeningTimeout = 60 * time.Second

type EventsListener func(events []*parser.Event, blockNumber types.BlockNumber, blockHash types.Hash) error

type Client struct {
Expand Down Expand Up @@ -183,7 +187,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 +202,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(EventsListeningTimeout):
return context.DeadlineExceeded
}

return ctx.Err()
Expand Down

0 comments on commit 0dcc558

Please sign in to comment.