From c654e991ea9f1465d59a17097097bdb0dc55c7fe Mon Sep 17 00:00:00 2001 From: Hussam Date: Mon, 11 Mar 2024 16:36:04 -0500 Subject: [PATCH] Establish timeout for stale requests --- p2p/node/p2p_services.go | 16 ++++++++++++++-- p2p/requestManager/requestManager.go | 5 +++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/p2p/node/p2p_services.go b/p2p/node/p2p_services.go index ed443604bd..9aab82e3fb 100644 --- a/p2p/node/p2p_services.go +++ b/p2p/node/p2p_services.go @@ -2,6 +2,7 @@ package node import ( "errors" + "time" "github.com/ipfs/go-cid" "github.com/libp2p/go-libp2p/core/host" @@ -53,10 +54,21 @@ func (p *P2PNode) requestFromPeer(peerID peer.ID, location common.Location, data if err != nil { return nil, err } - recvdType := <-dataChan + var recvdType interface{} + select { + case recvdType = <-dataChan: + break + case <-time.After(requestManager.C_requestTimeout): + log.Global.WithFields(log.Fields{ + "peerId": peerID, + "error": err, + }).Warn("Request failed") + p.peerManager.MarkUnresponsivePeer(peerID, location) + return nil, nil + } // Remove request ID from the map of pending requests - p.requestManager.CloseRequest(id) + defer p.requestManager.CloseRequest(id) // Check the received data type & hash matches the request switch datatype.(type) { diff --git a/p2p/requestManager/requestManager.go b/p2p/requestManager/requestManager.go index 264d70e644..edf7b45d52 100644 --- a/p2p/requestManager/requestManager.go +++ b/p2p/requestManager/requestManager.go @@ -4,10 +4,15 @@ import ( "crypto/rand" "errors" "sync" + "time" "github.com/dominant-strategies/go-quai/log" ) +const ( + C_requestTimeout = 10 * time.Second +) + var ( errRequestNotFound = errors.New("request not found") )