Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-p2p-kad-dht
Commits
2ec86a46
Commit
2ec86a46
authored
Dec 29, 2014
by
Jeromy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use query for getClosestPeers
parent
0408e60e
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
36 deletions
+32
-36
routing.go
routing.go
+32
-36
No files found.
routing.go
View file @
2ec86a46
...
...
@@ -159,50 +159,46 @@ func (dht *IpfsDHT) getClosestPeers(ctx context.Context, key u.Key, count int) (
peerset
:=
pset
.
NewLimited
(
count
)
for
_
,
p
:=
range
tablepeers
{
out <- p
peerset.Add(p)
select
{
case
out
<-
p
:
case
<-
ctx
.
Done
()
:
return
nil
,
ctx
.
Err
()
}
wg := sync.WaitGroup{}
for _, p := range tablepeers {
wg.Add(1)
go func(p peer.ID) {
dht.getClosestPeersRecurse(ctx, key, p, peerset, out)
wg.Done()
}(p)
peerset
.
Add
(
p
)
}
go func() {
wg.Wait()
close(out)
}()
return out, nil
}
func (dht *IpfsDHT) getClosestPeersRecurse(ctx context.Context, key u.Key, p peer.ID, peers *pset.PeerSet, peerOut chan<- peer.ID) {
query
:=
newQuery
(
key
,
dht
.
network
,
func
(
ctx
context
.
Context
,
p
peer
.
ID
)
(
*
dhtQueryResult
,
error
)
{
closer
,
err
:=
dht
.
closerPeersSingle
(
ctx
,
key
,
p
)
if
err
!=
nil
{
log
.
Errorf
(
"error getting closer peers: %s"
,
err
)
return
return
nil
,
err
}
wg := sync.WaitGroup{}
var
filtered
[]
peer
.
PeerInfo
for
_
,
p
:=
range
closer
{
if kb.Closer(p, dht.self, key) && peers.TryAdd(p) {
if
kb
.
Closer
(
p
,
dht
.
self
,
key
)
&&
peers
et
.
TryAdd
(
p
)
{
select
{
case
peerO
ut <- p:
case
o
ut
<-
p
:
case
<-
ctx
.
Done
()
:
return
return
nil
,
ctx
.
Err
()
}
wg.Add(1)
go func(p peer.ID) {
dht.getClosestPeersRecurse(ctx, key, p, peers, peerOut)
wg.Done()
}(p)
filtered
=
append
(
filtered
,
dht
.
peerstore
.
PeerInfo
(
p
))
}
}
wg.Wait()
return
&
dhtQueryResult
{
closerPeers
:
filtered
},
nil
})
go
func
()
{
defer
close
(
out
)
// run it!
_
,
err
:=
query
.
Run
(
ctx
,
tablepeers
)
if
err
!=
nil
{
log
.
Errorf
(
"closestPeers query run error: %s"
,
err
)
}
}()
return
out
,
nil
}
func
(
dht
*
IpfsDHT
)
closerPeersSingle
(
ctx
context
.
Context
,
key
u
.
Key
,
p
peer
.
ID
)
([]
peer
.
ID
,
error
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment