move resource from protocol to service when a service for a stream is set

This commit is contained in:
vyzo 2022-01-06 20:12:19 +02:00
parent 7bc4ce25f2
commit a5e4e5a670
2 changed files with 15 additions and 11 deletions

View File

@ -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,
}

View File

@ -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)