Skip to content

Commit

Permalink
Merge pull request #1305 from libp2p/fix/identify-storm
Browse files Browse the repository at this point in the history
add semaphore to control push/delta concurrency
  • Loading branch information
vyzo authored Jan 21, 2022
2 parents da67311 + ba33799 commit 0b5e1c2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
8 changes: 8 additions & 0 deletions p2p/protocol/identify/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ const LibP2PVersion = "ipfs/0.1.0"

const ServiceName = "libp2p.identify"

const maxPushConcurrency = 32

// StreamReadTimeout is the read timeout on all incoming Identify family streams.
var StreamReadTimeout = 60 * time.Second

Expand Down Expand Up @@ -129,6 +131,10 @@ type idService struct {

addPeerHandlerCh chan addPeerHandlerReq
rmPeerHandlerCh chan rmPeerHandlerReq

// pushSemaphore limits the push/delta concurrency to avoid storms
// that clog the transient scope.
pushSemaphore chan struct{}
}

// NewIDService constructs a new *idService and activates it by
Expand All @@ -154,6 +160,8 @@ func NewIDService(h host.Host, opts ...Option) (*idService, error) {

addPeerHandlerCh: make(chan addPeerHandlerReq),
rmPeerHandlerCh: make(chan rmPeerHandlerReq),

pushSemaphore: make(chan struct{}, maxPushConcurrency),
}
s.ctx, s.ctxCancel = context.WithCancel(context.Background())

Expand Down
5 changes: 5 additions & 0 deletions p2p/protocol/identify/peer_loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ func (ph *peerHandler) openStream(ctx context.Context, protos []string) (network
return nil, errProtocolNotSupported
}

ph.ids.pushSemaphore <- struct{}{}
defer func() {
<-ph.ids.pushSemaphore
}()

// negotiate a stream without opening a new connection as we "should" already have a connection.
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
Expand Down

0 comments on commit 0b5e1c2

Please sign in to comment.