Skip to content

Commit

Permalink
feat: Swarm.RelayService (circuit v2) (ipfs#146)
Browse files Browse the repository at this point in the history
* 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`

ipfs/go-ipfs-config#146 (comment)
ipfs/go-ipfs-config#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 (ipfs#150)

adds Internal.Libp2pForceReachability
needed for sharness tests in ipfs#8522

Co-authored-by: Marcin Rataj <lidel@lidel.org>

Co-authored-by: Marcin Rataj <lidel@lidel.org>
  • Loading branch information
marten-seemann and lidel committed Nov 13, 2021
1 parent 7a9d248 commit 155ab2c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 16 deletions.
4 changes: 3 additions & 1 deletion config/internal.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
48 changes: 37 additions & 11 deletions config/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,54 @@ 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

// ConnMgr configures the connection manager.
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.
Expand Down
8 changes: 4 additions & 4 deletions config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
15 changes: 15 additions & 0 deletions config/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ func TestOptionalInteger(t *testing.T) {
}
}

// marshal with omitempty
type Foo struct {
I *OptionalInteger `json:",omitempty"`
}
Expand All @@ -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", "[]",
} {
Expand Down

0 comments on commit 155ab2c

Please sign in to comment.