From 487ec4a0aacee28b7fdd6e846ee317175f4b26b5 Mon Sep 17 00:00:00 2001 From: vyzo Date: Thu, 16 May 2019 18:30:16 +0300 Subject: [PATCH 1/3] allow user-spceified TTL in routing advertisements --- routing.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/routing.go b/routing.go index c618ba7..5d85b90 100644 --- a/routing.go +++ b/routing.go @@ -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) { From 6d54d49a8418ef674029aaeec307447de449025b Mon Sep 17 00:00:00 2001 From: vyzo Date: Thu, 16 May 2019 18:30:53 +0300 Subject: [PATCH 2/3] add options to utility interface --- util.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/util.go b/util.go index be43a4b..2a267ef 100644 --- a/util.go +++ b/util.go @@ -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 { From 98c8684db0a9c25dd3bab3fecc85c3fdc24f1717 Mon Sep 17 00:00:00 2001 From: vyzo Date: Thu, 16 May 2019 18:44:07 +0300 Subject: [PATCH 3/3] fix test --- routing_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routing_test.go b/routing_test.go index 82cebf3..5aff567 100644 --- a/routing_test.go +++ b/routing_test.go @@ -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) }