@cryptotaxi247 / kubo / commits / d2268793d

go-ipfs-config: feat: Swarm.RelayService (circuit v2) (#146)

* remove the EnableRelayHop option in the SwarmConfig * add an option to disable the limited relay * make the relay service resources configurable * refactor: use custom types This enables us to swap defaults in go-ipfs without touching the config file generated during `ipfs init` https://github.com/ipfs/go-ipfs-config/pull/146#discussion_r734728162 https://github.com/ipfs/go-ipfs-config/pull/146#discussion_r734728019 * use OptionalDuration in RelayService configuration * fix: *OptionalInteger with omitempty This removes null values from the config * fix: Flag does not need to be a pointer * refactor: flatten RelayService limits this simplifies consumer code and removes nil footgun * docs: clarify different relay types * feat: flag for ForceReachability mode in libp2p (#150) adds Internal.Libp2pForceReachability needed for sharness tests in ipfs/go-ipfs#8522 Co-authored-by: Marcin Rataj <lidel@lidel.org> Co-authored-by: Marcin Rataj <lidel@lidel.org>

Marten Seemann committed Nov 13, 2021 at 15:07 UTC d2268793dc0e5faa6e7424cdbbcd35180cb42738
4 files changed +59 -16
config/internal.go
+3 -1
@@ -1,8 +1,10 @@
1 package config
2
3 type Internal struct {
4 - Bitswap *InternalBitswap `json:",omitempty"` // This is omitempty since we are expecting to make changes to all subcomponents of Internal
4 + // All marked as omitempty since we are expecting to make changes to all subcomponents of Internal
5 + Bitswap *InternalBitswap `json:",omitempty"`
6 UnixFSShardingSizeThreshold *OptionalString `json:",omitempty"`
7 + Libp2pForceReachability *OptionalString `json:",omitempty"`
8 }
9
10 type InternalBitswap struct {
config/swarm.go
+37 -11
@@ -16,21 +16,18 @@ type SwarmConfig struct {
16 // DisableRelay explicitly disables the relay transport.
17 //
18 // Deprecated: This flag is deprecated and is overridden by
19 - // `Transports.Relay` if specified.
19 + // `Swarm.Transports.Relay` if specified.
20 DisableRelay bool `json:",omitempty"`
21
22 - // EnableRelayHop makes this node act as a public relay, relaying
23 - // traffic between other nodes.
24 - EnableRelayHop bool
25 -
26 - // EnableAutoRelay enables the "auto relay" feature.
27 - //
28 - // When both EnableAutoRelay and EnableRelayHop are set, this go-ipfs node
29 - // will advertise itself as a public relay. Otherwise it will find and use
30 - // advertised public relays when it determines that it's not reachable
31 - // from the public internet.
22 + // EnableAutoRelay enables the "auto relay user" feature.
23 + // Node will find and use advertised public relays when it determines that
24 + // it's not reachable from the public internet.
25 EnableAutoRelay bool
26
27 + // RelayService.* controls the "auto relay service" feature.
28 + // When enabled, node will provide a limited relay service to other peers.
29 + RelayService RelayService
30 +
31 // Transports contains flags to enable/disable libp2p transports.
32 Transports Transports
33
@@ -38,6 +35,35 @@ type SwarmConfig struct {
35 ConnMgr ConnMgr
36 }
37
38 +// RelayService configures the resources of the circuit v2 relay.
39 +// For every field a reasonable default will be defined in go-ipfs.
40 +type RelayService struct {
41 + // Enables the limited relay (circuit v2 relay).
42 + Enabled Flag `json:",omitempty"`
43 +
44 + // ConnectionDurationLimit is the time limit before resetting a relayed connection.
45 + ConnectionDurationLimit *OptionalDuration `json:",omitempty"`
46 + // ConnectionDataLimit is the limit of data relayed (on each direction) before resetting the connection.
47 + ConnectionDataLimit *OptionalInteger `json:",omitempty"`
48 +
49 + // ReservationTTL is the duration of a new (or refreshed reservation).
50 + ReservationTTL *OptionalDuration `json:",omitempty"`
51 +
52 + // MaxReservations is the maximum number of active relay slots.
53 + MaxReservations *OptionalInteger `json:",omitempty"`
54 + // MaxCircuits is the maximum number of open relay connections for each peer; defaults to 16.
55 + MaxCircuits *OptionalInteger `json:",omitempty"`
56 + // BufferSize is the size of the relayed connection buffers.
57 + BufferSize *OptionalInteger `json:",omitempty"`
58 +
59 + // MaxReservationsPerPeer is the maximum number of reservations originating from the same peer.
60 + MaxReservationsPerPeer *OptionalInteger `json:",omitempty"`
61 + // MaxReservationsPerIP is the maximum number of reservations originating from the same IP address.
62 + MaxReservationsPerIP *OptionalInteger `json:",omitempty"`
63 + // MaxReservationsPerASN is the maximum number of reservations origination from the same ASN.
64 + MaxReservationsPerASN *OptionalInteger `json:",omitempty"`
65 +}
66 +
67 type Transports struct {
68 // Network specifies the base transports we'll use for dialing. To
69 // listen on a transport, add the transport to your Addresses.Swarm.
config/types.go
+4 -4
@@ -270,16 +270,16 @@ type OptionalInteger struct {
270 }
271
272 // WithDefault resolves the integer with the given default.
273 -func (p OptionalInteger) WithDefault(defaultValue int64) (value int64) {
274 - if p.value == nil {
273 +func (p *OptionalInteger) WithDefault(defaultValue int64) (value int64) {
274 + if p == nil || p.value == nil {
275 return defaultValue
276 }
277 return *p.value
278 }
279
280 // IsDefault returns if this is a default optional integer
281 -func (p OptionalInteger) IsDefault() bool {
282 - return p.value == nil
281 +func (p *OptionalInteger) IsDefault() bool {
282 + return p == nil || p.value == nil
283 }
284
285 func (p OptionalInteger) MarshalJSON() ([]byte, error) {
config/types_test.go
+15
@@ -375,6 +375,7 @@ func TestOptionalInteger(t *testing.T) {
375 }
376 }
377
378 + // marshal with omitempty
379 type Foo struct {
380 I *OptionalInteger `json:",omitempty"`
381 }
@@ -386,6 +387,20 @@ func TestOptionalInteger(t *testing.T) {
387 if string(out) != expected {
388 t.Fatal("expected omitempty to omit the optional integer")
389 }
390 +
391 + // unmarshal from omitempty output and get default value
392 + var foo2 Foo
393 + if err := json.Unmarshal(out, &foo2); err != nil {
394 + t.Fatalf("%s failed to unmarshall with %s", string(out), err)
395 + }
396 + if i := foo2.I.WithDefault(42); i != 42 {
397 + t.Fatalf("expected default value to be used, got %d", i)
398 + }
399 + if !foo2.I.IsDefault() {
400 + t.Fatal("expected value to be the default")
401 + }
402 +
403 + // test invalid values
404 for _, invalid := range []string{
405 "foo", "-1.1", "1.1", "0.0", "[]",
406 } {