Skip to content

Commit

Permalink
fix: limit providers from each source to half of max (#71)
Browse files Browse the repository at this point in the history
* fix: limit providers from each source to half of max. This ensures we get from both the DHT and IPNI even if one is faster.
* fix: handle case where maxProvidersCount is 1. Ensures that we provide at least one result for each source.
  • Loading branch information
2color authored Oct 3, 2024
1 parent b35461c commit a727540
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,16 @@ func (d *daemon) runCidCheck(ctx context.Context, cidKey cid.Cid, ipniURL string
queryCtx, cancelQuery := context.WithCancel(ctx)
defer cancelQuery()

// Find providers with DHT and IPNI concurrently.
provsCh := d.dht.FindProvidersAsync(queryCtx, cidKey, maxProvidersCount)
ipniProvsCh := routerClient.FindProvidersAsync(queryCtx, cidKey, maxProvidersCount)
// half of the max providers count per source
providersPerSource := maxProvidersCount >> 1
if maxProvidersCount == 1 {
// Ensure at least one provider from each source when maxProvidersCount is 1
providersPerSource = 1
}

// Find providers with DHT and IPNI concurrently (each half of the max providers count)
dhtProvsCh := d.dht.FindProvidersAsync(queryCtx, cidKey, providersPerSource)
ipniProvsCh := routerClient.FindProvidersAsync(queryCtx, cidKey, providersPerSource)

out := make([]providerOutput, 0, maxProvidersCount)
var wg sync.WaitGroup
Expand All @@ -174,9 +181,9 @@ func (d *daemon) runCidCheck(ctx context.Context, cidKey cid.Cid, ipniURL string
var source string

select {
case provider, open = <-provsCh:
case provider, open = <-dhtProvsCh:
if !open {
provsCh = nil
dhtProvsCh = nil
if ipniProvsCh == nil {
done = true
}
Expand All @@ -186,7 +193,7 @@ func (d *daemon) runCidCheck(ctx context.Context, cidKey cid.Cid, ipniURL string
case provider, open = <-ipniProvsCh:
if !open {
ipniProvsCh = nil
if provsCh == nil {
if dhtProvsCh == nil {
done = true
}
continue
Expand Down

0 comments on commit a727540

Please sign in to comment.