diff --git a/config/internal.go b/config/internal.go index 318bb3392324..dcd834e701c3 100644 --- a/config/internal.go +++ b/config/internal.go @@ -1,8 +1,10 @@ package config type Internal struct { - Bitswap *InternalBitswap `json:",omitempty"` // This is omitempty since we are expecting to make changes to all subcomponents of Internal + // All marked as omitempty since we are expecting to make changes to all subcomponents of Internal + Bitswap *InternalBitswap `json:",omitempty"` UnixFSShardingSizeThreshold *OptionalString `json:",omitempty"` + Libp2pForceReachability *OptionalString `json:",omitempty"` } type InternalBitswap struct { diff --git a/config/swarm.go b/config/swarm.go index d662bb30d44e..95d4a3d89126 100644 --- a/config/swarm.go +++ b/config/swarm.go @@ -16,21 +16,18 @@ type SwarmConfig struct { // DisableRelay explicitly disables the relay transport. // // Deprecated: This flag is deprecated and is overridden by - // `Transports.Relay` if specified. + // `Swarm.Transports.Relay` if specified. DisableRelay bool `json:",omitempty"` - // EnableRelayHop makes this node act as a public relay, relaying - // traffic between other nodes. - EnableRelayHop bool - - // EnableAutoRelay enables the "auto relay" feature. - // - // When both EnableAutoRelay and EnableRelayHop are set, this go-ipfs node - // will advertise itself as a public relay. Otherwise it will find and use - // advertised public relays when it determines that it's not reachable - // from the public internet. + // EnableAutoRelay enables the "auto relay user" feature. + // Node will find and use advertised public relays when it determines that + // it's not reachable from the public internet. EnableAutoRelay bool + // RelayService.* controls the "auto relay service" feature. + // When enabled, node will provide a limited relay service to other peers. + RelayService RelayService + // Transports contains flags to enable/disable libp2p transports. Transports Transports @@ -38,6 +35,35 @@ type SwarmConfig struct { ConnMgr ConnMgr } +// RelayService configures the resources of the circuit v2 relay. +// For every field a reasonable default will be defined in go-ipfs. +type RelayService struct { + // Enables the limited relay (circuit v2 relay). + Enabled Flag `json:",omitempty"` + + // ConnectionDurationLimit is the time limit before resetting a relayed connection. + ConnectionDurationLimit *OptionalDuration `json:",omitempty"` + // ConnectionDataLimit is the limit of data relayed (on each direction) before resetting the connection. + ConnectionDataLimit *OptionalInteger `json:",omitempty"` + + // ReservationTTL is the duration of a new (or refreshed reservation). + ReservationTTL *OptionalDuration `json:",omitempty"` + + // MaxReservations is the maximum number of active relay slots. + MaxReservations *OptionalInteger `json:",omitempty"` + // MaxCircuits is the maximum number of open relay connections for each peer; defaults to 16. + MaxCircuits *OptionalInteger `json:",omitempty"` + // BufferSize is the size of the relayed connection buffers. + BufferSize *OptionalInteger `json:",omitempty"` + + // MaxReservationsPerPeer is the maximum number of reservations originating from the same peer. + MaxReservationsPerPeer *OptionalInteger `json:",omitempty"` + // MaxReservationsPerIP is the maximum number of reservations originating from the same IP address. + MaxReservationsPerIP *OptionalInteger `json:",omitempty"` + // MaxReservationsPerASN is the maximum number of reservations origination from the same ASN. + MaxReservationsPerASN *OptionalInteger `json:",omitempty"` +} + type Transports struct { // Network specifies the base transports we'll use for dialing. To // listen on a transport, add the transport to your Addresses.Swarm. diff --git a/config/types.go b/config/types.go index fccae22b5ae0..c33689c5b2ac 100644 --- a/config/types.go +++ b/config/types.go @@ -270,16 +270,16 @@ type OptionalInteger struct { } // WithDefault resolves the integer with the given default. -func (p OptionalInteger) WithDefault(defaultValue int64) (value int64) { - if p.value == nil { +func (p *OptionalInteger) WithDefault(defaultValue int64) (value int64) { + if p == nil || p.value == nil { return defaultValue } return *p.value } // IsDefault returns if this is a default optional integer -func (p OptionalInteger) IsDefault() bool { - return p.value == nil +func (p *OptionalInteger) IsDefault() bool { + return p == nil || p.value == nil } func (p OptionalInteger) MarshalJSON() ([]byte, error) { diff --git a/config/types_test.go b/config/types_test.go index 3a1fd6a8426a..caef2b112c00 100644 --- a/config/types_test.go +++ b/config/types_test.go @@ -375,6 +375,7 @@ func TestOptionalInteger(t *testing.T) { } } + // marshal with omitempty type Foo struct { I *OptionalInteger `json:",omitempty"` } @@ -386,6 +387,20 @@ func TestOptionalInteger(t *testing.T) { if string(out) != expected { t.Fatal("expected omitempty to omit the optional integer") } + + // unmarshal from omitempty output and get default value + var foo2 Foo + if err := json.Unmarshal(out, &foo2); err != nil { + t.Fatalf("%s failed to unmarshall with %s", string(out), err) + } + if i := foo2.I.WithDefault(42); i != 42 { + t.Fatalf("expected default value to be used, got %d", i) + } + if !foo2.I.IsDefault() { + t.Fatal("expected value to be the default") + } + + // test invalid values for _, invalid := range []string{ "foo", "-1.1", "1.1", "0.0", "[]", } {