ceremonyclient/go-libp2p/p2p/discovery/util/util.go
Cassandra Heart dbd95bd9e9
v2.1.0 (#439)
* v2.1.0 [omit consensus and adjacent] - this commit will be amended with the full release after the file copy is complete

* 2.1.0 main node rollup
2025-09-30 02:48:15 -05:00

59 lines
1.2 KiB
Go

package util
import (
"context"
"time"
"github.com/libp2p/go-libp2p/core/discovery"
"github.com/libp2p/go-libp2p/core/peer"
logging "github.com/libp2p/go-libp2p/gologshim"
)
var log = logging.Logger("discovery-util")
// FindPeers is a utility function that synchronously collects peers from a Discoverer.
func FindPeers(ctx context.Context, d discovery.Discoverer, ns string, opts ...discovery.Option) ([]peer.AddrInfo, error) {
ch, err := d.FindPeers(ctx, ns, opts...)
if err != nil {
return nil, err
}
res := make([]peer.AddrInfo, 0, len(ch))
for pi := range ch {
res = append(res, pi)
}
return res, nil
}
// Advertise is a utility function that persistently advertises a service through an Advertiser.
func Advertise(ctx context.Context, a discovery.Advertiser, ns string, opts ...discovery.Option) {
go func() {
for {
ttl, err := a.Advertise(ctx, ns, opts...)
if err != nil {
log.Debug("Error advertising", "namespace", ns, "err", err)
if ctx.Err() != nil {
return
}
select {
case <-time.After(2 * time.Minute):
continue
case <-ctx.Done():
return
}
}
wait := 7 * ttl / 8
select {
case <-time.After(wait):
case <-ctx.Done():
return
}
}
}()
}