Skip to content

Commit

Permalink
intercept failing query events when finding providers
Browse files Browse the repository at this point in the history
  • Loading branch information
willscott committed Apr 22, 2020
1 parent 278c3ea commit a3542d9
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions dual/dual.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,25 @@ func (dht *DHT) Provide(ctx context.Context, key cid.Cid, announce bool) error {
func (dht *DHT) FindProvidersAsync(ctx context.Context, key cid.Cid, count int) <-chan peer.AddrInfo {
reqCtx, cancel := context.WithCancel(ctx)
outCh := make(chan peer.AddrInfo)
wanCh := dht.WAN.FindProvidersAsync(reqCtx, key, count)
lanCh := dht.LAN.FindProvidersAsync(reqCtx, key, count)
subCtx, errCh := routing.RegisterForQueryEvents(reqCtx)
wanCh := dht.WAN.FindProvidersAsync(subCtx, key, count)
lanCh := dht.LAN.FindProvidersAsync(subCtx, key, count)
zeroCount := (count == 0)
go func() {
defer cancel()
defer close(outCh)

found := make(map[peer.ID]struct{}, count)
var pi peer.AddrInfo
var qEv *routing.QueryEvent
for (zeroCount || count > 0) && (wanCh != nil || lanCh != nil) {
var ok bool
select {
case qEv, ok = <-errCh:
if ok && qEv != nil && qEv.Type != routing.QueryError {
routing.PublishQueryEvent(reqCtx, qEv)
}
continue
case pi, ok = <-wanCh:
if !ok {
wanCh = nil
Expand All @@ -133,6 +140,9 @@ func (dht *DHT) FindProvidersAsync(ctx context.Context, key cid.Cid, count int)
return
}
}
if qEv.Type == routing.QueryError && len(found) == 0 {
routing.PublishQueryEvent(reqCtx, qEv)
}
}()
return outCh
}
Expand Down

0 comments on commit a3542d9

Please sign in to comment.