Skip to content

Commit

Permalink
Fix distance track when provider not found
Browse files Browse the repository at this point in the history
- Configurable advertisement depth limit when finding distance.
- Default advertisement depth limit to 5000
  • Loading branch information
gammazero committed Jul 19, 2023
1 parent 44e2b64 commit 7ce3756
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 18 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/ipfs/go-log/v2 v2.5.1
github.com/ipld/go-car/v2 v2.10.0
github.com/ipld/go-ipld-prime v0.20.0
github.com/ipni/go-libipni v0.2.10
github.com/ipni/go-libipni v0.2.12
github.com/libp2p/go-libp2p v0.28.1
github.com/mattn/go-isatty v0.0.19
github.com/montanaflynn/stats v0.7.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ github.com/ipld/go-ipld-prime v0.9.1-0.20210324083106-dc342a9917db/go.mod h1:KvB
github.com/ipld/go-ipld-prime v0.20.0 h1:Ud3VwE9ClxpO2LkCYP7vWPc0Fo+dYdYzgxUJZ3uRG4g=
github.com/ipld/go-ipld-prime v0.20.0/go.mod h1:PzqZ/ZR981eKbgdr3y2DJYeD/8bgMawdGVlJDE8kK+M=
github.com/ipld/go-ipld-prime/storage/bsadapter v0.0.0-20230102063945-1a409dc236dd h1:gMlw/MhNr2Wtp5RwGdsW23cs+yCuj9k2ON7i9MiJlRo=
github.com/ipni/go-libipni v0.2.10 h1:XDEchmQwkrD/c67Fk3+dVcpdxcKPz5NbsPSAAjqF2Bs=
github.com/ipni/go-libipni v0.2.10/go.mod h1:dhBH9HwxT6HzQPRZ8ikWv+ccqF8ucMIoGiiTSrHA4tw=
github.com/ipni/go-libipni v0.2.12 h1:vR8fuUUdqTpBrykojxV7uFdkHLOV8RmtUtSFTQJR0yk=
github.com/ipni/go-libipni v0.2.12/go.mod h1:dhBH9HwxT6HzQPRZ8ikWv+ccqF8ucMIoGiiTSrHA4tw=
github.com/ipsn/go-secp256k1 v0.0.0-20180726113642-9d62b9f0bc52 h1:QG4CGBqCeuBo6aZlGAamSkxWdgWfZGeE49eUOWJPA4c=
github.com/jackpal/go-nat-pmp v1.0.2 h1:KzKSgb7qkJvOUTqYl9/Hg/me3pWgBmERKrTGD7BdWus=
github.com/jackpal/go-nat-pmp v1.0.2/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc=
Expand Down
2 changes: 1 addition & 1 deletion pkg/adpub/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

const (
defaultAdChainDepthLimit = 50000
defaultAdChainDepthLimit = 10000
defaultEntriesDepthLimit = 1000
)

Expand Down
29 changes: 21 additions & 8 deletions pkg/dtrack/distance_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
errTypeNone = iota
errTypeNoPublisher
errTypeNoSync
errTypeNotFound
errTypePubClient
errTypeUpdate
)
Expand All @@ -34,14 +35,14 @@ type distTrack struct {
errType int
}

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

return updates
}

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

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

func updateTracks(ctx context.Context, provCache *pcache.ProviderCache, tracks map[peer.ID]*distTrack, updates chan<- DistanceUpdate) {
func updateTracks(ctx context.Context, provCache *pcache.ProviderCache, tracks map[peer.ID]*distTrack, depthLimit int64, updates chan<- DistanceUpdate) {
var wg sync.WaitGroup
for providerID, dtrack := range tracks {
wg.Add(1)
go func(pid peer.ID, track *distTrack) {
updateTrack(ctx, pid, track, provCache, updates)
updateTrack(ctx, pid, track, provCache, depthLimit, updates)
wg.Done()
}(providerID, dtrack)
}
wg.Wait()
}

func updateTrack(ctx context.Context, pid peer.ID, track *distTrack, provCache *pcache.ProviderCache, updates chan<- DistanceUpdate) {
func updateTrack(ctx context.Context, pid peer.ID, track *distTrack, provCache *pcache.ProviderCache, depthLimit int64, updates chan<- DistanceUpdate) {
pinfo, err := provCache.Get(ctx, pid)
if err != nil {
return
}

if pinfo == nil {
if track.errType != errTypeNotFound {
track.errType = errTypeNotFound
track.err = fmt.Errorf("provider info not found")
updates <- DistanceUpdate{
ID: pid,
Err: track.err,
}
}
return
}

if pinfo.LastAdvertisement == cid.Undef {
if track.errType != errTypeNoSync {
track.errType = errTypeNoSync
Expand All @@ -128,7 +141,7 @@ func updateTrack(ctx context.Context, pid peer.ID, track *distTrack, provCache *
return
}

pubClient, err := adpub.NewClient(*pinfo.Publisher)
pubClient, err := adpub.NewClient(*pinfo.Publisher, adpub.WithAdChainDepthLimit(depthLimit))
if err != nil {
if track.errType != errTypePubClient {
track.errType = errTypePubClient
Expand Down
18 changes: 12 additions & 6 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ Here is an example that shows using the output of one provider command to filter

var providerFlags = []cli.Flag{
&cli.StringSliceFlag{
Name: "indexer",
Usage: "Indexer URL. Specifying multiple results in a unified view of providers across all.",
Aliases: []string{"i"},
Value: cli.NewStringSlice("http://localhost:3000"),
Name: "indexer",
Usage: "Indexer URL. Specifying multiple results in a unified view of providers across all.",
Aliases: []string{"i"},
Required: true,
},
&cli.StringSliceFlag{
Name: "pid",
Expand Down Expand Up @@ -82,6 +82,12 @@ var providerFlags = []cli.Flag{
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.",
Value: "2m",
},
&cli.Int64Flag{
Name: "ad-depth-limit",
Aliases: []string{"adl"},
Usage: "Limit on number of advertisements when finding distance. 0 for unlimited.",
Value: 5000,
},
}

func providerAction(cctx *cli.Context) error {
Expand Down Expand Up @@ -245,7 +251,7 @@ func followDistance(cctx *cli.Context, include, exclude map[peer.ID]struct{}, pc
}

fmt.Fprintln(os.Stderr, "Showing provider distance updates, ctrl-c to cancel...")
updates := dtrack.RunDistanceTracker(cctx.Context, include, exclude, pc, trackUpdateIn)
updates := dtrack.RunDistanceTracker(cctx.Context, include, exclude, pc, cctx.Int64("ad-depth-limit"), trackUpdateIn)
for update := range updates {
if update.Err != nil {
fmt.Fprintln(os.Stderr, "Provider", update.ID, "distance error:", update.Err)
Expand Down Expand Up @@ -313,7 +319,7 @@ func getLastSeenDistance(cctx *cli.Context, pinfo *model.ProviderInfo) (int, cid
if !pinfo.LastAdvertisement.Defined() {
return 0, cid.Undef, errors.New("no last advertisement")
}
pubClient, err := adpub.NewClient(*pinfo.Publisher)
pubClient, err := adpub.NewClient(*pinfo.Publisher, adpub.WithAdChainDepthLimit(cctx.Int64("ad-depth-limit")))
if err != nil {
return 0, cid.Undef, err
}
Expand Down

0 comments on commit 7ce3756

Please sign in to comment.