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
d72432ca
Commit
d72432ca
authored
Aug 03, 2018
by
Łukasz Magiera
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
SearchValue: address review
parent
51c475d1
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
9 deletions
+23
-9
ext_test.go
ext_test.go
+2
-1
routing.go
routing.go
+21
-8
No files found.
ext_test.go
View file @
d72432ca
...
...
@@ -36,6 +36,7 @@ func TestGetFailures(t *testing.T) {
// Reply with failures to every message
hosts
[
1
]
.
SetStreamHandler
(
d
.
protocols
[
0
],
func
(
s
inet
.
Stream
)
{
time
.
Sleep
(
400
*
time
.
Millisecond
)
s
.
Close
()
})
...
...
@@ -47,7 +48,7 @@ func TestGetFailures(t *testing.T) {
err
=
merr
[
0
]
}
if
err
!=
routing
.
ErrNotFoun
d
{
if
err
!=
context
.
DeadlineExceede
d
{
t
.
Fatal
(
"Got different error than we expected"
,
err
)
}
}
else
{
...
...
routing.go
View file @
d72432ca
...
...
@@ -137,6 +137,10 @@ func (dht *IpfsDHT) GetValue(ctx context.Context, key string, opts ...ropts.Opti
best
=
r
}
if
ctx
.
Err
()
!=
nil
{
return
best
,
ctx
.
Err
()
}
if
best
==
nil
{
return
nil
,
routing
.
ErrNotFound
}
...
...
@@ -164,10 +168,12 @@ func (dht *IpfsDHT) SearchValue(ctx context.Context, key string, opts ...ropts.O
go
func
()
{
defer
close
(
out
)
if
responsesNeeded
<
0
{
responsesNeeded
=
0
maxVals
:=
responsesNeeded
if
maxVals
<
0
{
maxVals
=
defaultQuorum
*
4
// we want some upper bound on how
// much correctional entries we will send
}
vals
:=
make
([]
RecvdVal
,
0
,
responsesNeeded
)
vals
:=
make
([]
RecvdVal
,
0
,
maxVals
)
best
:=
-
1
defer
func
()
{
...
...
@@ -204,18 +210,25 @@ func (dht *IpfsDHT) SearchValue(ctx context.Context, key string, opts ...ropts.O
return
}
vals
=
append
(
vals
,
v
)
i
:=
len
(
vals
)
if
len
(
vals
)
<
maxVals
{
vals
=
append
(
vals
,
v
)
}
else
{
i
=
(
best
+
1
)
%
maxVals
vals
[
i
]
=
v
}
if
v
.
Val
==
nil
{
continue
}
// Select best value
if
best
>
-
1
{
i
,
err
:=
dht
.
Validator
.
Select
(
key
,
[][]
byte
{
vals
[
best
]
.
Val
,
v
.
Val
})
sel
,
err
:=
dht
.
Validator
.
Select
(
key
,
[][]
byte
{
vals
[
best
]
.
Val
,
v
.
Val
})
if
err
!=
nil
{
continue
//TODO: Do we want to do something with the error here?
log
.
Warning
(
"Failed to select dht key: "
,
err
)
continue
}
if
i
!=
best
&&
!
bytes
.
Equal
(
v
.
Val
,
vals
[
best
]
.
Val
)
{
if
sel
==
1
&&
!
bytes
.
Equal
(
v
.
Val
,
vals
[
best
]
.
Val
)
{
best
=
i
out
<-
v
.
Val
}
...
...
@@ -247,7 +260,7 @@ func (dht *IpfsDHT) GetValues(ctx context.Context, key string, nvals int) (_ []R
out
=
append
(
out
,
val
)
}
return
out
,
nil
return
out
,
ctx
.
Err
()
}
func
(
dht
*
IpfsDHT
)
getValues
(
ctx
context
.
Context
,
key
string
,
nvals
int
)
(
<-
chan
RecvdVal
,
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