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
41d60060
Commit
41d60060
authored
Dec 09, 2014
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
dht/bootstrap: (optional) parallelism + error on peer
This also makes it an Error to find a peer.
parent
18641a44
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
41 additions
and
6 deletions
+41
-6
dht.go
dht.go
+41
-6
No files found.
dht.go
View file @
41d60060
...
...
@@ -342,6 +342,7 @@ func (dht *IpfsDHT) PingRoutine(t time.Duration) {
// Bootstrap builds up list of peers by requesting random peer IDs
func
(
dht
*
IpfsDHT
)
Bootstrap
(
ctx
context
.
Context
,
queries
int
)
error
{
var
merr
u
.
MultiErr
randomID
:=
func
()
peer
.
ID
{
// 16 random bytes is not a valid peer id. it may be fine becuase
...
...
@@ -352,18 +353,52 @@ func (dht *IpfsDHT) Bootstrap(ctx context.Context, queries int) error {
}
// bootstrap sequentially, as results will compound
for
i
:=
0
;
i
<
queries
;
i
++
{
id
:=
randomID
()
log
.
Debugf
(
"Bootstrapping query (%d/%d) to random ID: %s"
,
i
,
queries
,
id
)
runQuery
:=
func
(
ctx
context
.
Context
,
id
peer
.
ID
)
{
p
,
err
:=
dht
.
FindPeer
(
ctx
,
id
)
if
err
==
routing
.
ErrNotFound
{
// this isn't an error. this is precisely what we expect.
}
else
if
err
!=
nil
{
log
.
Errorf
(
"Bootstr
ap
pe
er error: %s"
,
err
)
merr
=
appe
nd
(
merr
,
err
)
}
else
{
// woah, we got a peer under a random id? it _cannot_ be valid.
log
.
Errorf
(
"dht seemingly found a peer at a random bootstrap id (%s)..."
,
p
)
// woah, actually found a peer with that ID? this shouldn't happen normally
// (as the ID we use is not a real ID). this is an odd error worth logging.
err
:=
fmt
.
Errorf
(
"Bootstrap peer error: Actually FOUND peer. (%s, %s)"
,
id
,
p
)
log
.
Errorf
(
"%s"
,
err
)
merr
=
append
(
merr
,
err
)
}
}
sequential
:=
true
if
sequential
{
// these should be parallel normally. but can make them sequential for debugging.
// note that the core/bootstrap context deadline should be extended too for that.
for
i
:=
0
;
i
<
queries
;
i
++
{
id
:=
randomID
()
log
.
Debugf
(
"Bootstrapping query (%d/%d) to random ID: %s"
,
i
+
1
,
queries
,
id
)
runQuery
(
ctx
,
id
)
}
}
else
{
// note on parallelism here: the context is passed in to the queries, so they
// **should** exit when it exceeds, making this function exit on ctx cancel.
// normally, we should be selecting on ctx.Done() here too, but this gets
// complicated to do with WaitGroup, and doesnt wait for the children to exit.
var
wg
sync
.
WaitGroup
for
i
:=
0
;
i
<
queries
;
i
++
{
wg
.
Add
(
1
)
go
func
()
{
defer
wg
.
Done
()
id
:=
randomID
()
log
.
Debugf
(
"Bootstrapping query (%d/%d) to random ID: %s"
,
i
+
1
,
queries
,
id
)
runQuery
(
ctx
,
id
)
}()
}
wg
.
Wait
()
}
if
len
(
merr
)
>
0
{
return
merr
}
return
nil
}
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