changes from PR
Jeromy committed
Dec 11, 2014 at 05:42 UTC
9e14bcfe2db8f7d4d6322c67b727e75e53d88fc2
1 file changed
+16
-16
routing/dht/routing.go
+16
-16
@@ -148,15 +148,17 @@ func (dht *IpfsDHT) findProvidersAsyncRoutine(ctx context.Context, key u.Key, co
148
ps := newPeerSet()
149
provs := dht.providers.GetProviders(ctx, key)
150
for _, p := range provs {
151
- count--
151
// NOTE: assuming that this list of peers is unique
153
- ps.Add(p)
154
- select {
155
- case peerOut <- p:
156
- case <-ctx.Done():
157
- return
152
+ if ps.AddIfSmallerThan(p, count) {
153
+ select {
154
+ case peerOut <- p:
155
+ case <-ctx.Done():
156
+ return
157
+ }
158
}
159
- if count <= 0 {
159
+
160
+ // If we have enough peers locally, dont bother with remote RPC
161
+ if ps.Size() >= count {
162
return
163
}
164
}
@@ -178,16 +180,14 @@ func (dht *IpfsDHT) findProvidersAsyncRoutine(ctx context.Context, key u.Key, co
180
181
// Add unique providers from request, up to 'count'
182
for _, prov := range provs {
181
- if ps.Contains(prov) {
182
- continue
183
- }
184
- select {
185
- case peerOut <- prov:
186
- case <-ctx.Done():
187
- log.Error("Context timed out sending more providers")
188
- return nil, ctx.Err()
183
+ if ps.AddIfSmallerThan(prov, count) {
184
+ select {
185
+ case peerOut <- prov:
186
+ case <-ctx.Done():
187
+ log.Error("Context timed out sending more providers")
188
+ return nil, ctx.Err()
189
+ }
190
}
190
- ps.Add(prov)
191
if ps.Size() >= count {
192
return &dhtQueryResult{success: true}, nil
193
}