mirror of
https://github.com/libp2p/go-libp2p-resource-manager.git
synced 2026-08-22 19:43:28 +08:00
add options to NewResourceManager constructor
This commit is contained in:
12
rcmgr.go
12
rcmgr.go
@@ -106,7 +106,9 @@ type streamScope struct {
|
||||
var _ network.StreamScope = (*streamScope)(nil)
|
||||
var _ network.StreamManagementScope = (*streamScope)(nil)
|
||||
|
||||
func NewResourceManager(limits Limiter) network.ResourceManager {
|
||||
type Option func(*resourceManager) error
|
||||
|
||||
func NewResourceManager(limits Limiter, opts ...Option) (network.ResourceManager, error) {
|
||||
r := &resourceManager{
|
||||
limits: limits,
|
||||
svc: make(map[string]*serviceScope),
|
||||
@@ -114,6 +116,12 @@ func NewResourceManager(limits Limiter) network.ResourceManager {
|
||||
peer: make(map[peer.ID]*peerScope),
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
if err := opt(r); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
r.system = newSystemScope(limits.GetSystemLimits())
|
||||
r.system.IncRef()
|
||||
r.transient = newTransientScope(limits.GetTransientLimits(), r.system)
|
||||
@@ -124,7 +132,7 @@ func NewResourceManager(limits Limiter) network.ResourceManager {
|
||||
r.wg.Add(1)
|
||||
go r.background()
|
||||
|
||||
return r
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func (r *resourceManager) ViewSystem(f func(network.ResourceScope) error) error {
|
||||
|
||||
Reference in New Issue
Block a user