Skip to content
This repository has been archived by the owner on Mar 8, 2024. It is now read-only.

Commit

Permalink
Establish timeout for stale requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Djadih committed Mar 11, 2024
1 parent 3506c49 commit c654e99
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
16 changes: 14 additions & 2 deletions p2p/node/p2p_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package node

import (
"errors"
"time"

"github.com/ipfs/go-cid"
"github.com/libp2p/go-libp2p/core/host"
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions p2p/requestManager/requestManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
Expand Down

0 comments on commit c654e99

Please sign in to comment.