Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add timeout for reads to prevent halts #111

Merged
merged 3 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
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
10 changes: 9 additions & 1 deletion block_feed/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ func (ags *AggregateSubscription) Subscribe(rpcIndex int) (chan *BlockResult, er
// if not, the local blockchain is behind, in such case we would need to sync from Rpc.
if firstBlock := <-cWS; firstBlock.Block.Header.Height != ags.lastKnownBlock+1 {
log.Printf("[block_feed/aggregate] received the first block(%d), but local blockchain is at (%d)\n", firstBlock.Block.Header.Height, ags.lastKnownBlock)
if ags.lastKnownBlock >= firstBlock.Block.Header.Height {
log.Printf("[block_feed/aggregate] local blockchain is ahead, reconnecting to another node. \n")
ags.setSyncState(false)
ags.Close()
ags.Reconnect()
return ags.aggregateBlockChannel, nil
}
go func() {
go ags.rpc.SyncFromUntil(ags.lastKnownBlock+1, firstBlock.Block.Header.Height, rpcIndex)
for {
Expand All @@ -82,7 +89,7 @@ func (ags *AggregateSubscription) Subscribe(rpcIndex int) (chan *BlockResult, er
}
}

log.Printf("[block_feed/aggregate] switching to ws...")
log.Printf("[block_feed/aggregate] switching to ws at height %d...", ags.lastKnownBlock)

// patch ws to aggregate
for {
Expand All @@ -99,6 +106,7 @@ func (ags *AggregateSubscription) Subscribe(rpcIndex int) (chan *BlockResult, er
} else {
// if block feeder got upto this point,
// it is relatively safe that mantle is synced
log.Printf("[block_feed/aggregate] received block at height %d...", r.Block.Height)
ags.setSyncState(true)
ags.aggregateBlockChannel <- r
ags.lastKnownBlock = r.Block.Height
Expand Down
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
Loading