Skip to content

Commit

Permalink
fix: add timeout for reads to prevent halts (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
javiersuweijie authored Mar 13, 2024
1 parent 8dbb7be commit 0f5973a
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions block_feed/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"github.com/gorilla/websocket"
"log"
"time"
)

var _ BlockFeed = (*WSSubscription)(nil)
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 0f5973a

Please sign in to comment.