Skip to content

Commit

Permalink
Merge pull request #7 from libp2p/feat/sticky-limits
Browse files Browse the repository at this point in the history
make protocols and peers sticky when setting their limit
  • Loading branch information
vyzo authored Jan 19, 2022
2 parents 39e3bd3 + cd4dbe6 commit e03ee6f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
10 changes: 10 additions & 0 deletions p2p/host/resource-manager/extapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ func (s *resourceScope) SetLimit(limit Limit) {
s.rc.limit = limit
}

func (s *protocolScope) SetLimit(limit Limit) {
s.rcmgr.setStickyProtocol(s.proto)
s.resourceScope.SetLimit(limit)
}

func (s *peerScope) SetLimit(limit Limit) {
s.rcmgr.setStickyPeer(s.peer)
s.resourceScope.SetLimit(limit)
}

func (r *resourceManager) ListServices() []string {
r.mx.Lock()
defer r.mx.Unlock()
Expand Down
33 changes: 33 additions & 0 deletions p2p/host/resource-manager/rcmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ type resourceManager struct {
proto map[protocol.ID]*protocolScope
peer map[peer.ID]*peerScope

stickyProto map[protocol.ID]struct{}
stickyPeer map[peer.ID]struct{}

connId, streamId int64
}

Expand Down Expand Up @@ -200,6 +203,16 @@ func (r *resourceManager) getProtocolScope(proto protocol.ID) *protocolScope {
return s
}

func (r *resourceManager) setStickyProtocol(proto protocol.ID) {
r.mx.Lock()
defer r.mx.Unlock()

if r.stickyProto == nil {
r.stickyProto = make(map[protocol.ID]struct{})
}
r.stickyProto[proto] = struct{}{}
}

func (r *resourceManager) getPeerScope(p peer.ID) *peerScope {
r.mx.Lock()
defer r.mx.Unlock()
Expand All @@ -214,6 +227,17 @@ func (r *resourceManager) getPeerScope(p peer.ID) *peerScope {
return s
}

func (r *resourceManager) setStickyPeer(p peer.ID) {
r.mx.Lock()
defer r.mx.Unlock()

if r.stickyPeer == nil {
r.stickyPeer = make(map[peer.ID]struct{})
}

r.stickyPeer[p] = struct{}{}
}

func (r *resourceManager) nextConnId() int64 {
r.mx.Lock()
defer r.mx.Unlock()
Expand Down Expand Up @@ -285,6 +309,10 @@ func (r *resourceManager) gc() {
defer r.mx.Unlock()

for proto, s := range r.proto {
_, sticky := r.stickyProto[proto]
if sticky {
continue
}
if s.IsUnused() {
s.Done()
delete(r.proto, proto)
Expand All @@ -293,6 +321,11 @@ func (r *resourceManager) gc() {

var deadPeers []peer.ID
for p, s := range r.peer {
_, sticky := r.stickyPeer[p]
if sticky {
continue
}

if s.IsUnused() {
s.Done()
delete(r.peer, p)
Expand Down

0 comments on commit e03ee6f

Please sign in to comment.