mirror of
https://github.com/logos-messaging/go-libp2p-rendezvous.git
synced 2026-01-02 04:43:11 +00:00
fix: add filter option for advertise
Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com>
This commit is contained in:
parent
ea43c800bd
commit
10c05aa28d
24
client.go
24
client.go
@ -40,16 +40,20 @@ type RendezvousClient interface {
|
|||||||
DiscoverSubscribe(ctx context.Context, ns string) (<-chan peer.AddrInfo, error)
|
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{
|
return &rendezvousPoint{
|
||||||
host: host,
|
addrFactory: cfg.AddrsFactory,
|
||||||
p: p,
|
host: host,
|
||||||
|
p: p,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type rendezvousPoint struct {
|
type rendezvousPoint struct {
|
||||||
host host.Host
|
addrFactory AddrsFactory
|
||||||
p peer.ID
|
host host.Host
|
||||||
|
p peer.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRendezvousClient(host host.Host, rp peer.ID, sync ...RendezvousSyncClient) RendezvousClient {
|
func NewRendezvousClient(host host.Host, rp peer.ID, sync ...RendezvousSyncClient) RendezvousClient {
|
||||||
@ -75,7 +79,13 @@ func (rp *rendezvousPoint) Register(ctx context.Context, ns string, ttl int) (ti
|
|||||||
r := ggio.NewDelimitedReader(s, inet.MessageSizeMax)
|
r := ggio.NewDelimitedReader(s, inet.MessageSizeMax)
|
||||||
w := ggio.NewDelimitedWriter(s)
|
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)
|
err = w.WriteMsg(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
@ -88,7 +98,7 @@ func (rp *rendezvousPoint) Register(ctx context.Context, ns string, ttl int) (ti
|
|||||||
}
|
}
|
||||||
|
|
||||||
if res.GetType() != pb.Message_REGISTER_RESPONSE {
|
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()
|
response := res.GetRegisterResponse()
|
||||||
|
|||||||
32
options.go
Normal file
32
options.go
Normal file
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user