Skip to content

Commit

Permalink
Option to set distance update timeout (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
gammazero authored Jul 31, 2023
1 parent b033ae8 commit cbe0830
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
20 changes: 13 additions & 7 deletions pkg/dtrack/distance_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ type distTrack struct {
errType int
}

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

return updates
}

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

var lookForNew bool
Expand Down Expand Up @@ -78,21 +78,27 @@ func runTracker(ctx context.Context, include, exclude map[peer.ID]struct{}, prov
}
}
}
updateTracks(ctx, provCache, tracks, depthLimit, updates)
updateTracks(ctx, provCache, tracks, timeout, depthLimit, updates)
timer.Reset(updateIn)
case <-ctx.Done():
return
}
}
}

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

func updateTrack(ctx context.Context, pid peer.ID, track *distTrack, provCache *pcache.ProviderCache, depthLimit int64, updates chan<- DistanceUpdate) {
func updateTrack(ctx context.Context, pid peer.ID, track *distTrack, provCache *pcache.ProviderCache, timeout time.Duration, depthLimit int64, updates chan<- DistanceUpdate) {
if timeout != 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, timeout)
defer cancel()
}

pinfo, err := provCache.Get(ctx, pid)
if err != nil {
return
Expand Down
21 changes: 18 additions & 3 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,16 @@ var providerFlags = []cli.Flag{
},
&cli.StringFlag{
Name: "update-interval",
Aliases: []string{"ui"},
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.",
Aliases: []string{"uin"},
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.",
Value: "2m",
},
&cli.StringFlag{
Name: "update-timeout",
Aliases: []string{"uto"},
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.",
Value: "5m",
},
&cli.Int64Flag{
Name: "ad-depth-limit",
Aliases: []string{"adl"},
Expand Down Expand Up @@ -266,9 +272,18 @@ func followDistance(cctx *cli.Context, include, exclude map[peer.ID]struct{}, pc
return err
}

var timeout time.Duration
updateTimeout := cctx.String("update-timeout")
if updateTimeout != "" {
timeout, err = time.ParseDuration(updateTimeout)
if err != nil {
return err
}
}

fmt.Fprintln(os.Stderr, "Showing provider distance updates, ctrl-c to cancel...")
limit := cctx.Int64("ad-depth-limit")
updates := dtrack.RunDistanceTracker(cctx.Context, include, exclude, pc, limit, trackUpdateIn)
updates := dtrack.RunDistanceTracker(cctx.Context, include, exclude, pc, limit, trackUpdateIn, timeout)
for update := range updates {
if update.Err != nil {
fmt.Fprintln(os.Stderr, "Provider", update.ID, "distance error:", update.Err)
Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "v0.0.7"
"version": "v0.0.8"
}

0 comments on commit cbe0830

Please sign in to comment.