Skip to content

Commit

Permalink
Return correct defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
ajnavarro committed Oct 6, 2022
1 parent 54839ce commit b7c3059
Showing 1 changed file with 15 additions and 23 deletions.
38 changes: 15 additions & 23 deletions core/node/libp2p/rcmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ func NetSetLimit(mgr network.ResourceManager, repo repo.Repo, scope string, limi
return nil
}

// NetSetLimit sets new ResourceManager limits for the given scope. The limits take effect immediately, and are also persisted to the repo config.
// NetResetLimit resets ResourceManager limits to defaults. The limits take effect immediately, and are also persisted to the repo config.
func NetResetLimit(mgr network.ResourceManager, repo repo.Repo, scope string) (rcmgr.BaseLimit, error) {
var result rcmgr.BaseLimit

Expand All @@ -373,21 +373,19 @@ func NetResetLimit(mgr network.ResourceManager, repo repo.Repo, scope string) (r
cfg.Swarm.ResourceMgr.Limits = &rcmgr.LimitConfig{}
}
configLimits := cfg.Swarm.ResourceMgr.Limits
var empty rcmgr.BaseLimit

var setConfigFunc func() rcmgr.BaseLimit
switch {
case scope == config.ResourceMgrSystemScope:
err = mgr.ViewSystem(func(s network.ResourceScope) error { return setLimit(s, &defaults.System) })
setConfigFunc = func() rcmgr.BaseLimit {
configLimits.System = empty

configLimits.System = defaults.System
return defaults.System
}
case scope == config.ResourceMgrTransientScope:
err = mgr.ViewTransient(func(s network.ResourceScope) error { return setLimit(s, &defaults.Transient) })
setConfigFunc = func() rcmgr.BaseLimit {
configLimits.Transient = empty
configLimits.Transient = defaults.Transient
return defaults.Transient
}
case strings.HasPrefix(scope, config.ResourceMgrServiceScopePrefix):
Expand All @@ -397,21 +395,19 @@ func NetResetLimit(mgr network.ResourceManager, repo repo.Repo, scope string) (r
// reset defaults
err = mgr.ViewSystem(func(s network.ResourceScope) error { return setLimit(s, &defaults.ServiceDefault) })
setConfigFunc = func() rcmgr.BaseLimit {
configLimits.ServiceDefault = empty
configLimits.ServiceDefault = defaults.ServiceDefault
return defaults.ServiceDefault
}
break
}

ds := defaults.Service[svc]

err = mgr.ViewService(svc, func(s network.ServiceScope) error { return setLimit(s, &ds) })
err = mgr.ViewService(svc, func(s network.ServiceScope) error { return setLimit(s, &defaults.ServiceDefault) })
setConfigFunc = func() rcmgr.BaseLimit {
if configLimits.Service == nil {
configLimits.Service = map[string]rcmgr.BaseLimit{}
}
configLimits.Service[svc] = empty
return ds
configLimits.Service[svc] = defaults.ServiceDefault
return defaults.ServiceDefault
}
case strings.HasPrefix(scope, config.ResourceMgrProtocolScopePrefix):
proto := strings.TrimPrefix(scope, config.ResourceMgrProtocolScopePrefix)
Expand All @@ -420,23 +416,21 @@ func NetResetLimit(mgr network.ResourceManager, repo repo.Repo, scope string) (r
// reset defaults
err = mgr.ViewSystem(func(s network.ResourceScope) error { return setLimit(s, &defaults.ProtocolDefault) })
setConfigFunc = func() rcmgr.BaseLimit {
configLimits.ProtocolDefault = empty
configLimits.ProtocolDefault = defaults.ProtocolDefault

return defaults.ProtocolDefault
}
break
}

dp := defaults.Protocol[protocol.ID(proto)]

err = mgr.ViewProtocol(protocol.ID(proto), func(s network.ProtocolScope) error { return setLimit(s, &dp) })
err = mgr.ViewProtocol(protocol.ID(proto), func(s network.ProtocolScope) error { return setLimit(s, &defaults.ProtocolDefault) })
setConfigFunc = func() rcmgr.BaseLimit {
if configLimits.Protocol == nil {
configLimits.Protocol = map[protocol.ID]rcmgr.BaseLimit{}
}
configLimits.Protocol[protocol.ID(proto)] = empty
configLimits.Protocol[protocol.ID(proto)] = defaults.ProtocolDefault

return dp
return defaults.ProtocolDefault
}
case strings.HasPrefix(scope, config.ResourceMgrPeerScopePrefix):
p := strings.TrimPrefix(scope, config.ResourceMgrPeerScopePrefix)
Expand All @@ -445,7 +439,7 @@ func NetResetLimit(mgr network.ResourceManager, repo repo.Repo, scope string) (r
// reset defaults
err = mgr.ViewSystem(func(s network.ResourceScope) error { return setLimit(s, &defaults.PeerDefault) })
setConfigFunc = func() rcmgr.BaseLimit {
configLimits.PeerDefault = empty
configLimits.PeerDefault = defaults.PeerDefault

return defaults.PeerDefault
}
Expand All @@ -458,16 +452,14 @@ func NetResetLimit(mgr network.ResourceManager, repo repo.Repo, scope string) (r
return result, fmt.Errorf("invalid peer ID: %q: %w", p, err)
}

dp := defaults.Peer[pid]

err = mgr.ViewPeer(pid, func(s network.PeerScope) error { return setLimit(s, &dp) })
err = mgr.ViewPeer(pid, func(s network.PeerScope) error { return setLimit(s, &defaults.PeerDefault) })
setConfigFunc = func() rcmgr.BaseLimit {
if configLimits.Peer == nil {
configLimits.Peer = map[peer.ID]rcmgr.BaseLimit{}
}
configLimits.Peer[pid] = empty
configLimits.Peer[pid] = defaults.PeerDefault

return dp
return defaults.PeerDefault
}
default:
return result, fmt.Errorf("invalid scope %q", scope)
Expand Down

0 comments on commit b7c3059

Please sign in to comment.