Skip to content

Commit 01cf92e

Browse files
authored
Merge pull request #603 from libp2p/bug/queryeventmerge
intercept failing query events when finding providers
2 parents 2d84306 + eeb9767 commit 01cf92e

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

dual/dual.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,25 @@ func (dht *DHT) Provide(ctx context.Context, key cid.Cid, announce bool) error {
9797
func (dht *DHT) FindProvidersAsync(ctx context.Context, key cid.Cid, count int) <-chan peer.AddrInfo {
9898
reqCtx, cancel := context.WithCancel(ctx)
9999
outCh := make(chan peer.AddrInfo)
100-
wanCh := dht.WAN.FindProvidersAsync(reqCtx, key, count)
101-
lanCh := dht.LAN.FindProvidersAsync(reqCtx, key, count)
100+
subCtx, errCh := routing.RegisterForQueryEvents(reqCtx)
101+
wanCh := dht.WAN.FindProvidersAsync(subCtx, key, count)
102+
lanCh := dht.LAN.FindProvidersAsync(subCtx, key, count)
102103
zeroCount := (count == 0)
103104
go func() {
104105
defer cancel()
105106
defer close(outCh)
106107

107108
found := make(map[peer.ID]struct{}, count)
108109
var pi peer.AddrInfo
110+
var qEv *routing.QueryEvent
109111
for (zeroCount || count > 0) && (wanCh != nil || lanCh != nil) {
110112
var ok bool
111113
select {
114+
case qEv, ok = <-errCh:
115+
if ok && qEv != nil && qEv.Type != routing.QueryError {
116+
routing.PublishQueryEvent(reqCtx, qEv)
117+
}
118+
continue
112119
case pi, ok = <-wanCh:
113120
if !ok {
114121
wanCh = nil
@@ -133,6 +140,9 @@ func (dht *DHT) FindProvidersAsync(ctx context.Context, key cid.Cid, count int)
133140
return
134141
}
135142
}
143+
if qEv != nil && qEv.Type == routing.QueryError && len(found) == 0 {
144+
routing.PublishQueryEvent(reqCtx, qEv)
145+
}
136146
}()
137147
return outCh
138148
}

0 commit comments

Comments
 (0)