Skip to content

Commit

Permalink
fix: update rcmgr for go-libp2p v0.25
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorropo committed Feb 14, 2023
1 parent f0db906 commit 6a45235
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 83 deletions.
4 changes: 2 additions & 2 deletions config/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ type ConnMgr struct {
// <https://github.com/libp2p/go-libp2p/tree/master/p2p/host/resource-manager#readme>
type ResourceMgr struct {
// Enables the Network Resource Manager feature, default to on.
Enabled Flag `json:",omitempty"`
Limits *rcmgr.LimitConfig `json:",omitempty"`
Enabled Flag `json:",omitempty"`
Limits *rcmgr.PartialLimitConfig `json:",omitempty"`

MaxMemory *OptionalString `json:",omitempty"`
MaxFileDescriptors *OptionalInteger `json:",omitempty"`
Expand Down
6 changes: 3 additions & 3 deletions core/commands/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ var swarmPeeringCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Modify the peering subsystem.",
ShortDescription: `
'ipfs swarm peering' manages the peering subsystem.
Peers in the peering subsystem are maintained to be connected, reconnected
'ipfs swarm peering' manages the peering subsystem.
Peers in the peering subsystem are maintained to be connected, reconnected
on disconnect with a back-off.
The changes are not saved to the config.
`,
Expand Down Expand Up @@ -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.BaseLimit
var newLimit rcmgr.ResourceLimits
it := req.Files.Entries()
if it.Next() {
file := files.FileFromEntry(it)
Expand Down
43 changes: 22 additions & 21 deletions core/node/libp2p/rcmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func ResourceManager(cfg config.SwarmConfig) interface{} {
return nil, opts, fmt.Errorf("opening IPFS_PATH: %w", err)
}

var limitConfig rcmgr.LimitConfig
var limitConfig rcmgr.ConcreteLimitConfig
defaultComputedLimitConfig, err := createDefaultLimitConfig(cfg)
if err != nil {
return nil, opts, err
Expand All @@ -66,8 +66,7 @@ func ResourceManager(cfg config.SwarmConfig) interface{} {
// Because of how how Apply works, any 0 value for a user supplied override
// will be overriden with a computed default value.
// There currently isn't a way for a user to supply a 0-value override.
userSuppliedOverrideLimitConfig.Apply(defaultComputedLimitConfig)
limitConfig = userSuppliedOverrideLimitConfig
limitConfig = userSuppliedOverrideLimitConfig.Build(defaultComputedLimitConfig)
} else {
limitConfig = defaultComputedLimitConfig
}
Expand Down Expand Up @@ -421,14 +420,14 @@ func NetLimit(mgr network.ResourceManager, scope string) (rcmgr.BaseLimit, error
}

// 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.BaseLimit) 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
return ErrNoResourceMgr
}

limiter.SetLimit(&limit)
limiter.SetLimit(limit.Build(rcmgr.InfiniteLimits.System))
return nil
}

Expand All @@ -438,7 +437,7 @@ func NetSetLimit(mgr network.ResourceManager, repo repo.Repo, scope string, limi
}

if cfg.Swarm.ResourceMgr.Limits == nil {
cfg.Swarm.ResourceMgr.Limits = &rcmgr.LimitConfig{}
cfg.Swarm.ResourceMgr.Limits = &rcmgr.PartialLimitConfig{}
}
configLimits := cfg.Swarm.ResourceMgr.Limits

Expand All @@ -455,7 +454,7 @@ func NetSetLimit(mgr network.ResourceManager, repo repo.Repo, scope string, limi
err = mgr.ViewService(svc, func(s network.ServiceScope) error { return setLimit(s) })
setConfigFunc = func() {
if configLimits.Service == nil {
configLimits.Service = map[string]rcmgr.BaseLimit{}
configLimits.Service = map[string]rcmgr.ResourceLimits{}
}
configLimits.Service[svc] = limit
}
Expand All @@ -464,7 +463,7 @@ func NetSetLimit(mgr network.ResourceManager, repo repo.Repo, scope string, limi
err = mgr.ViewProtocol(protocol.ID(proto), func(s network.ProtocolScope) error { return setLimit(s) })
setConfigFunc = func() {
if configLimits.Protocol == nil {
configLimits.Protocol = map[protocol.ID]rcmgr.BaseLimit{}
configLimits.Protocol = map[protocol.ID]rcmgr.ResourceLimits{}
}
configLimits.Protocol[protocol.ID(proto)] = limit
}
Expand All @@ -478,7 +477,7 @@ func NetSetLimit(mgr network.ResourceManager, repo repo.Repo, scope string, limi
err = mgr.ViewPeer(pid, func(s network.PeerScope) error { return setLimit(s) })
setConfigFunc = func() {
if configLimits.Peer == nil {
configLimits.Peer = map[peer.ID]rcmgr.BaseLimit{}
configLimits.Peer = map[peer.ID]rcmgr.ResourceLimits{}
}
configLimits.Peer[pid] = limit
}
Expand All @@ -491,7 +490,7 @@ func NetSetLimit(mgr network.ResourceManager, repo repo.Repo, scope string, limi
}

if cfg.Swarm.ResourceMgr.Limits == nil {
cfg.Swarm.ResourceMgr.Limits = &rcmgr.LimitConfig{}
cfg.Swarm.ResourceMgr.Limits = &rcmgr.PartialLimitConfig{}
}
setConfigFunc()

Expand Down Expand Up @@ -526,8 +525,10 @@ func NetResetLimit(mgr network.ResourceManager, repo repo.Repo, scope string) (r
return result, fmt.Errorf("creating default limit config: %w", err)
}

// INVESTIGATE(@Jorropo): Why do we save scaled configs in the repo ?

if cfg.Swarm.ResourceMgr.Limits == nil {
cfg.Swarm.ResourceMgr.Limits = &rcmgr.LimitConfig{}
cfg.Swarm.ResourceMgr.Limits = &rcmgr.PartialLimitConfig{}
}
configLimits := cfg.Swarm.ResourceMgr.Limits

Expand All @@ -536,13 +537,13 @@ func NetResetLimit(mgr network.ResourceManager, repo repo.Repo, scope string) (r
case scope == config.ResourceMgrSystemScope:
err = mgr.ViewSystem(func(s network.ResourceScope) error { return setLimit(s, &defaults.System) })
setConfigFunc = func() rcmgr.BaseLimit {
configLimits.System = defaults.System
configLimits.System = defaults.System.ToResourceLimits()
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 = defaults.Transient
configLimits.Transient = defaults.Transient.ToResourceLimits()
return defaults.Transient
}
case strings.HasPrefix(scope, config.ResourceMgrServiceScopePrefix):
Expand All @@ -551,9 +552,9 @@ func NetResetLimit(mgr network.ResourceManager, repo repo.Repo, scope string) (r
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 = map[string]rcmgr.ResourceLimits{}
}
configLimits.Service[svc] = defaults.ServiceDefault
configLimits.Service[svc] = defaults.ServiceDefault.ToResourceLimits()
return defaults.ServiceDefault
}
case strings.HasPrefix(scope, config.ResourceMgrProtocolScopePrefix):
Expand All @@ -562,9 +563,9 @@ func NetResetLimit(mgr network.ResourceManager, repo repo.Repo, scope string) (r
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 = map[protocol.ID]rcmgr.ResourceLimits{}
}
configLimits.Protocol[protocol.ID(proto)] = defaults.ProtocolDefault
configLimits.Protocol[protocol.ID(proto)] = defaults.ProtocolDefault.ToResourceLimits()

return defaults.ProtocolDefault
}
Expand All @@ -577,12 +578,12 @@ func NetResetLimit(mgr network.ResourceManager, repo repo.Repo, scope string) (r
return result, fmt.Errorf("invalid peer ID: %q: %w", p, err)
}

err = mgr.ViewPeer(pid, func(s network.PeerScope) error { return setLimit(s, &defaults.PeerDefault) })
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 = map[peer.ID]rcmgr.ResourceLimits{}
}
configLimits.Peer[pid] = defaults.PeerDefault
configLimits.Peer[pid] = defaults.PeerDefault.ToResourceLimits()

return defaults.PeerDefault
}
Expand All @@ -603,7 +604,7 @@ func NetResetLimit(mgr network.ResourceManager, repo repo.Repo, scope string) (r
return result, nil
}

func ensureConnMgrMakeSenseVsResourceMgr(rcm rcmgr.LimitConfig, cmgr config.ConnMgr) error {
func ensureConnMgrMakeSenseVsResourceMgr(rcm rcmgr.ConcreteLimitConfig, cmgr config.ConnMgr) error {
if cmgr.Type.WithDefault(config.DefaultConnMgrType) == "none" {
return nil // none connmgr, no checks to do
}
Expand Down
4 changes: 2 additions & 2 deletions core/node/libp2p/rcmgr_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ var noLimitIncrease = rcmgr.BaseLimitIncrease{
// createDefaultLimitConfig creates LimitConfig to pass to libp2p's resource manager.
// The defaults follow the documentation in docs/libp2p-resource-management.md.
// Any changes in the logic here should be reflected there.
func createDefaultLimitConfig(cfg config.SwarmConfig) (rcmgr.LimitConfig, error) {
func createDefaultLimitConfig(cfg config.SwarmConfig) (rcmgr.ConcreteLimitConfig, error) {
maxMemoryDefaultString := humanize.Bytes(uint64(memory.TotalMemory()) / 2)
maxMemoryString := cfg.ResourceMgr.MaxMemory.WithDefault(maxMemoryDefaultString)
maxMemory, err := humanize.ParseBytes(maxMemoryString)
if err != nil {
return rcmgr.LimitConfig{}, err
return rcmgr.ConcreteLimitConfig{}, err
}

maxMemoryMB := maxMemory / (1024 * 1024)
Expand Down
33 changes: 16 additions & 17 deletions docs/examples/kubo-as-a-library/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ go 1.18
replace github.com/ipfs/kubo => ./../../..

require (
github.com/ipfs/go-libipfs v0.4.1-0.20230208022905-378aaf777cf9
github.com/ipfs/go-libipfs v0.5.0
github.com/ipfs/interface-go-ipfs-core v0.11.0
github.com/ipfs/kubo v0.0.0-00010101000000-000000000000
github.com/libp2p/go-libp2p v0.24.2
github.com/libp2p/go-libp2p v0.25.2-0.20230214091718-aef956be688d
github.com/multiformats/go-multiaddr v0.8.0
)

Expand Down Expand Up @@ -54,12 +54,12 @@ require (
github.com/google/pprof v0.0.0-20221203041831-ce31453925ec // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
github.com/hannahhoward/go-pubsub v0.0.0-20200423002714-8d62886cc36e // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.1 // indirect
github.com/huin/goupnp v1.0.3 // indirect
github.com/ipfs/bbloom v0.0.4 // indirect
github.com/ipfs/go-bitfield v1.0.0 // indirect
Expand Down Expand Up @@ -120,29 +120,22 @@ require (
github.com/libp2p/go-doh-resolver v0.4.0 // indirect
github.com/libp2p/go-flow-metrics v0.1.0 // indirect
github.com/libp2p/go-libp2p-asn-util v0.2.0 // indirect
github.com/libp2p/go-libp2p-kad-dht v0.20.0 // indirect
github.com/libp2p/go-libp2p-kad-dht v0.21.0 // indirect
github.com/libp2p/go-libp2p-kbucket v0.5.0 // indirect
github.com/libp2p/go-libp2p-pubsub v0.8.3 // indirect
github.com/libp2p/go-libp2p-pubsub v0.9.0 // indirect
github.com/libp2p/go-libp2p-pubsub-router v0.6.0 // indirect
github.com/libp2p/go-libp2p-record v0.2.0 // indirect
github.com/libp2p/go-libp2p-routing-helpers v0.6.1 // indirect
github.com/libp2p/go-libp2p-xor v0.1.0 // indirect
github.com/libp2p/go-mplex v0.7.0 // indirect
github.com/libp2p/go-msgio v0.2.0 // indirect
github.com/libp2p/go-msgio v0.3.0 // indirect
github.com/libp2p/go-nat v0.1.0 // indirect
github.com/libp2p/go-netroute v0.2.1 // indirect
github.com/libp2p/go-openssl v0.1.0 // indirect
github.com/libp2p/go-reuseport v0.2.0 // indirect
github.com/libp2p/go-yamux/v4 v4.0.0 // indirect
github.com/libp2p/zeroconf/v2 v2.2.0 // indirect
github.com/lucas-clemente/quic-go v0.31.1 // indirect
github.com/marten-seemann/qpack v0.3.0 // indirect
github.com/marten-seemann/qtls-go1-18 v0.1.3 // indirect
github.com/marten-seemann/qtls-go1-19 v0.1.1 // indirect
github.com/marten-seemann/tcp v0.0.0-20210406111302-dfbc87cc63fd // indirect
github.com/marten-seemann/webtransport-go v0.4.3 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-pointer v0.0.1 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/miekg/dns v1.1.50 // indirect
github.com/mikioh/tcpinfo v0.0.0-20190314235526-30a79bb1804b // indirect
Expand All @@ -157,7 +150,7 @@ require (
github.com/multiformats/go-multibase v0.1.1 // indirect
github.com/multiformats/go-multicodec v0.7.0 // indirect
github.com/multiformats/go-multihash v0.2.1 // indirect
github.com/multiformats/go-multistream v0.3.3 // indirect
github.com/multiformats/go-multistream v0.4.1 // indirect
github.com/multiformats/go-varint v0.0.7 // indirect
github.com/onsi/ginkgo/v2 v2.5.1 // indirect
github.com/opencontainers/runtime-spec v1.0.2 // indirect
Expand All @@ -170,9 +163,14 @@ require (
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/quic-go/qpack v0.4.0 // indirect
github.com/quic-go/qtls-go1-18 v0.2.0 // indirect
github.com/quic-go/qtls-go1-19 v0.2.0 // indirect
github.com/quic-go/qtls-go1-20 v0.1.0 // indirect
github.com/quic-go/quic-go v0.32.0 // indirect
github.com/quic-go/webtransport-go v0.5.1 // indirect
github.com/raulk/go-watchdog v1.3.0 // indirect
github.com/samber/lo v1.36.0 // indirect
github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
github.com/whyrusleeping/base32 v0.0.0-20170828182744-c30ac30633cc // indirect
Expand All @@ -198,10 +196,10 @@ require (
go.uber.org/multierr v1.9.0 // indirect
go.uber.org/zap v1.24.0 // indirect
go4.org v0.0.0-20200411211856-f5505b9728dd // indirect
golang.org/x/crypto v0.3.0 // indirect
golang.org/x/crypto v0.4.0 // indirect
golang.org/x/exp v0.0.0-20221205204356-47842c84f3db // indirect
golang.org/x/mod v0.7.0 // indirect
golang.org/x/net v0.3.0 // indirect
golang.org/x/net v0.4.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.4.0 // indirect
golang.org/x/text v0.5.0 // indirect
Expand All @@ -212,4 +210,5 @@ require (
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/square/go-jose.v2 v2.5.1 // indirect
lukechampine.com/blake3 v1.1.7 // indirect
nhooyr.io/websocket v1.8.7 // indirect
)
Loading

0 comments on commit 6a45235

Please sign in to comment.