Commit 8ce74b81 authored by Jeromy's avatar Jeromy

rewrite FindProvidersAsync

parent 3363963b
...@@ -182,7 +182,6 @@ func (dht *IpfsDHT) handleGetProviders(ctx context.Context, p peer.Peer, pmes *p ...@@ -182,7 +182,6 @@ func (dht *IpfsDHT) handleGetProviders(ctx context.Context, p peer.Peer, pmes *p
providers = append(providers, dht.self) providers = append(providers, dht.self)
} }
// if we've got providers, send thos those.
if providers != nil && len(providers) > 0 { if providers != nil && len(providers) > 0 {
resp.ProviderPeers = pb.PeersToPBPeers(dht.dialer, providers) resp.ProviderPeers = pb.PeersToPBPeers(dht.dialer, providers)
} }
......
...@@ -138,43 +138,78 @@ func (dht *IpfsDHT) Provide(ctx context.Context, key u.Key) error { ...@@ -138,43 +138,78 @@ func (dht *IpfsDHT) Provide(ctx context.Context, key u.Key) error {
func (dht *IpfsDHT) FindProvidersAsync(ctx context.Context, key u.Key, count int) <-chan peer.Peer { func (dht *IpfsDHT) FindProvidersAsync(ctx context.Context, key u.Key, count int) <-chan peer.Peer {
log.Event(ctx, "findProviders", &key) log.Event(ctx, "findProviders", &key)
peerOut := make(chan peer.Peer, count) peerOut := make(chan peer.Peer, count)
go func() { go dht.findProvidersAsyncRoutine(ctx, key, count, peerOut)
defer close(peerOut) return peerOut
}
ps := newPeerSet()
// TODO may want to make this function async to hide latency func (dht *IpfsDHT) findProvidersAsyncRoutine(ctx context.Context, key u.Key, count int, peerOut chan peer.Peer) {
provs := dht.providers.GetProviders(ctx, key) defer close(peerOut)
for _, p := range provs {
count-- ps := newPeerSet()
// NOTE: assuming that this list of peers is unique provs := dht.providers.GetProviders(ctx, key)
ps.Add(p) for _, p := range provs {
count--
// NOTE: assuming that this list of peers is unique
ps.Add(p)
select {
case peerOut <- p:
case <-ctx.Done():
return
}
if count <= 0 {
return
}
}
// setup the Query
query := newQuery(key, dht.dialer, func(ctx context.Context, p peer.Peer) (*dhtQueryResult, error) {
pmes, err := dht.findProvidersSingle(ctx, p, key, 0)
if err != nil {
return nil, err
}
provs, errs := pb.PBPeersToPeers(dht.peerstore, pmes.GetProviderPeers())
for _, err := range errs {
if err != nil {
log.Warning(err)
}
}
// Add unique providers from request, up to 'count'
for _, prov := range provs {
if ps.Contains(prov) {
continue
}
select { select {
case peerOut <- p: case peerOut <- prov:
case <-ctx.Done(): case <-ctx.Done():
return log.Error("Context timed out sending more providers")
return nil, ctx.Err()
} }
if count <= 0 { ps.Add(prov)
return if ps.Size() >= count {
return &dhtQueryResult{success: true}, nil
} }
} }
var wg sync.WaitGroup // Give closer peers back to the query to be queried
peers := dht.routingTables[0].NearestPeers(kb.ConvertKey(key), AlphaValue) closer := pmes.GetCloserPeers()
for _, pp := range peers { clpeers, errs := pb.PBPeersToPeers(dht.peerstore, closer)
wg.Add(1) for _, err := range errs {
go func(p peer.Peer) { if err != nil {
defer wg.Done() log.Warning(err)
pmes, err := dht.findProvidersSingle(ctx, p, key, 0) }
if err != nil {
log.Error(err)
return
}
dht.addPeerListAsync(ctx, key, pmes.GetProviderPeers(), ps, count, peerOut)
}(pp)
} }
wg.Wait()
}() return &dhtQueryResult{closerPeers: clpeers}, nil
return peerOut })
peers := dht.routingTables[0].NearestPeers(kb.ConvertKey(key), AlphaValue)
_, err := query.Run(ctx, peers)
if err != nil {
log.Errorf("FindProviders Query error: %s", err)
}
} }
func (dht *IpfsDHT) addPeerListAsync(ctx context.Context, k u.Key, peers []*pb.Message_Peer, ps *peerSet, count int, out chan peer.Peer) { func (dht *IpfsDHT) addPeerListAsync(ctx context.Context, k u.Key, peers []*pb.Message_Peer, ps *peerSet, count int, out chan peer.Peer) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment