Skip to content
This repository has been archived by the owner on Nov 29, 2024. It is now read-only.

Websocket hanging watchdog #106

Merged
merged 2 commits into from
Sep 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading