Skip to content

Commit

Permalink
refactor(config): remove Swarm.ConnMgr defaults
Browse files Browse the repository at this point in the history
This moves defaults to Kubo code, cleaning up config.
If value is in config, we assume it is an explicit choice made by user.
Makes migrations easier.
  • Loading branch information
schomatis authored and lidel committed Nov 10, 2022
1 parent fd1a8f2 commit ec4b83f
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 34 deletions.
12 changes: 4 additions & 8 deletions config/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,6 @@ func InitWithIdentity(identity Identity) (*Config, error) {
Interval: "12h",
Strategy: "all",
},
Swarm: SwarmConfig{
ConnMgr: ConnMgr{
LowWater: DefaultConnMgrLowWater,
HighWater: DefaultConnMgrHighWater,
GracePeriod: DefaultConnMgrGracePeriod.String(),
Type: "basic",
},
},
Pinning: Pinning{
RemoteServices: map[string]RemotePinningService{},
},
Expand Down Expand Up @@ -114,6 +106,10 @@ const DefaultConnMgrLowWater = 600
// grace period
const DefaultConnMgrGracePeriod = time.Second * 20

// DefaultConnMgrType is the default value for the connection managers
// type.
const DefaultConnMgrType = "basic"

func addressesConfig() Addresses {
return Addresses{
Swarm: []string{
Expand Down
9 changes: 6 additions & 3 deletions config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,12 @@ fetching may be degraded.
c.AutoNAT.ServiceMode = AutoNATServiceDisabled
c.Reprovider.Interval = "0"

c.Swarm.ConnMgr.LowWater = 20
c.Swarm.ConnMgr.HighWater = 40
c.Swarm.ConnMgr.GracePeriod = time.Minute.String()
lowWater := int64(20)
highWater := int64(40)
gracePeriod := time.Minute
c.Swarm.ConnMgr.LowWater = &OptionalInteger{value: &lowWater}
c.Swarm.ConnMgr.HighWater = &OptionalInteger{value: &highWater}
c.Swarm.ConnMgr.GracePeriod = &OptionalDuration{&gracePeriod}
return nil
},
},
Expand Down
8 changes: 4 additions & 4 deletions config/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ type Transports struct {

// ConnMgr defines configuration options for the libp2p connection manager
type ConnMgr struct {
Type string
LowWater int
HighWater int
GracePeriod string
Type *OptionalString `json:",omitempty"`
LowWater *OptionalInteger `json:",omitempty"`
HighWater *OptionalInteger `json:",omitempty"`
GracePeriod *OptionalDuration `json:",omitempty"`
}

// ResourceMgr defines configuration options for the libp2p Network Resource Manager
Expand Down
19 changes: 8 additions & 11 deletions core/node/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,19 @@ func LibP2P(bcfg *BuildCfg, cfg *config.Config) fx.Option {

connmgr := fx.Options()

if cfg.Swarm.ConnMgr.Type != "none" {
switch cfg.Swarm.ConnMgr.Type {
connMgrType := cfg.Swarm.ConnMgr.Type.WithDefault(config.DefaultConnMgrType)
if connMgrType != "none" {
switch connMgrType {
case "":
// 'default' value is the basic connection manager
break
case "basic":
var err error
grace, err = time.ParseDuration(cfg.Swarm.ConnMgr.GracePeriod)
if err != nil {
return fx.Error(fmt.Errorf("parsing Swarm.ConnMgr.GracePeriod: %s", err))
}

low = cfg.Swarm.ConnMgr.LowWater
high = cfg.Swarm.ConnMgr.HighWater
grace = cfg.Swarm.ConnMgr.GracePeriod.WithDefault(config.DefaultConnMgrGracePeriod)

low = int(cfg.Swarm.ConnMgr.LowWater.WithDefault(config.DefaultConnMgrLowWater))
high = int(cfg.Swarm.ConnMgr.HighWater.WithDefault(config.DefaultConnMgrHighWater))
default:
return fx.Error(fmt.Errorf("unrecognized ConnMgr.Type: %q", cfg.Swarm.ConnMgr.Type))
return fx.Error(fmt.Errorf("unrecognized ConnMgr.Type: %q", connMgrType))
}

connmgr = fx.Provide(libp2p.ConnectionManager(low, high, grace))
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 @@ -198,9 +198,9 @@ func createDefaultLimitConfig(cfg config.SwarmConfig) (rcmgr.LimitConfig, error)
defaultLimitConfig := scalingLimitConfig.Scale(int64(maxMemory), int(numFD))

// If a high water mark is set:
if cfg.ConnMgr.Type == "basic" {
if cfg.ConnMgr.Type.WithDefault(config.DefaultConnMgrType) == config.DefaultConnMgrType {
// set the connection limit higher than high water mark so that the ConnMgr has "space and time" to close "least useful" connections.
defaultLimitConfig.System.Conns = 2 * cfg.ConnMgr.HighWater
defaultLimitConfig.System.Conns = 2 * int(cfg.ConnMgr.HighWater.WithDefault(config.DefaultConnMgrHighWater))
log.Info("adjusted default resource manager System.Conns limits to match ConnMgr.HighWater value of %s", cfg.ConnMgr.HighWater)
}

Expand Down
10 changes: 4 additions & 6 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -1730,16 +1730,14 @@ be configured to keep. Kubo currently supports two connection managers:
* none: never close idle connections.
* basic: the default connection manager.

Default: basic

#### `Swarm.ConnMgr.Type`

Sets the type of connection manager to use, options are: `"none"` (no connection
management) and `"basic"`.

Default: "basic".

Type: `string` (when unset or `""`, the default connection manager is applied
Type: `optionalString` (when unset or `""`, the default connection manager is applied
and all `ConnMgr` fields are ignored).

#### Basic Connection Manager
Expand Down Expand Up @@ -1779,7 +1777,7 @@ trim down to.

Default: `600`

Type: `integer`
Type: `optionalInteger`

##### `Swarm.ConnMgr.HighWater`

Expand All @@ -1789,7 +1787,7 @@ towards this limit.

Default: `900`

Type: `integer`
Type: `optionalInteger`

##### `Swarm.ConnMgr.GracePeriod`

Expand All @@ -1798,7 +1796,7 @@ by the connection manager.

Default: `"20s"`

Type: `duration`
Type: `optionalDuration`

### `Swarm.ResourceMgr`

Expand Down

0 comments on commit ec4b83f

Please sign in to comment.