Skip to content

Commit

Permalink
Merge pull request #454 from edjx/cfg-routing-table-latency
Browse files Browse the repository at this point in the history
Make the Routing Table's latency tolerance configurable.
  • Loading branch information
aarshkshah1992 authored Feb 19, 2020
2 parents 747410e + 28f2f2a commit 5552b3f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
14 changes: 7 additions & 7 deletions dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func New(ctx context.Context, h host.Host, options ...opts.Option) (*IpfsDHT, er
if err := cfg.Apply(append([]opts.Option{opts.Defaults}, options...)...); err != nil {
return nil, err
}
dht := makeDHT(ctx, h, cfg.Datastore, cfg.Protocols, cfg.BucketSize)
dht := makeDHT(ctx, h, &cfg)
dht.autoRefresh = cfg.RoutingTable.AutoRefresh
dht.rtRefreshPeriod = cfg.RoutingTable.RefreshPeriod
dht.rtRefreshQueryTimeout = cfg.RoutingTable.RefreshQueryTimeout
Expand Down Expand Up @@ -152,9 +152,9 @@ func NewDHTClient(ctx context.Context, h host.Host, dstore ds.Batching) *IpfsDHT
return dht
}

func makeDHT(ctx context.Context, h host.Host, dstore ds.Batching, protocols []protocol.ID, bucketSize int) *IpfsDHT {
func makeDHT(ctx context.Context, h host.Host, cfg *opts.Options) *IpfsDHT {
self := kb.ConvertPeerID(h.ID())
rt := kb.NewRoutingTable(bucketSize, self, time.Minute, h.Peerstore())
rt := kb.NewRoutingTable(cfg.BucketSize, self, cfg.RoutingTable.LatencyTolerance, h.Peerstore())
cmgr := h.ConnManager()

rt.PeerAdded = func(p peer.ID) {
Expand All @@ -167,17 +167,17 @@ func makeDHT(ctx context.Context, h host.Host, dstore ds.Batching, protocols []p
}

dht := &IpfsDHT{
datastore: dstore,
datastore: cfg.Datastore,
self: h.ID(),
peerstore: h.Peerstore(),
host: h,
strmap: make(map[peer.ID]*messageSender),
ctx: ctx,
providers: providers.NewProviderManager(ctx, h.ID(), dstore),
providers: providers.NewProviderManager(ctx, h.ID(), cfg.Datastore),
birth: time.Now(),
routingTable: rt,
protocols: protocols,
bucketSize: bucketSize,
protocols: cfg.Protocols,
bucketSize: cfg.BucketSize,
triggerRtRefresh: make(chan chan<- error),
}

Expand Down
11 changes: 11 additions & 0 deletions opts/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Options struct {
RefreshQueryTimeout time.Duration
RefreshPeriod time.Duration
AutoRefresh bool
LatencyTolerance time.Duration
}
}

Expand Down Expand Up @@ -61,6 +62,7 @@ var Defaults = func(o *Options) error {
o.EnableProviders = true
o.EnableValues = true

o.RoutingTable.LatencyTolerance = time.Minute
o.RoutingTable.RefreshQueryTimeout = 10 * time.Second
o.RoutingTable.RefreshPeriod = 1 * time.Hour
o.RoutingTable.AutoRefresh = true
Expand All @@ -69,6 +71,15 @@ var Defaults = func(o *Options) error {
return nil
}

// RoutingTableLatencyTolerance sets the maximum acceptable latency for peers
// in the routing table's cluster.
func RoutingTableLatencyTolerance(latency time.Duration) Option {
return func(o *Options) error {
o.RoutingTable.LatencyTolerance = latency
return nil
}
}

// RoutingTableRefreshQueryTimeout sets the timeout for routing table refresh
// queries.
func RoutingTableRefreshQueryTimeout(timeout time.Duration) Option {
Expand Down

0 comments on commit 5552b3f

Please sign in to comment.