From 0f5973af860d3663af37487dab531ff8bb053966 Mon Sep 17 00:00:00 2001 From: Javier Su Date: Wed, 13 Mar 2024 18:02:14 +0800 Subject: [PATCH] fix: add timeout for reads to prevent halts (#110) --- block_feed/websocket.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/block_feed/websocket.go b/block_feed/websocket.go index 93dd44a..048d652 100644 --- a/block_feed/websocket.go +++ b/block_feed/websocket.go @@ -4,6 +4,7 @@ import ( "encoding/json" "github.com/gorilla/websocket" "log" + "time" ) var _ BlockFeed = (*WSSubscription)(nil) @@ -97,6 +98,8 @@ func handleInitialHandhake(ws *websocket.Conn) error { func receiveBlockEvents(ws *websocket.Conn, c chan *BlockResult) { defer close(c) for { + // There should be a block every ~6s so 20 seconds would mean the connection is faulty + ws.SetReadDeadline(time.Now().Add(time.Second * 20)) _, message, err := ws.ReadMessage() // if read message failed,