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

Use stream manager for rate limiting requests #1751

Merged
merged 2 commits into from
May 20, 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
4 changes: 3 additions & 1 deletion p2p/node/p2p_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (p *P2PNode) requestFromPeer(peerID peer.ID, location common.Location, data
}

// Send the request to the peer
err = common.WriteMessageToStream(stream, requestBytes)
err = p.GetPeerManager().WriteMessageToStream(peerID, stream, requestBytes)
if err != nil {
return nil, err
}
Expand All @@ -69,12 +69,14 @@ func (p *P2PNode) requestFromPeer(peerID peer.ID, location common.Location, data
var recvdType interface{}
select {
case recvdType = <-dataChan:
p.GetPeerManager().ClosePendingRequest(peerID)
break
case <-time.After(requestManager.C_requestTimeout):
log.Global.WithFields(log.Fields{
"peerId": peerID,
}).Warn("Peer did not respond in time")
p.peerManager.MarkUnresponsivePeer(peerID, location)
p.GetPeerManager().ClosePendingRequest(peerID)
return nil, errors.New("peer did not respond in time")
}

Expand Down
8 changes: 8 additions & 0 deletions p2p/node/peerManager/peerManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,3 +604,11 @@ func (pm *BasicPeerManager) GetStream(peerID p2p.PeerID) (network.Stream, error)
func (pm *BasicPeerManager) CloseStream(peerID p2p.PeerID) error {
return pm.streamManager.CloseStream(peerID)
}

func (pm *BasicPeerManager) WriteMessageToStream(peerId p2p.PeerID, stream network.Stream, msg []byte) error {
return pm.streamManager.WriteMessageToStream(peerId, stream, msg)
}

func (pm *BasicPeerManager) ClosePendingRequest(peer p2p.PeerID) error {
return pm.streamManager.ClosePendingRequest(peer)
}
16 changes: 16 additions & 0 deletions p2p/node/streamManager/streamManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ type StreamManager interface {

// CloseStream goes through all the steps to properly close and remove a stream's resources
CloseStream(peer.ID) error

// WriteMessageToStream writes the given message into the given stream
WriteMessageToStream(peerID p2p.PeerID, stream network.Stream, msg []byte) error

// Releases a semaphore slot for the given peerID
ClosePendingRequest(peerID p2p.PeerID) error
}

type basicStreamManager struct {
Expand Down Expand Up @@ -184,5 +190,15 @@ func (sm *basicStreamManager) WriteMessageToStream(peerID p2p.PeerID, stream net
if err != nil {
return errors.Wrap(err, "failed to write message to stream")
}

return nil
}

func (sm *basicStreamManager) ClosePendingRequest(peerID p2p.PeerID) error {
wrappedStream, found := sm.streamCache.Get(peerID)
if !found {
return errors.New("stream not found")
}
<-wrappedStream.(*streamWrapper).semaphore
return nil
}
Loading