From d9bb868aeec7e52785d0eff7f43cb1d67f642441 Mon Sep 17 00:00:00 2001 From: Antonio Navarro Perez Date: Fri, 3 Feb 2023 15:43:04 +0100 Subject: [PATCH] Update with latest commit --- core/commands/swarm.go | 2 +- core/node/libp2p/rcmgr.go | 40 +++++++++++++------------- core/node/libp2p/rcmgr_defaults.go | 8 +++--- core/node/libp2p/rcmgr_logging_test.go | 2 +- go.mod | 2 +- go.sum | 2 ++ 6 files changed, 29 insertions(+), 27 deletions(-) diff --git a/core/commands/swarm.go b/core/commands/swarm.go index 0ef9a2ac19c..6857bfae561 100644 --- a/core/commands/swarm.go +++ b/core/commands/swarm.go @@ -430,7 +430,7 @@ Changes made via command line are persisted in the Swarm.ResourceMgr.Limits fiel // set scope limit to new values (when limit.json is passed as a second arg) if req.Files != nil { - var newLimit *rcmgr.ResourceLimits + var newLimit rcmgr.ResourceLimits it := req.Files.Entries() if it.Next() { file := files.FileFromEntry(it) diff --git a/core/node/libp2p/rcmgr.go b/core/node/libp2p/rcmgr.go index bca680e1149..873922d80b5 100644 --- a/core/node/libp2p/rcmgr.go +++ b/core/node/libp2p/rcmgr.go @@ -333,13 +333,13 @@ func NetLimitAll(mgr network.ResourceManager) (*NetStatOut, error) { if err != nil { return nil, err } - result.System = s + result.System = &s case config.ResourceMgrTransientScope: s, err := NetLimit(mgr, config.ResourceMgrSystemScope) if err != nil { return nil, err } - result.Transient = s + result.Transient = &s case config.ResourceMgrServiceScopePrefix: result.Services = make(map[string]rcmgr.ResourceLimits) for _, serv := range lister.ListServices() { @@ -347,7 +347,7 @@ func NetLimitAll(mgr network.ResourceManager) (*NetStatOut, error) { if err != nil { return nil, err } - result.Services[serv] = *s + result.Services[serv] = s } case config.ResourceMgrProtocolScopePrefix: result.Protocols = make(map[string]rcmgr.ResourceLimits) @@ -357,7 +357,7 @@ func NetLimitAll(mgr network.ResourceManager) (*NetStatOut, error) { if err != nil { return nil, err } - result.Protocols[ps] = *s + result.Protocols[ps] = s } case config.ResourceMgrPeerScopePrefix: result.Peers = make(map[string]rcmgr.ResourceLimits) @@ -367,7 +367,7 @@ func NetLimitAll(mgr network.ResourceManager) (*NetStatOut, error) { if err != nil { return nil, err } - result.Peers[ps] = *s + result.Peers[ps] = s } } } @@ -375,8 +375,8 @@ func NetLimitAll(mgr network.ResourceManager) (*NetStatOut, error) { return result, nil } -func NetLimit(mgr network.ResourceManager, scope string) (*rcmgr.ResourceLimits, error) { - var result *rcmgr.ResourceLimits +func NetLimit(mgr network.ResourceManager, scope string) (rcmgr.ResourceLimits, error) { + var result rcmgr.ResourceLimits getLimit := func(s network.ResourceScope) error { limiter, ok := s.(rcmgr.ResourceScopeLimiter) if !ok { // NullResourceManager @@ -417,7 +417,7 @@ func NetLimit(mgr network.ResourceManager, scope string) (*rcmgr.ResourceLimits, } // NetSetLimit sets new ResourceManager limits for the given scope. The limits take effect immediately, and are also persisted to the repo config. -func NetSetLimit(mgr network.ResourceManager, repo repo.Repo, scope string, limit *rcmgr.ResourceLimits) error { +func NetSetLimit(mgr network.ResourceManager, repo repo.Repo, scope string, limit rcmgr.ResourceLimits) error { setLimit := func(s network.ResourceScope) error { limiter, ok := s.(rcmgr.ResourceScopeLimiter) if !ok { // NullResourceManager @@ -454,7 +454,7 @@ func NetSetLimit(mgr network.ResourceManager, repo repo.Repo, scope string, limi if configLimits.Service == nil { configLimits.Service = map[string]rcmgr.ResourceLimits{} } - configLimits.Service[svc] = *limit + configLimits.Service[svc] = limit } case strings.HasPrefix(scope, config.ResourceMgrProtocolScopePrefix): proto := strings.TrimPrefix(scope, config.ResourceMgrProtocolScopePrefix) @@ -463,7 +463,7 @@ func NetSetLimit(mgr network.ResourceManager, repo repo.Repo, scope string, limi if configLimits.Protocol == nil { configLimits.Protocol = map[protocol.ID]rcmgr.ResourceLimits{} } - configLimits.Protocol[protocol.ID(proto)] = *limit + configLimits.Protocol[protocol.ID(proto)] = limit } case strings.HasPrefix(scope, config.ResourceMgrPeerScopePrefix): p := strings.TrimPrefix(scope, config.ResourceMgrPeerScopePrefix) @@ -477,7 +477,7 @@ func NetSetLimit(mgr network.ResourceManager, repo repo.Repo, scope string, limi if configLimits.Peer == nil { configLimits.Peer = map[peer.ID]rcmgr.ResourceLimits{} } - configLimits.Peer[pid] = *limit + configLimits.Peer[pid] = limit } default: return fmt.Errorf("invalid scope %q", scope) @@ -500,10 +500,10 @@ func NetSetLimit(mgr network.ResourceManager, repo repo.Repo, scope string, limi } // 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.ResourceLimits, error) { - var result *rcmgr.ResourceLimits +func NetResetLimit(mgr network.ResourceManager, repo repo.Repo, scope string) (rcmgr.ResourceLimits, error) { + var result rcmgr.ResourceLimits - setLimit := func(s network.ResourceScope, limit *rcmgr.ResourceLimits) error { + setLimit := func(s network.ResourceScope, limit rcmgr.ResourceLimits) error { limiter, ok := s.(rcmgr.ResourceScopeLimiter) if !ok { return ErrNoResourceMgr @@ -535,10 +535,10 @@ func NetResetLimit(mgr network.ResourceManager, repo repo.Repo, scope string) (* switch { case scope == config.ResourceMgrSystemScope: err = mgr.ViewSystem(func(s network.ResourceScope) error { return setLimit(s, defaults.System) }) - configLimits.System = nil + configLimits.System = rcmgr.ResourceLimits{} case scope == config.ResourceMgrTransientScope: err = mgr.ViewTransient(func(s network.ResourceScope) error { return setLimit(s, defaults.Transient) }) - configLimits.Transient = nil + configLimits.Transient = rcmgr.ResourceLimits{} case strings.HasPrefix(scope, config.ResourceMgrServiceScopePrefix): svc := strings.TrimPrefix(scope, config.ResourceMgrServiceScopePrefix) @@ -595,7 +595,7 @@ func ensureConnMgrMakeSenseVsResourceMgr(concreteLimits rcmgr.ConcreteLimitConfi rcm := concreteLimits.ToLimitConfig() highWater := cmgr.HighWater.WithDefault(config.DefaultConnMgrHighWater) - if rcm.System.ConnsInbound <= rcm.System.Conns { + if rcm.System.ConnsInbound <= rcm.System.Conns && rcm.System.ConnsInbound != rcmgr.Unlimited { if int64(rcm.System.ConnsInbound) <= highWater { // nolint return fmt.Errorf(` @@ -603,14 +603,14 @@ Unable to initialize libp2p due to conflicting limit configuration: ResourceMgr.Limits.System.ConnsInbound (%d) must be bigger than ConnMgr.HighWater (%d) `, rcm.System.ConnsInbound, highWater) } - } else if int64(rcm.System.Conns) <= highWater { + } else if int64(rcm.System.Conns) <= highWater && rcm.System.Conns != rcmgr.Unlimited { // nolint return fmt.Errorf(` Unable to initialize libp2p due to conflicting limit configuration: ResourceMgr.Limits.System.Conns (%d) must be bigger than ConnMgr.HighWater (%d) `, rcm.System.Conns, highWater) } - if rcm.System.StreamsInbound <= rcm.System.Streams { + if rcm.System.StreamsInbound <= rcm.System.Streams && rcm.System.StreamsInbound != rcmgr.Unlimited { if int64(rcm.System.StreamsInbound) <= highWater { // nolint return fmt.Errorf(` @@ -618,7 +618,7 @@ Unable to initialize libp2p due to conflicting limit configuration: ResourceMgr.Limits.System.StreamsInbound (%d) must be bigger than ConnMgr.HighWater (%d) `, rcm.System.StreamsInbound, highWater) } - } else if int64(rcm.System.Streams) <= highWater { + } else if int64(rcm.System.Streams) <= highWater && rcm.System.Streams != rcmgr.Unlimited { // nolint return fmt.Errorf(` Unable to initialize libp2p due to conflicting limit configuration: diff --git a/core/node/libp2p/rcmgr_defaults.go b/core/node/libp2p/rcmgr_defaults.go index a64409414a8..7f53af23002 100644 --- a/core/node/libp2p/rcmgr_defaults.go +++ b/core/node/libp2p/rcmgr_defaults.go @@ -11,7 +11,7 @@ import ( "github.com/ipfs/kubo/core/node/libp2p/fd" ) -var infiniteResourceLimit = &rcmgr.ResourceLimits{ +var infiniteResourceLimit = rcmgr.ResourceLimits{ Streams: rcmgr.Unlimited, StreamsInbound: rcmgr.Unlimited, StreamsOutbound: rcmgr.Unlimited, @@ -61,7 +61,7 @@ Run 'ipfs swarm limit all' to see the resulting limits. systemConnsInbound := int(1 * maxMemoryMB) partialLimits := rcmgr.PartialLimitConfig{ - System: &rcmgr.ResourceLimits{ + System: rcmgr.ResourceLimits{ Memory: rcmgr.LimitVal64(maxMemory), FD: rcmgr.LimitVal(maxFD), @@ -78,7 +78,7 @@ Run 'ipfs swarm limit all' to see the resulting limits. // Only established connections do. // As a result, we can't rely on System.Memory to protect us from a bunch of transient connection being opened. // We limit the same values as the System scope, but only allow the Transient scope to take 25% of what is allowed for the System scope. - Transient: &rcmgr.ResourceLimits{ + Transient: rcmgr.ResourceLimits{ Memory: rcmgr.LimitVal64(maxMemory / 4), FD: rcmgr.LimitVal(maxFD / 4), @@ -115,7 +115,7 @@ Run 'ipfs swarm limit all' to see the resulting limits. // We specify this limit against unintentional DoS attacks (e.g., a peer has a bug and is sending too much traffic intentionally). // In that case we want to keep that peer's resource consumption contained. // To keep this simple, we only constrain inbound connections and streams. - PeerDefault: &rcmgr.ResourceLimits{ + PeerDefault: rcmgr.ResourceLimits{ Memory: rcmgr.Unlimited64, FD: rcmgr.Unlimited, Conns: rcmgr.Unlimited, diff --git a/core/node/libp2p/rcmgr_logging_test.go b/core/node/libp2p/rcmgr_logging_test.go index 477e1c44214..711b6101bc7 100644 --- a/core/node/libp2p/rcmgr_logging_test.go +++ b/core/node/libp2p/rcmgr_logging_test.go @@ -17,7 +17,7 @@ import ( func TestLoggingResourceManager(t *testing.T) { clock := clock.NewMock() partialLimits := rcmgr.PartialLimitConfig{ - System: &rcmgr.ResourceLimits{ + System: rcmgr.ResourceLimits{ Conns: 1, ConnsInbound: 1, ConnsOutbound: 1, diff --git a/go.mod b/go.mod index 720e6be5964..2ee39891649 100644 --- a/go.mod +++ b/go.mod @@ -67,7 +67,7 @@ require ( github.com/jbenet/go-temp-err-catcher v0.1.0 github.com/jbenet/goprocess v0.1.4 github.com/libp2p/go-doh-resolver v0.4.0 - github.com/libp2p/go-libp2p v0.24.3-0.20230120204155-c68e24ccd286 + github.com/libp2p/go-libp2p v0.24.3-0.20230131210643-75c0b1418e10 github.com/libp2p/go-libp2p-http v0.4.0 github.com/libp2p/go-libp2p-kad-dht v0.20.0 github.com/libp2p/go-libp2p-kbucket v0.5.0 diff --git a/go.sum b/go.sum index cff8311aa83..cc47eea5b83 100644 --- a/go.sum +++ b/go.sum @@ -738,6 +738,8 @@ github.com/libp2p/go-libp2p v0.8.1/go.mod h1:QRNH9pwdbEBpx5DTJYg+qxcVaDMAz3Ee/qD github.com/libp2p/go-libp2p v0.14.3/go.mod h1:d12V4PdKbpL0T1/gsUNN8DfgMuRPDX8bS2QxCZlwRH0= github.com/libp2p/go-libp2p v0.24.3-0.20230120204155-c68e24ccd286 h1:wgKtjGrXdLm1OqPAwgZzt6JpfCs383lF8Qa5brUUezo= github.com/libp2p/go-libp2p v0.24.3-0.20230120204155-c68e24ccd286/go.mod h1:ZqQXmVB7kTkKtm+LoPODTqgKE/j/nMPpRWDgw5Lcvog= +github.com/libp2p/go-libp2p v0.24.3-0.20230131210643-75c0b1418e10 h1:bfe8H5Tj/N6yNl2LfN8Lv7k7ESJ12xDh3x1LVrrStII= +github.com/libp2p/go-libp2p v0.24.3-0.20230131210643-75c0b1418e10/go.mod h1:ZqQXmVB7kTkKtm+LoPODTqgKE/j/nMPpRWDgw5Lcvog= github.com/libp2p/go-libp2p-asn-util v0.2.0 h1:rg3+Os8jbnO5DxkC7K/Utdi+DkY3q/d1/1q+8WeNAsw= github.com/libp2p/go-libp2p-asn-util v0.2.0/go.mod h1:WoaWxbHKBymSN41hWSq/lGKJEca7TNm58+gGJi2WsLI= github.com/libp2p/go-libp2p-autonat v0.1.0/go.mod h1:1tLf2yXxiE/oKGtDwPYWTSYG3PtvYlJmg7NeVtPRqH8=