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
dms3
go-dms3-routing
Commits
ec5d9c78
Commit
ec5d9c78
authored
Dec 29, 2014
by
Jeromy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve readability of getClosestPeers method.
Also remove older useless code.
parent
a55bf913
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
9 additions
and
11 deletions
+9
-11
dht/routing.go
dht/routing.go
+9
-11
No files found.
dht/routing.go
View file @
ec5d9c78
...
...
@@ -40,7 +40,7 @@ func (dht *IpfsDHT) PutValue(ctx context.Context, key u.Key, value []byte) error
return
err
}
pchan
,
err
:=
dht
.
getClosestPeers
(
ctx
,
key
,
KValue
)
pchan
,
err
:=
dht
.
getClosestPeers
(
ctx
,
key
)
if
err
!=
nil
{
return
err
}
...
...
@@ -116,11 +116,11 @@ func (dht *IpfsDHT) GetValue(ctx context.Context, key u.Key) ([]byte, error) {
// Provide makes this node announce that it can provide a value for the given key
func
(
dht
*
IpfsDHT
)
Provide
(
ctx
context
.
Context
,
key
u
.
Key
)
error
{
log
.
Event
(
ctx
,
"
P
rovide
Value start
"
,
&
key
)
defer
log
.
Event
(
ctx
,
"
P
rovide
Value e
nd"
,
&
key
)
log
.
Event
(
ctx
,
"
p
rovide
Begin
"
,
&
key
)
defer
log
.
Event
(
ctx
,
"
p
rovide
E
nd"
,
&
key
)
dht
.
providers
.
AddProvider
(
key
,
dht
.
self
)
peers
,
err
:=
dht
.
getClosestPeers
(
ctx
,
key
,
KValue
)
peers
,
err
:=
dht
.
getClosestPeers
(
ctx
,
key
)
if
err
!=
nil
{
return
err
}
...
...
@@ -149,14 +149,16 @@ func (dht *IpfsDHT) FindProviders(ctx context.Context, key u.Key) ([]peer.PeerIn
return
providers
,
nil
}
func
(
dht
*
IpfsDHT
)
getClosestPeers
(
ctx
context
.
Context
,
key
u
.
Key
,
count
int
)
(
<-
chan
peer
.
ID
,
error
)
{
// Kademlia 'node lookup' operation. Returns a channel of the K closest peers
// to the given key
func
(
dht
*
IpfsDHT
)
getClosestPeers
(
ctx
context
.
Context
,
key
u
.
Key
)
(
<-
chan
peer
.
ID
,
error
)
{
tablepeers
:=
dht
.
routingTable
.
NearestPeers
(
kb
.
ConvertKey
(
key
),
AlphaValue
)
if
len
(
tablepeers
)
==
0
{
return
nil
,
kb
.
ErrLookupFailure
}
out
:=
make
(
chan
peer
.
ID
,
count
)
peerset
:=
pset
.
NewLimited
(
count
)
out
:=
make
(
chan
peer
.
ID
,
KValue
)
peerset
:=
pset
.
NewLimited
(
KValue
)
for
_
,
p
:=
range
tablepeers
{
select
{
...
...
@@ -211,10 +213,6 @@ func (dht *IpfsDHT) closerPeersSingle(ctx context.Context, key u.Key, p peer.ID)
for
_
,
pbp
:=
range
pmes
.
GetCloserPeers
()
{
pid
:=
peer
.
ID
(
pbp
.
GetId
())
dht
.
peerstore
.
AddAddresses
(
pid
,
pbp
.
Addresses
())
err
:=
dht
.
ensureConnectedToPeer
(
ctx
,
pid
)
if
err
!=
nil
{
return
nil
,
err
}
out
=
append
(
out
,
pid
)
}
return
out
,
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