mirror of
https://github.com/libp2p/go-libp2p-resource-manager.git
synced 2025-01-28 05:10:26 +08:00
move resource from protocol to service when a service for a stream is set
This commit is contained in:
parent
7bc4ce25f2
commit
a5e4e5a670
14
rcmgr.go
14
rcmgr.go
@ -406,26 +406,30 @@ func (s *streamScope) SetService(svc string) error {
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
|
||||
if s.proto == nil {
|
||||
return fmt.Errorf("stream scope not attached to a protocol")
|
||||
}
|
||||
if s.svc != nil {
|
||||
return fmt.Errorf("stream scope already attached to a service")
|
||||
}
|
||||
if s.proto == nil {
|
||||
return fmt.Errorf("stream scope not attached to a protocol")
|
||||
}
|
||||
|
||||
s.svc = s.rcmgr.getServiceScope(svc)
|
||||
|
||||
// reserve resources in service
|
||||
if err := s.svc.ReserveForChild(s.resourceScope.rc.stat()); err != nil {
|
||||
stat := s.resourceScope.rc.stat()
|
||||
if err := s.svc.ReserveForChild(stat); err != nil {
|
||||
s.svc.DecRef()
|
||||
s.svc = nil
|
||||
return err
|
||||
}
|
||||
|
||||
// remove resources from the protocol
|
||||
s.proto.ReleaseForChild(stat)
|
||||
s.proto.DecRef() // removed from constraints
|
||||
|
||||
// update constraints
|
||||
constraints := []*resourceScope{
|
||||
s.peer.resourceScope,
|
||||
s.proto.resourceScope,
|
||||
s.svc.resourceScope,
|
||||
s.rcmgr.system.resourceScope,
|
||||
}
|
||||
|
@ -522,8 +522,8 @@ func TestResourceManager(t *testing.T) {
|
||||
checkResources(t, &s.rc, network.ScopeStat{NumStreamsInbound: 1})
|
||||
})
|
||||
checkProtocol(protoA, func(s *resourceScope) {
|
||||
checkRefCnt(s, 3)
|
||||
checkResources(t, &s.rc, network.ScopeStat{NumStreamsInbound: 2})
|
||||
checkRefCnt(s, 2)
|
||||
checkResources(t, &s.rc, network.ScopeStat{NumStreamsInbound: 1})
|
||||
})
|
||||
checkProtocol(protoB, func(s *resourceScope) {
|
||||
checkRefCnt(s, 2)
|
||||
@ -555,8 +555,8 @@ func TestResourceManager(t *testing.T) {
|
||||
checkResources(t, &s.rc, network.ScopeStat{NumStreamsInbound: 2})
|
||||
})
|
||||
checkProtocol(protoA, func(s *resourceScope) {
|
||||
checkRefCnt(s, 3)
|
||||
checkResources(t, &s.rc, network.ScopeStat{NumStreamsInbound: 2})
|
||||
checkRefCnt(s, 1)
|
||||
checkResources(t, &s.rc, network.ScopeStat{})
|
||||
})
|
||||
checkProtocol(protoB, func(s *resourceScope) {
|
||||
checkRefCnt(s, 2)
|
||||
@ -588,8 +588,8 @@ func TestResourceManager(t *testing.T) {
|
||||
checkResources(t, &s.rc, network.ScopeStat{NumStreamsInbound: 2})
|
||||
})
|
||||
checkProtocol(protoA, func(s *resourceScope) {
|
||||
checkRefCnt(s, 3)
|
||||
checkResources(t, &s.rc, network.ScopeStat{NumStreamsInbound: 2})
|
||||
checkRefCnt(s, 1)
|
||||
checkResources(t, &s.rc, network.ScopeStat{})
|
||||
})
|
||||
checkProtocol(protoB, func(s *resourceScope) {
|
||||
checkRefCnt(s, 2)
|
||||
|
Loading…
Reference in New Issue
Block a user