Skip to content

Commit 5552b3f

Browse files
Merge pull request #454 from edjx/cfg-routing-table-latency
Make the Routing Table's latency tolerance configurable.
2 parents 747410e + 28f2f2a commit 5552b3f

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

dht.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func New(ctx context.Context, h host.Host, options ...opts.Option) (*IpfsDHT, er
9999
if err := cfg.Apply(append([]opts.Option{opts.Defaults}, options...)...); err != nil {
100100
return nil, err
101101
}
102-
dht := makeDHT(ctx, h, cfg.Datastore, cfg.Protocols, cfg.BucketSize)
102+
dht := makeDHT(ctx, h, &cfg)
103103
dht.autoRefresh = cfg.RoutingTable.AutoRefresh
104104
dht.rtRefreshPeriod = cfg.RoutingTable.RefreshPeriod
105105
dht.rtRefreshQueryTimeout = cfg.RoutingTable.RefreshQueryTimeout
@@ -152,9 +152,9 @@ func NewDHTClient(ctx context.Context, h host.Host, dstore ds.Batching) *IpfsDHT
152152
return dht
153153
}
154154

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

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

169169
dht := &IpfsDHT{
170-
datastore: dstore,
170+
datastore: cfg.Datastore,
171171
self: h.ID(),
172172
peerstore: h.Peerstore(),
173173
host: h,
174174
strmap: make(map[peer.ID]*messageSender),
175175
ctx: ctx,
176-
providers: providers.NewProviderManager(ctx, h.ID(), dstore),
176+
providers: providers.NewProviderManager(ctx, h.ID(), cfg.Datastore),
177177
birth: time.Now(),
178178
routingTable: rt,
179-
protocols: protocols,
180-
bucketSize: bucketSize,
179+
protocols: cfg.Protocols,
180+
bucketSize: cfg.BucketSize,
181181
triggerRtRefresh: make(chan chan<- error),
182182
}
183183

opts/options.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type Options struct {
3434
RefreshQueryTimeout time.Duration
3535
RefreshPeriod time.Duration
3636
AutoRefresh bool
37+
LatencyTolerance time.Duration
3738
}
3839
}
3940

@@ -61,6 +62,7 @@ var Defaults = func(o *Options) error {
6162
o.EnableProviders = true
6263
o.EnableValues = true
6364

65+
o.RoutingTable.LatencyTolerance = time.Minute
6466
o.RoutingTable.RefreshQueryTimeout = 10 * time.Second
6567
o.RoutingTable.RefreshPeriod = 1 * time.Hour
6668
o.RoutingTable.AutoRefresh = true
@@ -69,6 +71,15 @@ var Defaults = func(o *Options) error {
6971
return nil
7072
}
7173

74+
// RoutingTableLatencyTolerance sets the maximum acceptable latency for peers
75+
// in the routing table's cluster.
76+
func RoutingTableLatencyTolerance(latency time.Duration) Option {
77+
return func(o *Options) error {
78+
o.RoutingTable.LatencyTolerance = latency
79+
return nil
80+
}
81+
}
82+
7283
// RoutingTableRefreshQueryTimeout sets the timeout for routing table refresh
7384
// queries.
7485
func RoutingTableRefreshQueryTimeout(timeout time.Duration) Option {

0 commit comments

Comments
 (0)