Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

Commit

Permalink
Merge pull request #25 from libp2p/feat/options
Browse files Browse the repository at this point in the history
More consistent use of options
  • Loading branch information
vyzo authored May 17, 2019
2 parents 4cb4193 + 98c8684 commit 74aa1c8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
17 changes: 14 additions & 3 deletions routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion routing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
10 changes: 5 additions & 5 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down

0 comments on commit 74aa1c8

Please sign in to comment.