From d4a3c0e823d1e06468ea631a2b8acd7062d78059 Mon Sep 17 00:00:00 2001 From: "Alisher A. Khassanov" Date: Mon, 16 Sep 2024 12:18:47 +0500 Subject: [PATCH 1/2] Stop events lis if nothing received for too long --- blockchain/client.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/blockchain/client.go b/blockchain/client.go index e90b3e5..3a9b896 100644 --- a/blockchain/client.go +++ b/blockchain/client.go @@ -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" @@ -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 { @@ -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() From 718583b551cc5d4b838ab9624e60adbb56288186 Mon Sep 17 00:00:00 2001 From: "Alisher A. Khassanov" Date: Mon, 16 Sep 2024 12:47:08 +0500 Subject: [PATCH 2/2] Events listening timeout const --- blockchain/client.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/blockchain/client.go b/blockchain/client.go index 3a9b896..5827b8b 100644 --- a/blockchain/client.go +++ b/blockchain/client.go @@ -17,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 { @@ -202,7 +205,7 @@ func (c *Client) ListenEvents( // 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): + case <-time.After(EventsListeningTimeout): return context.DeadlineExceeded }