From 62ac3ae974930a57366edd403470cf7871e67cc0 Mon Sep 17 00:00:00 2001 From: javiersuweijie Date: Wed, 13 Mar 2024 16:40:41 +0800 Subject: [PATCH] fix: add timeout for reads to prevent halts --- 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,