mirror of
https://github.com/logos-messaging/go-waku-rendezvous.git
synced 2026-01-05 22:53:07 +00:00
refactor: obtain the peers from peerstore
This commit is contained in:
parent
1b6d7e4055
commit
ba3a59f225
44
client.go
44
client.go
@ -37,20 +37,18 @@ type RendezvousClient interface {
|
|||||||
DiscoverAsync(ctx context.Context, ns string) (<-chan peer.AddrInfo, error)
|
DiscoverAsync(ctx context.Context, ns string) (<-chan peer.AddrInfo, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRendezvousPoint(host host.Host, p []peer.ID) RendezvousPoint {
|
func NewRendezvousPoint(host host.Host) RendezvousPoint {
|
||||||
return &rendezvousPoint{
|
return &rendezvousPoint{
|
||||||
host: host,
|
host: host,
|
||||||
p: p,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type rendezvousPoint struct {
|
type rendezvousPoint struct {
|
||||||
host host.Host
|
host host.Host
|
||||||
p []peer.ID
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRendezvousClient(host host.Host, rp []peer.ID) RendezvousClient {
|
func NewRendezvousClient(host host.Host) RendezvousClient {
|
||||||
return NewRendezvousClientWithPoint(NewRendezvousPoint(host, rp))
|
return NewRendezvousClientWithPoint(NewRendezvousPoint(host))
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRendezvousClientWithPoint(rp RendezvousPoint) RendezvousClient {
|
func NewRendezvousClientWithPoint(rp RendezvousPoint) RendezvousClient {
|
||||||
@ -61,12 +59,28 @@ type rendezvousClient struct {
|
|||||||
rp RendezvousPoint
|
rp RendezvousPoint
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *rendezvousPoint) getRandomPeer() peer.ID {
|
func (r *rendezvousPoint) getRandomPeer() (peer.ID, error) {
|
||||||
return r.p[rand.Intn(len(r.p))] // nolint: gosec
|
var peerIDs []peer.ID
|
||||||
|
for _, peer := range r.host.Peerstore().Peers() {
|
||||||
|
protocols, err := r.host.Peerstore().SupportsProtocols(peer, string(RendezvousID_v001))
|
||||||
|
if err != nil {
|
||||||
|
log.Error("error obtaining the protocols supported by peers", err)
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if len(protocols) > 0 {
|
||||||
|
peerIDs = append(peerIDs, peer)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return peerIDs[rand.Intn(len(peerIDs))], nil // nolint: gosec
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rp *rendezvousPoint) Register(ctx context.Context, ns string, ttl int) (time.Duration, error) {
|
func (rp *rendezvousPoint) Register(ctx context.Context, ns string, ttl int) (time.Duration, error) {
|
||||||
s, err := rp.host.NewStream(ctx, rp.getRandomPeer(), RendezvousProto)
|
randomPeer, err := rp.getRandomPeer()
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
s, err := rp.host.NewStream(ctx, randomPeer, RendezvousID_v001)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
@ -147,7 +161,12 @@ func registerRefresh(ctx context.Context, rz RendezvousPoint, ns string, ttl int
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (rp *rendezvousPoint) Discover(ctx context.Context, ns string, limit int) ([]Registration, error) {
|
func (rp *rendezvousPoint) Discover(ctx context.Context, ns string, limit int) ([]Registration, error) {
|
||||||
s, err := rp.host.NewStream(ctx, rp.getRandomPeer(), RendezvousProto)
|
randomPeer, err := rp.getRandomPeer()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
s, err := rp.host.NewStream(ctx, randomPeer, RendezvousID_v001)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -196,7 +215,12 @@ func discoverQuery(ns string, limit int, r ggio.Reader, w ggio.Writer) ([]Regist
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (rp *rendezvousPoint) DiscoverAsync(ctx context.Context, ns string) (<-chan Registration, error) {
|
func (rp *rendezvousPoint) DiscoverAsync(ctx context.Context, ns string) (<-chan Registration, error) {
|
||||||
s, err := rp.host.NewStream(ctx, rp.getRandomPeer(), RendezvousProto)
|
randomPeer, err := rp.getRandomPeer()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
s, err := rp.host.NewStream(ctx, randomPeer, RendezvousID_v001)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@ -31,8 +31,8 @@ type record struct {
|
|||||||
expire int64
|
expire int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRendezvousDiscovery(host host.Host, rendezvousPeers []peer.ID) discovery.Discovery {
|
func NewRendezvousDiscovery(host host.Host) discovery.Discovery {
|
||||||
rp := NewRendezvousPoint(host, rendezvousPeers)
|
rp := NewRendezvousPoint(host)
|
||||||
return &rendezvousDiscovery{rp: rp, peerCache: make(map[string]*discoveryCache), rng: rand.New(rand.NewSource(rand.Int63()))}
|
return &rendezvousDiscovery{rp: rp, peerCache: make(map[string]*discoveryCache), rng: rand.New(rand.NewSource(rand.Int63()))}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
5
proto.go
5
proto.go
@ -15,9 +15,8 @@ import (
|
|||||||
var log = logging.Logger("rendezvous")
|
var log = logging.Logger("rendezvous")
|
||||||
|
|
||||||
const (
|
const (
|
||||||
RendezvousProto = protocol.ID("/vac/waku/rendezvous/0.0.1")
|
RendezvousID_v001 = protocol.ID("/vac/waku/rendezvous/0.0.1")
|
||||||
|
DefaultTTL = 2 * 3600 // 2hr
|
||||||
DefaultTTL = 2 * 3600 // 2hr
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type RendezvousError struct {
|
type RendezvousError struct {
|
||||||
|
|||||||
2
svc.go
2
svc.go
@ -48,7 +48,7 @@ func NewRendezvousService(host host.Host, storage Storage, rzs ...RendezvousSync
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (rz *RendezvousService) Start() error {
|
func (rz *RendezvousService) Start() error {
|
||||||
rz.h.SetStreamHandler(RendezvousProto, rz.handleStream)
|
rz.h.SetStreamHandler(RendezvousID_v001, rz.handleStream)
|
||||||
|
|
||||||
if err := rz.startCleaner(); err != nil {
|
if err := rz.startCleaner(); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user