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

Commit

Permalink
feat: functional options
Browse files Browse the repository at this point in the history
Introduce functional options. This should make it easier for us to add
additional options in the future (e.g., known relays, relay policies, etc.).
  • Loading branch information
Stebalien committed Mar 31, 2020
1 parent 1b975c0 commit 7392efe
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,24 @@ type Relay struct {
}

// RelayOpts are options for configuring the relay transport.
type RelayOpt int
type RelayOpt func(*Relay) error

var (
// OptActive configures the relay transport to actively establish
// outbound connections on behalf of clients. You probably don't want to
// enable this unless you know what you're doing.
OptActive = RelayOpt(0)
OptActive RelayOpt = func(r *Relay) error {
r.active = true
return nil
}
// OptHop configures the relay transport to accept requests to relay
// traffic on behalf of third-parties. Unless OptActive is specified,
// this will only relay traffic between peers already connected to this
// node.
OptHop = RelayOpt(1)
OptHop = func(r *Relay) error {
r.hop = true
return nil
}
// OptDiscovery is a no-op. It was introduced as a way to probe new
// peers to see if they were willing to act as a relays. However, in
// practice, it's useless. While it does test to see if these peers are
Expand All @@ -75,7 +81,13 @@ var (
//
// This option may be re-enabled in the future but for now you shouldn't
// use it.
OptDiscovery = RelayOpt(2)
OptDiscovery = func(r *Relay) error {
log.Errorf(
"circuit.OptDiscovery is now a no-op: %s",
"dialing peers with a random relay is no longer supported",
)
return nil
}
)

type RelayError struct {
Expand All @@ -97,18 +109,8 @@ func NewRelay(ctx context.Context, h host.Host, upgrader *tptu.Upgrader, opts ..
}

for _, opt := range opts {
switch opt {
case OptActive:
r.active = true
case OptHop:
r.hop = true
case OptDiscovery:
log.Errorf(
"circuit.OptDiscovery is now a no-op: %s",
"dialing peers with a random relay is no longer supported",
)
default:
return nil, fmt.Errorf("unrecognized option: %d", opt)
if err := opt(r); err != nil {
return nil, err
}
}

Expand Down

0 comments on commit 7392efe

Please sign in to comment.