routing.go 13.6 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3
package dht

import (
4
	"bytes"
Jeromy's avatar
Jeromy committed
5
	"context"
6
	"fmt"
7
	"runtime"
Jeromy's avatar
Jeromy committed
8
	"sync"
Jeromy's avatar
Jeromy committed
9
	"time"
10

11
	cid "github.com/ipfs/go-cid"
George Antoniadis's avatar
George Antoniadis committed
12 13
	pb "github.com/libp2p/go-libp2p-kad-dht/pb"
	kb "github.com/libp2p/go-libp2p-kbucket"
14 15 16 17
	inet "github.com/libp2p/go-libp2p-net"
	peer "github.com/libp2p/go-libp2p-peer"
	pset "github.com/libp2p/go-libp2p-peer/peerset"
	pstore "github.com/libp2p/go-libp2p-peerstore"
George Antoniadis's avatar
George Antoniadis committed
18 19 20
	record "github.com/libp2p/go-libp2p-record"
	routing "github.com/libp2p/go-libp2p-routing"
	notif "github.com/libp2p/go-libp2p-routing/notifications"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
21 22
)

23 24 25 26 27 28
// asyncQueryBuffer is the size of buffered channels in async queries. This
// buffer allows multiple queries to execute simultaneously, return their
// results and continue querying closer peers. Note that different query
// results will wait for the channel to drain.
var asyncQueryBuffer = 10

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
29 30 31 32 33
// This file implements the Routing interface for the IpfsDHT struct.

// Basic Put/Get

// PutValue adds value corresponding to given Key.
34
// This is the top level "Store" operation of the DHT
35
func (dht *IpfsDHT) PutValue(ctx context.Context, key string, value []byte) error {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
36
	log.Debugf("PutValue %s", key)
Jeromy's avatar
Jeromy committed
37
	sk, err := dht.getOwnPrivateKey()
Jeromy's avatar
Jeromy committed
38 39 40
	if err != nil {
		return err
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
41

42 43 44 45 46
	sign, err := dht.Validator.IsSigned(key)
	if err != nil {
		return err
	}

Jeromy's avatar
Jeromy committed
47
	rec, err := record.MakePutRecord(sk, key, value, sign)
Jeromy's avatar
Jeromy committed
48
	if err != nil {
49
		log.Debug("creation of record failed!")
Jeromy's avatar
Jeromy committed
50 51 52
		return err
	}

Jeromy's avatar
Jeromy committed
53
	err = dht.putLocal(key, rec)
54 55 56 57
	if err != nil {
		return err
	}

58
	pchan, err := dht.GetClosestPeers(ctx, key)
59 60 61
	if err != nil {
		return err
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
62

63 64 65 66
	wg := sync.WaitGroup{}
	for p := range pchan {
		wg.Add(1)
		go func(p peer.ID) {
Jeromy's avatar
Jeromy committed
67 68
			ctx, cancel := context.WithCancel(ctx)
			defer cancel()
69
			defer wg.Done()
Jeromy's avatar
Jeromy committed
70 71 72 73 74
			notif.PublishQueryEvent(ctx, &notif.QueryEvent{
				Type: notif.Value,
				ID:   p,
			})

Jeromy's avatar
Jeromy committed
75
			err := dht.putValueToPeer(ctx, p, key, rec)
76
			if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
77
				log.Debugf("failed putting value to peer: %s", err)
78 79 80 81 82
			}
		}(p)
	}
	wg.Wait()
	return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
83 84 85
}

// GetValue searches for the value corresponding to given Key.
86
func (dht *IpfsDHT) GetValue(ctx context.Context, key string) ([]byte, error) {
Jeromy's avatar
Jeromy committed
87 88 89 90
	ctx, cancel := context.WithTimeout(ctx, time.Minute)
	defer cancel()

	vals, err := dht.GetValues(ctx, key, 16)
91 92 93 94 95 96
	if err != nil {
		return nil, err
	}

	var recs [][]byte
	for _, v := range vals {
97 98 99
		if v.Val != nil {
			recs = append(recs, v.Val)
		}
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	}

	i, err := dht.Selector.BestRecord(key, recs)
	if err != nil {
		return nil, err
	}

	best := recs[i]
	log.Debugf("GetValue %v %v", key, best)
	if best == nil {
		log.Errorf("GetValue yielded correct record with nil value.")
		return nil, routing.ErrNotFound
	}

	fixupRec, err := record.MakePutRecord(dht.peerstore.PrivKey(dht.self), key, best, true)
	if err != nil {
		// probably shouldnt actually 'error' here as we have found a value we like,
		// but this call failing probably isnt something we want to ignore
		return nil, err
	}

	for _, v := range vals {
		// if someone sent us a different 'less-valid' record, lets correct them
		if !bytes.Equal(v.Val, best) {
			go func(v routing.RecvdVal) {
125 126 127 128 129 130 131
				if v.From == dht.self {
					err := dht.putLocal(key, fixupRec)
					if err != nil {
						log.Error("Error correcting local dht entry:", err)
					}
					return
				}
Jeromy's avatar
Jeromy committed
132 133
				ctx, cancel := context.WithTimeout(dht.Context(), time.Second*30)
				defer cancel()
134 135 136 137 138 139 140 141 142 143 144
				err := dht.putValueToPeer(ctx, v.From, key, fixupRec)
				if err != nil {
					log.Error("Error correcting DHT entry: ", err)
				}
			}(v)
		}
	}

	return best, nil
}

145
func (dht *IpfsDHT) GetValues(ctx context.Context, key string, nvals int) ([]routing.RecvdVal, error) {
146 147 148
	var vals []routing.RecvdVal
	var valslock sync.Mutex

Jeromy's avatar
Jeromy committed
149
	// If we have it local, dont bother doing an RPC!
150
	lrec, err := dht.getLocal(key)
Jeromy's avatar
Jeromy committed
151
	if err == nil {
152 153
		// TODO: this is tricky, we dont always want to trust our own value
		// what if the authoritative source updated it?
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
154
		log.Debug("have it locally")
155 156 157 158 159 160 161 162 163 164
		vals = append(vals, routing.RecvdVal{
			Val:  lrec.GetValue(),
			From: dht.self,
		})

		if nvals <= 1 {
			return vals, nil
		}
	} else if nvals == 0 {
		return nil, err
Jeromy's avatar
Jeromy committed
165 166
	}

167
	// get closest peers in the routing table
Jeromy's avatar
Jeromy committed
168
	rtp := dht.routingTable.NearestPeers(kb.ConvertKey(key), AlphaValue)
169
	log.Debugf("peers in rt: %d %s", len(rtp), rtp)
170
	if len(rtp) == 0 {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
171
		log.Warning("No peers from routing table!")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
172
		return nil, kb.ErrLookupFailure
173 174
	}

175
	// setup the Query
Jeromy's avatar
Jeromy committed
176
	parent := ctx
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
177
	query := dht.newQuery(key, func(ctx context.Context, p peer.ID) (*dhtQueryResult, error) {
Jeromy's avatar
Jeromy committed
178
		notif.PublishQueryEvent(parent, &notif.QueryEvent{
Jeromy's avatar
Jeromy committed
179 180 181 182
			Type: notif.SendingQuery,
			ID:   p,
		})

183
		rec, peers, err := dht.getValueOrPeers(ctx, p, key)
184 185 186 187 188 189 190 191 192
		switch err {
		case routing.ErrNotFound:
			// in this case, they responded with nothing,
			// still send a notification so listeners can know the
			// request has completed 'successfully'
			notif.PublishQueryEvent(parent, &notif.QueryEvent{
				Type: notif.PeerResponse,
				ID:   p,
			})
193
			return nil, err
194 195 196 197 198
		default:
			return nil, err

		case nil, errInvalidRecord:
			// in either of these cases, we want to keep going
199
		}
200

201 202
		res := &dhtQueryResult{closerPeers: peers}

203
		if rec.GetValue() != nil || err == errInvalidRecord {
204 205 206 207 208 209 210 211 212 213 214 215
			rv := routing.RecvdVal{
				Val:  rec.GetValue(),
				From: p,
			}
			valslock.Lock()
			vals = append(vals, rv)

			// If weve collected enough records, we're done
			if len(vals) >= nvals {
				res.success = true
			}
			valslock.Unlock()
216 217
		}

Jeromy's avatar
Jeromy committed
218
		notif.PublishQueryEvent(parent, &notif.QueryEvent{
Jeromy's avatar
Jeromy committed
219 220
			Type:      notif.PeerResponse,
			ID:        p,
Jeromy's avatar
Jeromy committed
221
			Responses: peers,
Jeromy's avatar
Jeromy committed
222 223
		})

224 225
		return res, nil
	})
226

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
227
	// run it!
228 229 230 231 232
	_, err = query.Run(ctx, rtp)
	if len(vals) == 0 {
		if err != nil {
			return nil, err
		}
233 234
	}

235
	return vals, nil
236

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
237 238 239 240 241
}

// Value provider layer of indirection.
// This is what DSHTs (Coral and MainlineDHT) do to store large values in a DHT.

242
// Provide makes this node announce that it can provide a value for the given key
243 244
func (dht *IpfsDHT) Provide(ctx context.Context, key *cid.Cid) error {
	defer log.EventBegin(ctx, "provide", key).Done()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
245 246

	// add self locally
247
	dht.providers.AddProvider(ctx, key, dht.self)
248

249
	peers, err := dht.GetClosestPeers(ctx, key.KeyString())
250 251
	if err != nil {
		return err
252 253
	}

254 255 256 257 258
	mes, err := dht.makeProvRecord(key)
	if err != nil {
		return err
	}

Jeromy's avatar
Jeromy committed
259
	wg := sync.WaitGroup{}
260
	for p := range peers {
Jeromy's avatar
Jeromy committed
261 262 263
		wg.Add(1)
		go func(p peer.ID) {
			defer wg.Done()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
264
			log.Debugf("putProvider(%s, %s)", key, p)
265
			err := dht.sendMessage(ctx, p, mes)
Jeromy's avatar
Jeromy committed
266
			if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
267
				log.Debug(err)
Jeromy's avatar
Jeromy committed
268 269
			}
		}(p)
270
	}
Jeromy's avatar
Jeromy committed
271
	wg.Wait()
272
	return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
273
}
274
func (dht *IpfsDHT) makeProvRecord(skey *cid.Cid) (*pb.Message, error) {
275 276 277 278 279 280 281 282 283 284 285
	pi := pstore.PeerInfo{
		ID:    dht.self,
		Addrs: dht.host.Addrs(),
	}

	// // only share WAN-friendly addresses ??
	// pi.Addrs = addrutil.WANShareableAddrs(pi.Addrs)
	if len(pi.Addrs) < 1 {
		return nil, fmt.Errorf("no known addresses for self. cannot put provider.")
	}

286
	pmes := pb.NewMessage(pb.Message_ADD_PROVIDER, skey.KeyString(), 0)
287 288 289
	pmes.ProviderPeers = pb.RawPeerInfosToPBPeers([]pstore.PeerInfo{pi})
	return pmes, nil
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
290

Brian Tiger Chow's avatar
Brian Tiger Chow committed
291
// FindProviders searches until the context expires.
292
func (dht *IpfsDHT) FindProviders(ctx context.Context, c *cid.Cid) ([]pstore.PeerInfo, error) {
Jeromy's avatar
Jeromy committed
293
	var providers []pstore.PeerInfo
294
	for p := range dht.FindProvidersAsync(ctx, c, KValue) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
295 296 297 298 299
		providers = append(providers, p)
	}
	return providers, nil
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
300 301 302
// FindProvidersAsync is the same thing as FindProviders, but returns a channel.
// Peers will be returned on the channel as soon as they are found, even before
// the search query completes.
303 304
func (dht *IpfsDHT) FindProvidersAsync(ctx context.Context, key *cid.Cid, count int) <-chan pstore.PeerInfo {
	log.Event(ctx, "findProviders", key)
Jeromy's avatar
Jeromy committed
305
	peerOut := make(chan pstore.PeerInfo, count)
Jeromy's avatar
Jeromy committed
306 307 308 309
	go dht.findProvidersAsyncRoutine(ctx, key, count, peerOut)
	return peerOut
}

310 311
func (dht *IpfsDHT) findProvidersAsyncRoutine(ctx context.Context, key *cid.Cid, count int, peerOut chan pstore.PeerInfo) {
	defer log.EventBegin(ctx, "findProvidersAsync", key).Done()
Jeromy's avatar
Jeromy committed
312 313
	defer close(peerOut)

Jeromy's avatar
Jeromy committed
314
	ps := pset.NewLimited(count)
Jeromy's avatar
Jeromy committed
315 316
	provs := dht.providers.GetProviders(ctx, key)
	for _, p := range provs {
317
		// NOTE: Assuming that this list of peers is unique
Jeromy's avatar
Jeromy committed
318
		if ps.TryAdd(p) {
Jeromy's avatar
Jeromy committed
319
			pi := dht.peerstore.PeerInfo(p)
Jeromy's avatar
Jeromy committed
320
			select {
Jeromy's avatar
Jeromy committed
321
			case peerOut <- pi:
Jeromy's avatar
Jeromy committed
322 323 324
			case <-ctx.Done():
				return
			}
Jeromy's avatar
Jeromy committed
325
		}
Jeromy's avatar
Jeromy committed
326 327

		// If we have enough peers locally, dont bother with remote RPC
Jeromy's avatar
Jeromy committed
328
		// TODO: is this a DOS vector?
Jeromy's avatar
Jeromy committed
329
		if ps.Size() >= count {
Jeromy's avatar
Jeromy committed
330 331 332 333 334
			return
		}
	}

	// setup the Query
Jeromy's avatar
Jeromy committed
335
	parent := ctx
336
	query := dht.newQuery(key.KeyString(), func(ctx context.Context, p peer.ID) (*dhtQueryResult, error) {
Jeromy's avatar
Jeromy committed
337
		notif.PublishQueryEvent(parent, &notif.QueryEvent{
338 339 340
			Type: notif.SendingQuery,
			ID:   p,
		})
341
		pmes, err := dht.findProvidersSingle(ctx, p, key)
Jeromy's avatar
Jeromy committed
342 343 344 345
		if err != nil {
			return nil, err
		}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
346
		log.Debugf("%d provider entries", len(pmes.GetProviderPeers()))
347
		provs := pb.PBPeersToPeerInfos(pmes.GetProviderPeers())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
348
		log.Debugf("%d provider entries decoded", len(provs))
Jeromy's avatar
Jeromy committed
349 350 351

		// Add unique providers from request, up to 'count'
		for _, prov := range provs {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
352
			log.Debugf("got provider: %s", prov)
353
			if ps.TryAdd(prov.ID) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
354
				log.Debugf("using provider: %s", prov)
Jeromy's avatar
Jeromy committed
355
				select {
Jeromy's avatar
Jeromy committed
356
				case peerOut <- *prov:
Jeromy's avatar
Jeromy committed
357
				case <-ctx.Done():
358
					log.Debug("context timed out sending more providers")
Jeromy's avatar
Jeromy committed
359 360
					return nil, ctx.Err()
				}
361
			}
Jeromy's avatar
Jeromy committed
362
			if ps.Size() >= count {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
363
				log.Debugf("got enough providers (%d/%d)", ps.Size(), count)
Jeromy's avatar
Jeromy committed
364
				return &dhtQueryResult{success: true}, nil
365 366 367
			}
		}

Jeromy's avatar
Jeromy committed
368 369
		// Give closer peers back to the query to be queried
		closer := pmes.GetCloserPeers()
370
		clpeers := pb.PBPeersToPeerInfos(closer)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
371
		log.Debugf("got closer peers: %d %s", len(clpeers), clpeers)
372

Jeromy's avatar
Jeromy committed
373
		notif.PublishQueryEvent(parent, &notif.QueryEvent{
374 375
			Type:      notif.PeerResponse,
			ID:        p,
Jeromy's avatar
Jeromy committed
376
			Responses: clpeers,
377
		})
Jeromy's avatar
Jeromy committed
378 379 380
		return &dhtQueryResult{closerPeers: clpeers}, nil
	})

Jeromy's avatar
Jeromy committed
381
	peers := dht.routingTable.NearestPeers(kb.ConvertKey(key.KeyString()), AlphaValue)
Jeromy's avatar
Jeromy committed
382 383
	_, err := query.Run(ctx, peers)
	if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
384
		log.Debugf("Query error: %s", err)
385 386 387 388 389 390 391 392 393 394
		// Special handling for issue: https://github.com/ipfs/go-ipfs/issues/3032
		if fmt.Sprint(err) == "<nil>" {
			log.Error("reproduced bug 3032:")
			log.Errorf("Errors type information: %#v", err)
			log.Errorf("go version: %s", runtime.Version())
			log.Error("please report this information to: https://github.com/ipfs/go-ipfs/issues/3032")

			// replace problematic error with something that won't crash the daemon
			err = fmt.Errorf("<nil>")
		}
395 396 397 398
		notif.PublishQueryEvent(ctx, &notif.QueryEvent{
			Type:  notif.QueryError,
			Extra: err.Error(),
		})
Jeromy's avatar
Jeromy committed
399
	}
400 401
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
402
// FindPeer searches for a peer with given ID.
Jeromy's avatar
Jeromy committed
403
func (dht *IpfsDHT) FindPeer(ctx context.Context, id peer.ID) (pstore.PeerInfo, error) {
Jeromy's avatar
Jeromy committed
404
	defer log.EventBegin(ctx, "FindPeer", id).Done()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
405

406
	// Check if were already connected to them
Jeromy's avatar
Jeromy committed
407
	if pi := dht.FindLocal(id); pi.ID != "" {
408
		return pi, nil
409 410
	}

Jeromy's avatar
Jeromy committed
411
	peers := dht.routingTable.NearestPeers(kb.ConvertPeerID(id), AlphaValue)
412
	if len(peers) == 0 {
Jeromy's avatar
Jeromy committed
413
		return pstore.PeerInfo{}, kb.ErrLookupFailure
414
	}
415

Jeromy's avatar
Jeromy committed
416
	// Sanity...
417
	for _, p := range peers {
418
		if p == id {
419
			log.Debug("found target peer in list of closest peers...")
420
			return dht.peerstore.PeerInfo(p), nil
421
		}
422
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
423

Jeromy's avatar
Jeromy committed
424
	// setup the Query
Jeromy's avatar
Jeromy committed
425
	parent := ctx
426
	query := dht.newQuery(string(id), func(ctx context.Context, p peer.ID) (*dhtQueryResult, error) {
Jeromy's avatar
Jeromy committed
427
		notif.PublishQueryEvent(parent, &notif.QueryEvent{
428 429 430
			Type: notif.SendingQuery,
			ID:   p,
		})
Jeromy's avatar
Jeromy committed
431

432
		pmes, err := dht.findPeerSingle(ctx, p, id)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
433
		if err != nil {
434
			return nil, err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
435
		}
436

Jeromy's avatar
Jeromy committed
437
		closer := pmes.GetCloserPeers()
438
		clpeerInfos := pb.PBPeersToPeerInfos(closer)
439

440
		// see it we got the peer here
441 442
		for _, npi := range clpeerInfos {
			if npi.ID == id {
Jeromy's avatar
Jeromy committed
443
				return &dhtQueryResult{
444
					peer:    npi,
Jeromy's avatar
Jeromy committed
445 446 447
					success: true,
				}, nil
			}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
448 449
		}

Jeromy's avatar
Jeromy committed
450
		notif.PublishQueryEvent(parent, &notif.QueryEvent{
451
			Type:      notif.PeerResponse,
Jeromy's avatar
Jeromy committed
452
			Responses: clpeerInfos,
453 454
		})

455
		return &dhtQueryResult{closerPeers: clpeerInfos}, nil
456
	})
457

Jeromy's avatar
Jeromy committed
458
	// run it!
459
	result, err := query.Run(ctx, peers)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
460
	if err != nil {
Jeromy's avatar
Jeromy committed
461
		return pstore.PeerInfo{}, err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
462 463
	}

464
	log.Debugf("FindPeer %v %v", id, result.success)
465
	if result.peer.ID == "" {
Jeromy's avatar
Jeromy committed
466
		return pstore.PeerInfo{}, routing.ErrNotFound
467
	}
Jeromy's avatar
Jeromy committed
468

Jeromy's avatar
Jeromy committed
469
	return *result.peer, nil
470 471
}

472
// FindPeersConnectedToPeer searches for peers directly connected to a given peer.
Jeromy's avatar
Jeromy committed
473
func (dht *IpfsDHT) FindPeersConnectedToPeer(ctx context.Context, id peer.ID) (<-chan *pstore.PeerInfo, error) {
474

Jeromy's avatar
Jeromy committed
475
	peerchan := make(chan *pstore.PeerInfo, asyncQueryBuffer)
Jeromy's avatar
Jeromy committed
476
	peersSeen := make(map[peer.ID]struct{})
477

Jeromy's avatar
Jeromy committed
478
	peers := dht.routingTable.NearestPeers(kb.ConvertPeerID(id), AlphaValue)
479
	if len(peers) == 0 {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
480
		return nil, kb.ErrLookupFailure
481 482 483
	}

	// setup the Query
484
	query := dht.newQuery(string(id), func(ctx context.Context, p peer.ID) (*dhtQueryResult, error) {
485

486
		pmes, err := dht.findPeerSingle(ctx, p, id)
487 488 489 490
		if err != nil {
			return nil, err
		}

Jeromy's avatar
Jeromy committed
491
		var clpeers []*pstore.PeerInfo
492 493
		closer := pmes.GetCloserPeers()
		for _, pbp := range closer {
494
			pi := pb.PBPeerToPeerInfo(pbp)
495

496 497
			// skip peers already seen
			if _, found := peersSeen[pi.ID]; found {
498 499
				continue
			}
500
			peersSeen[pi.ID] = struct{}{}
501 502 503 504 505 506

			// if peer is connected, send it to our client.
			if pb.Connectedness(*pbp.Connection) == inet.Connected {
				select {
				case <-ctx.Done():
					return nil, ctx.Err()
507
				case peerchan <- pi:
508 509 510 511
				}
			}

			// if peer is the peer we're looking for, don't bother querying it.
512
			// TODO maybe query it?
513
			if pb.Connectedness(*pbp.Connection) != inet.Connected {
514
				clpeers = append(clpeers, pi)
515 516 517 518 519 520 521 522 523
			}
		}

		return &dhtQueryResult{closerPeers: clpeers}, nil
	})

	// run it! run it asynchronously to gen peers as results are found.
	// this does no error checking
	go func() {
524
		if _, err := query.Run(ctx, peers); err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
525
			log.Debug(err)
526 527 528 529 530 531 532 533
		}

		// close the peerchan channel when done.
		close(peerchan)
	}()

	return peerchan, nil
}