Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add filter option for advertise #7

Merged
merged 2 commits into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,20 @@ type RendezvousClient interface {
DiscoverSubscribe(ctx context.Context, ns string) (<-chan peer.AddrInfo, error)
}

func NewRendezvousPoint(host host.Host, p peer.ID) RendezvousPoint {
func NewRendezvousPoint(host host.Host, p peer.ID, opts ...RendezvousPointOption) RendezvousPoint {
cfg := defaultRendezvousPointConfig
cfg.apply(opts...)
return &rendezvousPoint{
host: host,
p: p,
addrFactory: cfg.AddrsFactory,
host: host,
p: p,
}
}

type rendezvousPoint struct {
host host.Host
p peer.ID
addrFactory AddrsFactory
host host.Host
p peer.ID
}

func NewRendezvousClient(host host.Host, rp peer.ID, sync ...RendezvousSyncClient) RendezvousClient {
Expand All @@ -75,7 +79,13 @@ func (rp *rendezvousPoint) Register(ctx context.Context, ns string, ttl int) (ti
r := ggio.NewDelimitedReader(s, inet.MessageSizeMax)
w := ggio.NewDelimitedWriter(s)

req := newRegisterMessage(ns, peer.AddrInfo{ID: rp.host.ID(), Addrs: rp.host.Addrs()}, ttl)
addrs := rp.addrFactory(rp.host.Addrs())
if len(addrs) == 0 {
return 0, fmt.Errorf("no addrs available to advertise: %s", ns)
}

log.Debugf("advertising on `%s` with: %v", ns, addrs)
req := newRegisterMessage(ns, peer.AddrInfo{ID: rp.host.ID(), Addrs: addrs}, ttl)
err = w.WriteMsg(req)
if err != nil {
return 0, err
Expand All @@ -88,7 +98,7 @@ func (rp *rendezvousPoint) Register(ctx context.Context, ns string, ttl int) (ti
}

if res.GetType() != pb.Message_REGISTER_RESPONSE {
return 0, fmt.Errorf("Unexpected response: %s", res.GetType().String())
return 0, fmt.Errorf("unexpected response: %s", res.GetType().String())
}

response := res.GetRegisterResponse()
Expand Down
32 changes: 32 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package rendezvous

import (
ma "github.com/multiformats/go-multiaddr"
)

type RendezvousPointOption func(cfg *rendezvousPointConfig)

type AddrsFactory func(addrs []ma.Multiaddr) []ma.Multiaddr

var DefaultAddrFactory = func(addrs []ma.Multiaddr) []ma.Multiaddr { return addrs }

var defaultRendezvousPointConfig = rendezvousPointConfig{
AddrsFactory: DefaultAddrFactory,
}

type rendezvousPointConfig struct {
AddrsFactory AddrsFactory
}

func (cfg *rendezvousPointConfig) apply(opts ...RendezvousPointOption) {
for _, opt := range opts {
opt(cfg)
}
}

// AddrsFactory configures libp2p to use the given address factory.
func ClientWithAddrsFactory(factory AddrsFactory) RendezvousPointOption {
return func(cfg *rendezvousPointConfig) {
cfg.AddrsFactory = factory
}
}