Unverified Commit 74aa1c8c authored by vyzo's avatar vyzo Committed by GitHub

Merge pull request #25 from libp2p/feat/options

More consistent use of options
parents 4cb4193d 98c8684d
Pipeline #557 failed with stages
in 0 seconds
......@@ -21,6 +21,19 @@ func NewRoutingDiscovery(router routing.ContentRouting) *RoutingDiscovery {
}
func (d *RoutingDiscovery) Advertise(ctx context.Context, ns string, opts ...Option) (time.Duration, error) {
var options Options
err := options.Apply(opts...)
if err != nil {
return 0, err
}
ttl := options.Ttl
if ttl == 0 || ttl > 3*time.Hour {
// the DHT provider record validity is 24hrs, but it is recommnded to republish at least every 6hrs
// we go one step further and republish every 3hrs
ttl = 3 * time.Hour
}
cid, err := nsToCid(ns)
if err != nil {
return 0, err
......@@ -37,9 +50,7 @@ func (d *RoutingDiscovery) Advertise(ctx context.Context, ns string, opts ...Opt
return 0, err
}
// the DHT provider record validity is 24hrs, but it is recommnded to republish at least every 6hrs
// we go one step further and republish every 3hrs
return 3 * time.Hour, nil
return ttl, nil
}
func (d *RoutingDiscovery) FindPeers(ctx context.Context, ns string, opts ...Option) (<-chan pstore.PeerInfo, error) {
......
......@@ -89,7 +89,7 @@ func TestRoutingDiscovery(t *testing.T) {
t.Fatal(err)
}
pis, err := FindPeers(ctx, d2, "/test", 20)
pis, err := FindPeers(ctx, d2, "/test", Limit(20))
if err != nil {
t.Fatal(err)
}
......
......@@ -11,10 +11,10 @@ import (
var log = logging.Logger("discovery")
// FindPeers is a utility function that synchonously collects peers from a Discoverer
func FindPeers(ctx context.Context, d Discoverer, ns string, limit int) ([]pstore.PeerInfo, error) {
res := make([]pstore.PeerInfo, 0, limit)
func FindPeers(ctx context.Context, d Discoverer, ns string, opts ...Option) ([]pstore.PeerInfo, error) {
var res []pstore.PeerInfo
ch, err := d.FindPeers(ctx, ns, Limit(limit))
ch, err := d.FindPeers(ctx, ns, opts...)
if err != nil {
return nil, err
}
......@@ -27,10 +27,10 @@ func FindPeers(ctx context.Context, d Discoverer, ns string, limit int) ([]pstor
}
// Advertise is a utility function that persistently advertises a service through an Advertiser
func Advertise(ctx context.Context, a Advertiser, ns string) {
func Advertise(ctx context.Context, a Advertiser, ns string, opts ...Option) {
go func() {
for {
ttl, err := a.Advertise(ctx, ns)
ttl, err := a.Advertise(ctx, ns, opts...)
if err != nil {
log.Debugf("Error advertising %s: %s", ns, err.Error())
if ctx.Err() != nil {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment