Skip to content
This repository was archived by the owner on Mar 9, 2022. It is now read-only.

Commit a1583a3

Browse files
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` #146 (comment) #146 (comment) * 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/kubo#8522 Co-authored-by: Marcin Rataj <lidel@lidel.org> Co-authored-by: Marcin Rataj <lidel@lidel.org>
1 parent 7aa6b00 commit a1583a3

File tree

4 files changed

+59
-16
lines changed

4 files changed

+59
-16
lines changed

internal.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package config
22

33
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"`
56
UnixFSShardingSizeThreshold *OptionalString `json:",omitempty"`
7+
Libp2pForceReachability *OptionalString `json:",omitempty"`
68
}
79

810
type InternalBitswap struct {

swarm.go

+37-11
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,54 @@ type SwarmConfig struct {
1616
// DisableRelay explicitly disables the relay transport.
1717
//
1818
// Deprecated: This flag is deprecated and is overridden by
19-
// `Transports.Relay` if specified.
19+
// `Swarm.Transports.Relay` if specified.
2020
DisableRelay bool `json:",omitempty"`
2121

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.
3225
EnableAutoRelay bool
3326

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+
3431
// Transports contains flags to enable/disable libp2p transports.
3532
Transports Transports
3633

3734
// ConnMgr configures the connection manager.
3835
ConnMgr ConnMgr
3936
}
4037

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+
4167
type Transports struct {
4268
// Network specifies the base transports we'll use for dialing. To
4369
// listen on a transport, add the transport to your Addresses.Swarm.

types.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -270,16 +270,16 @@ type OptionalInteger struct {
270270
}
271271

272272
// 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 {
275275
return defaultValue
276276
}
277277
return *p.value
278278
}
279279

280280
// 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
283283
}
284284

285285
func (p OptionalInteger) MarshalJSON() ([]byte, error) {

types_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ func TestOptionalInteger(t *testing.T) {
375375
}
376376
}
377377

378+
// marshal with omitempty
378379
type Foo struct {
379380
I *OptionalInteger `json:",omitempty"`
380381
}
@@ -386,6 +387,20 @@ func TestOptionalInteger(t *testing.T) {
386387
if string(out) != expected {
387388
t.Fatal("expected omitempty to omit the optional integer")
388389
}
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
389404
for _, invalid := range []string{
390405
"foo", "-1.1", "1.1", "0.0", "[]",
391406
} {

0 commit comments

Comments
 (0)