Skip to content

Commit cbe0830

Browse files
authored
Option to set distance update timeout (#48)
1 parent b033ae8 commit cbe0830

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

pkg/dtrack/distance_tracker.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ type distTrack struct {
3434
errType int
3535
}
3636

37-
func RunDistanceTracker(ctx context.Context, include, exclude map[peer.ID]struct{}, provCache *pcache.ProviderCache, depthLimit int64, updateIn time.Duration) <-chan DistanceUpdate {
37+
func RunDistanceTracker(ctx context.Context, include, exclude map[peer.ID]struct{}, provCache *pcache.ProviderCache, depthLimit int64, updateIn, timeout time.Duration) <-chan DistanceUpdate {
3838
updates := make(chan DistanceUpdate)
39-
go runTracker(ctx, include, exclude, provCache, updateIn, depthLimit, updates)
39+
go runTracker(ctx, include, exclude, provCache, updateIn, timeout, depthLimit, updates)
4040

4141
return updates
4242
}
4343

44-
func runTracker(ctx context.Context, include, exclude map[peer.ID]struct{}, provCache *pcache.ProviderCache, updateIn time.Duration, depthLimit int64, updates chan<- DistanceUpdate) {
44+
func runTracker(ctx context.Context, include, exclude map[peer.ID]struct{}, provCache *pcache.ProviderCache, updateIn, timeout time.Duration, depthLimit int64, updates chan<- DistanceUpdate) {
4545
defer close(updates)
4646

4747
var lookForNew bool
@@ -78,21 +78,27 @@ func runTracker(ctx context.Context, include, exclude map[peer.ID]struct{}, prov
7878
}
7979
}
8080
}
81-
updateTracks(ctx, provCache, tracks, depthLimit, updates)
81+
updateTracks(ctx, provCache, tracks, timeout, depthLimit, updates)
8282
timer.Reset(updateIn)
8383
case <-ctx.Done():
8484
return
8585
}
8686
}
8787
}
8888

89-
func updateTracks(ctx context.Context, provCache *pcache.ProviderCache, tracks map[peer.ID]*distTrack, depthLimit int64, updates chan<- DistanceUpdate) {
89+
func updateTracks(ctx context.Context, provCache *pcache.ProviderCache, tracks map[peer.ID]*distTrack, timeout time.Duration, depthLimit int64, updates chan<- DistanceUpdate) {
9090
for providerID, track := range tracks {
91-
updateTrack(ctx, providerID, track, provCache, depthLimit, updates)
91+
updateTrack(ctx, providerID, track, provCache, timeout, depthLimit, updates)
9292
}
9393
}
9494

95-
func updateTrack(ctx context.Context, pid peer.ID, track *distTrack, provCache *pcache.ProviderCache, depthLimit int64, updates chan<- DistanceUpdate) {
95+
func updateTrack(ctx context.Context, pid peer.ID, track *distTrack, provCache *pcache.ProviderCache, timeout time.Duration, depthLimit int64, updates chan<- DistanceUpdate) {
96+
if timeout != 0 {
97+
var cancel context.CancelFunc
98+
ctx, cancel = context.WithTimeout(ctx, timeout)
99+
defer cancel()
100+
}
101+
96102
pinfo, err := provCache.Get(ctx, pid)
97103
if err != nil {
98104
return

pkg/provider/provider.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,16 @@ var providerFlags = []cli.Flag{
7878
},
7979
&cli.StringFlag{
8080
Name: "update-interval",
81-
Aliases: []string{"ui"},
82-
Usage: "Time to wait between distance update checks. The value is an integer string ending in s, m, h for seconds. minutes, hours. Updates will only be seen as fast as they become visible at the upstream location.",
81+
Aliases: []string{"uin"},
82+
Usage: "Time to wait between distance update checks when using --follow-dist. The value is an integer string ending in s, m, h for seconds. minutes, hours. Updates will only be seen as fast as they become visible at the upstream location.",
8383
Value: "2m",
8484
},
85+
&cli.StringFlag{
86+
Name: "update-timeout",
87+
Aliases: []string{"uto"},
88+
Usage: "Timeout for getting a provider distance, when using --follow-dist. The value is an integer string ending in s, m, h for seconds. minutes, hours.",
89+
Value: "5m",
90+
},
8591
&cli.Int64Flag{
8692
Name: "ad-depth-limit",
8793
Aliases: []string{"adl"},
@@ -266,9 +272,18 @@ func followDistance(cctx *cli.Context, include, exclude map[peer.ID]struct{}, pc
266272
return err
267273
}
268274

275+
var timeout time.Duration
276+
updateTimeout := cctx.String("update-timeout")
277+
if updateTimeout != "" {
278+
timeout, err = time.ParseDuration(updateTimeout)
279+
if err != nil {
280+
return err
281+
}
282+
}
283+
269284
fmt.Fprintln(os.Stderr, "Showing provider distance updates, ctrl-c to cancel...")
270285
limit := cctx.Int64("ad-depth-limit")
271-
updates := dtrack.RunDistanceTracker(cctx.Context, include, exclude, pc, limit, trackUpdateIn)
286+
updates := dtrack.RunDistanceTracker(cctx.Context, include, exclude, pc, limit, trackUpdateIn, timeout)
272287
for update := range updates {
273288
if update.Err != nil {
274289
fmt.Fprintln(os.Stderr, "Provider", update.ID, "distance error:", update.Err)

version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"version": "v0.0.7"
2+
"version": "v0.0.8"
33
}

0 commit comments

Comments
 (0)