Skip to content

Commit

Permalink
feat(cfg): opt-in Swarm.ResourceMgr
Browse files Browse the repository at this point in the history
This ensures we can safely test the resource manager without impacting
default behavior.

- Resource manager is disabled by default
    - Default for Swarm.ResourceMgr.Enabled is false for now
- Swarm.ResourceMgr.Limits allows user to tweak limits per specific
  scope in a way that is persisted across restarts
- 'ipfs swarm limit system' outputs human-readable json
- 'ipfs swarm limit system new-limits.json' sets new runtime limits
  (but does not change Swarm.ResourceMgr.Limits in the config)

Conventions to make libp2p devs life easier:
- 'IPFS_RCMGR=1 ipfs daemon' overrides the config and enables resource manager
- 'limit.json' overrides implicit defaults from libp2p (if present)
  • Loading branch information
lidel committed Apr 1, 2022
1 parent bdac61f commit 859e648
Show file tree
Hide file tree
Showing 6 changed files with 505 additions and 365 deletions.
36 changes: 36 additions & 0 deletions config/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ type SwarmConfig struct {

// ConnMgr configures the connection manager.
ConnMgr ConnMgr

// ResourceMgr configures the libp2p Network Resource Manager
ResourceMgr ResourceMgr
}

type RelayClient struct {
Expand Down Expand Up @@ -129,3 +132,36 @@ type ConnMgr struct {
HighWater int
GracePeriod string
}

// ResourceMgr defines configuration options for the libp2p Network Resource Manager
// <https://github.com/libp2p/go-libp2p-resource-manager#readme>
type ResourceMgr struct {
// Enables the Network Resource Manager feature
Enabled Flag `json:",omitempty"`

// Limits is a map of Resource Scope.
Limits map[string]ResourceMgrScopeConfig `json:",omitempty"`
}

const (
ResourceMgrSystemScope = "system"
ResourceMgrTransientScope = "transient"
ResourceMgrServiceScopePrefix = "svc:"
ResourceMgrProtocolScopePrefix = "proto:"
ResourceMgrPeerScopePrefix = "peer:"
)

// libp2p Network Resource Manager config for a scope (ipfs swarm stats|limit)
type ResourceMgrScopeConfig struct {
Dynamic bool `json:",omitempty"`
// set if Dynamic is false
Memory int64 `json:",omitempty"`
// set if Dynamic is true
MemoryFraction float64 `json:",omitempty"`
MinMemory int64 `json:",omitempty"`
MaxMemory int64 `json:",omitempty"`

Streams, StreamsInbound, StreamsOutbound int
Conns, ConnsInbound, ConnsOutbound int
FD int
}
20 changes: 11 additions & 9 deletions core/commands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,18 +215,20 @@ NOTE: For security reasons, this command will omit your private key and remote s
return cmds.EmitOnce(res, &cfg)
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *map[string]interface{}) error {
buf, err := config.HumanOutput(out)
if err != nil {
return err
}
buf = append(buf, byte('\n'))
_, err = w.Write(buf)
return err
}),
cmds.Text: HumanJsonEncoder,
},
}

var HumanJsonEncoder = cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *map[string]interface{}) error {
buf, err := config.HumanOutput(out)
if err != nil {
return err
}
buf = append(buf, byte('\n'))
_, err = w.Write(buf)
return err
})

// Scrubs value and returns error if missing
func scrubValue(m map[string]interface{}, key []string) (map[string]interface{}, error) {
return scrubMapInternal(m, key, false)
Expand Down
Loading

0 comments on commit 859e648

Please sign in to comment.