mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-22 02:47:48 +08:00
* update go-libp2p to v0.18.0
* initialize the resource manager
* add resource manager stats/limit commands
* load limit file when building resource manager
* log absent limit file
* write rcmgr to file when IPFS_DEBUG_RCMGR is set
* fix: mark swarm limit|stats as experimental
* feat(cfg): opt-in Swarm.ResourceMgr
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)
* docs(config): small tweaks
* fix: skip libp2p.ResourceManager if disabled
This ensures 'ipfs swarm limit|stats' work only when enabled.
* fix: use NullResourceManager when disabled
This reverts commit b19f7c9eca.
after clarification feedback from
https://github.com/ipfs/go-ipfs/pull/8680#discussion_r841680182
* style: rename IPFS_RCMGR to LIBP2P_RCMGR
preexisting libp2p toggles use LIBP2P_ prefix
* test: Swarm.ResourceMgr
* fix: location of opt-in limit.json and rcmgr.json.gz
Places these files inside of IPFS_PATH
* Update docs/config.md
* feat: expose rcmgr metrics when enabled (#8785)
* add metrics for the resource manager
* export protocol and service name in Prometheus metrics
* fix: expose rcmgr metrics only when enabled
Co-authored-by: Marcin Rataj <lidel@lidel.org>
* refactor: rcmgr_metrics.go
* refactor: rcmgr_defaults.go
This file defines implicit limit defaults used when Swarm.ResourceMgr.Enabled
We keep vendored copy to ensure go-ipfs is not impacted when go-libp2p
decides to change defaults in any of the future releases.
* refactor: adjustedDefaultLimits
Cleans up the way we initialize defaults and adds a fix for case
when connection manager runs with high limits.
It also hides `Swarm.ResourceMgr.Limits` until we have a better
understanding what syntax makes sense.
* chore: cleanup after a review
* fix: restore go-ipld-prime v0.14.2
* fix: restore go-ds-flatfs v0.5.1
Co-authored-by: Lucas Molas <schomatis@gmail.com>
Co-authored-by: Marcin Rataj <lidel@lidel.org>
103 lines
2.5 KiB
Go
103 lines
2.5 KiB
Go
package libp2p
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
"time"
|
|
|
|
version "github.com/ipfs/go-ipfs"
|
|
config "github.com/ipfs/go-ipfs/config"
|
|
|
|
logging "github.com/ipfs/go-log"
|
|
"github.com/libp2p/go-libp2p"
|
|
"github.com/libp2p/go-libp2p-core/crypto"
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
"github.com/libp2p/go-libp2p-core/peerstore"
|
|
"github.com/libp2p/go-libp2p/p2p/net/connmgr"
|
|
"go.uber.org/fx"
|
|
)
|
|
|
|
var log = logging.Logger("p2pnode")
|
|
|
|
type Libp2pOpts struct {
|
|
fx.Out
|
|
|
|
Opts []libp2p.Option `group:"libp2p"`
|
|
}
|
|
|
|
// Misc options
|
|
var UserAgent = simpleOpt(libp2p.UserAgent(version.GetUserAgentVersion()))
|
|
|
|
func ConnectionManager(low, high int, grace time.Duration) func() (opts Libp2pOpts, err error) {
|
|
return func() (opts Libp2pOpts, err error) {
|
|
cm, err := connmgr.NewConnManager(low, high, connmgr.WithGracePeriod(grace))
|
|
if err != nil {
|
|
return opts, err
|
|
}
|
|
opts.Opts = append(opts.Opts, libp2p.ConnectionManager(cm))
|
|
return
|
|
}
|
|
}
|
|
|
|
func PstoreAddSelfKeys(id peer.ID, sk crypto.PrivKey, ps peerstore.Peerstore) error {
|
|
if err := ps.AddPubKey(id, sk.GetPublic()); err != nil {
|
|
return err
|
|
}
|
|
|
|
return ps.AddPrivKey(id, sk)
|
|
}
|
|
|
|
func simpleOpt(opt libp2p.Option) func() (opts Libp2pOpts, err error) {
|
|
return func() (opts Libp2pOpts, err error) {
|
|
opts.Opts = append(opts.Opts, opt)
|
|
return
|
|
}
|
|
}
|
|
|
|
type priorityOption struct {
|
|
priority, defaultPriority config.Priority
|
|
opt libp2p.Option
|
|
}
|
|
|
|
func prioritizeOptions(opts []priorityOption) libp2p.Option {
|
|
type popt struct {
|
|
priority int64 // lower priority values mean higher priority
|
|
opt libp2p.Option
|
|
}
|
|
enabledOptions := make([]popt, 0, len(opts))
|
|
for _, o := range opts {
|
|
if prio, ok := o.priority.WithDefault(o.defaultPriority); ok {
|
|
enabledOptions = append(enabledOptions, popt{
|
|
priority: prio,
|
|
opt: o.opt,
|
|
})
|
|
}
|
|
}
|
|
sort.Slice(enabledOptions, func(i, j int) bool {
|
|
return enabledOptions[i].priority < enabledOptions[j].priority
|
|
})
|
|
p2pOpts := make([]libp2p.Option, len(enabledOptions))
|
|
for i, opt := range enabledOptions {
|
|
p2pOpts[i] = opt.opt
|
|
}
|
|
return libp2p.ChainOptions(p2pOpts...)
|
|
}
|
|
|
|
func ForceReachability(val *config.OptionalString) func() (opts Libp2pOpts, err error) {
|
|
return func() (opts Libp2pOpts, err error) {
|
|
if val.IsDefault() {
|
|
return
|
|
}
|
|
v := val.WithDefault("unrecognized")
|
|
switch v {
|
|
case "public":
|
|
opts.Opts = append(opts.Opts, libp2p.ForceReachabilityPublic())
|
|
case "private":
|
|
opts.Opts = append(opts.Opts, libp2p.ForceReachabilityPrivate())
|
|
default:
|
|
return opts, fmt.Errorf("unrecognized reachability option: %s", v)
|
|
}
|
|
return
|
|
}
|
|
}
|