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-kbucket
Commits
0779168b
Commit
0779168b
authored
Apr 03, 2020
by
Aarsh Shah
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
export field
parent
51478358
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
18 additions
and
18 deletions
+18
-18
bucket.go
bucket.go
+2
-2
table.go
table.go
+8
-8
table_test.go
table_test.go
+8
-8
No files found.
bucket.go
View file @
0779168b
...
...
@@ -12,9 +12,9 @@ import (
// PeerInfo holds all related information for a peer in the K-Bucket.
type
PeerInfo
struct
{
Id
peer
.
ID
//
l
astSuccessfulOutboundQuery is the time instant when we last made a successful
//
L
astSuccessfulOutboundQuery is the time instant when we last made a successful
// outbound query to this peer
l
astSuccessfulOutboundQuery
time
.
Time
L
astSuccessfulOutboundQuery
time
.
Time
// Id of the peer in the DHT XOR keyspace
dhtId
ID
...
...
table.go
View file @
0779168b
...
...
@@ -49,7 +49,7 @@ type RoutingTable struct {
PeerRemoved
func
(
peer
.
ID
)
PeerAdded
func
(
peer
.
ID
)
// maxLastSuccessfulOutboundThreshold is the max threshold/upper limit for the value of "
l
astSuccessfulOutboundQuery"
// maxLastSuccessfulOutboundThreshold is the max threshold/upper limit for the value of "
L
astSuccessfulOutboundQuery"
// of the peer in the bucket above which we will evict it to make place for a new peer if the bucket
// is full
maxLastSuccessfulOutboundThreshold
float64
...
...
@@ -87,12 +87,12 @@ func (rt *RoutingTable) Close() error {
}
// TryAddPeer tries to add a peer to the Routing table. If the peer ALREADY exists in the Routing Table, this call is a no-op.
// If the peer is a queryPeer i.e. we queried it or it queried us, we set the
l
astSuccessfulOutboundQuery to the current time.
// If the peer is a queryPeer i.e. we queried it or it queried us, we set the
L
astSuccessfulOutboundQuery to the current time.
// If the peer is just a peer that we connect to/it connected to us without any DHT query, we consider it as having
// no
l
astSuccessfulOutboundQuery.
// no
L
astSuccessfulOutboundQuery.
//
// If the logical bucket to which the peer belongs is full and it's not the last bucket, we try to replace an existing peer
// whose
l
astSuccessfulOutboundQuery is above the maximum allowed threshold in that bucket with the new peer.
// whose
L
astSuccessfulOutboundQuery is above the maximum allowed threshold in that bucket with the new peer.
// If no such peer exists in that bucket, we do NOT add the peer to the Routing Table and return error "ErrPeerRejectedNoCapacity".
// It returns a boolean value set to true if the peer was newly added to the Routing Table, false otherwise.
...
...
@@ -150,10 +150,10 @@ func (rt *RoutingTable) addPeer(p peer.ID, queryPeer bool) (bool, error) {
}
// the bucket to which the peer belongs is full. Let's try to find a peer
// in that bucket with a
l
astSuccessfulOutboundQuery value above the maximum threshold and replace it.
// in that bucket with a
L
astSuccessfulOutboundQuery value above the maximum threshold and replace it.
allPeers
:=
bucket
.
peers
()
for
_
,
pc
:=
range
allPeers
{
if
float64
(
time
.
Since
(
pc
.
l
astSuccessfulOutboundQuery
))
>
rt
.
maxLastSuccessfulOutboundThreshold
{
if
float64
(
time
.
Since
(
pc
.
L
astSuccessfulOutboundQuery
))
>
rt
.
maxLastSuccessfulOutboundThreshold
{
// let's evict it and add the new peer
if
bucket
.
remove
(
pc
.
Id
)
{
bucket
.
pushFront
(
&
PeerInfo
{
p
,
lastSuccessfulOutboundQuery
,
ConvertPeerID
(
p
)})
...
...
@@ -180,7 +180,7 @@ func (rt *RoutingTable) GetPeerInfos() []PeerInfo {
return
pis
}
// UpdateLastSuccessfulOutboundQuery updates the
l
astSuccessfulOutboundQuery time of the peer
// UpdateLastSuccessfulOutboundQuery updates the
L
astSuccessfulOutboundQuery time of the peer
// Returns true if the update was successful, false otherwise.
func
(
rt
*
RoutingTable
)
UpdateLastSuccessfulOutboundQuery
(
p
peer
.
ID
,
t
time
.
Time
)
bool
{
rt
.
tabLock
.
Lock
()
...
...
@@ -190,7 +190,7 @@ func (rt *RoutingTable) UpdateLastSuccessfulOutboundQuery(p peer.ID, t time.Time
bucket
:=
rt
.
buckets
[
bucketID
]
if
pc
:=
bucket
.
getPeer
(
p
);
pc
!=
nil
{
pc
.
l
astSuccessfulOutboundQuery
=
t
pc
.
L
astSuccessfulOutboundQuery
=
t
return
true
}
return
false
...
...
table_test.go
View file @
0779168b
...
...
@@ -47,14 +47,14 @@ func TestBucket(t *testing.T) {
require
.
NotNil
(
t
,
p
)
require
.
Equal
(
t
,
peers
[
i
],
p
.
Id
)
require
.
Equal
(
t
,
ConvertPeerID
(
peers
[
i
]),
p
.
dhtId
)
require
.
EqualValues
(
t
,
testTime1
,
p
.
l
astSuccessfulOutboundQuery
)
require
.
EqualValues
(
t
,
testTime1
,
p
.
L
astSuccessfulOutboundQuery
)
// mark as missing
t2
:=
time
.
Now
()
.
Add
(
1
*
time
.
Hour
)
p
.
l
astSuccessfulOutboundQuery
=
t2
p
.
L
astSuccessfulOutboundQuery
=
t2
p
=
b
.
getPeer
(
peers
[
i
])
require
.
NotNil
(
t
,
p
)
require
.
EqualValues
(
t
,
t2
,
p
.
l
astSuccessfulOutboundQuery
)
require
.
EqualValues
(
t
,
t2
,
p
.
L
astSuccessfulOutboundQuery
)
spl
:=
b
.
split
(
0
,
ConvertPeerID
(
local
))
llist
:=
b
.
list
...
...
@@ -218,7 +218,7 @@ func TestUpdateLastSuccessfulOutboundQuery(t *testing.T) {
rt
.
tabLock
.
Lock
()
pi
:=
rt
.
buckets
[
0
]
.
getPeer
(
p
)
require
.
NotNil
(
t
,
pi
)
require
.
EqualValues
(
t
,
t2
,
pi
.
l
astSuccessfulOutboundQuery
)
require
.
EqualValues
(
t
,
t2
,
pi
.
L
astSuccessfulOutboundQuery
)
rt
.
tabLock
.
Unlock
()
}
...
...
@@ -257,7 +257,7 @@ func TestTryAddPeer(t *testing.T) {
require
.
True
(
t
,
b
)
require
.
Equal
(
t
,
p4
,
rt
.
Find
(
p4
))
// adding a peer with cpl 0 works if an existing peer has
l
astSuccessfulOutboundQuery above the max threshold
// adding a peer with cpl 0 works if an existing peer has
L
astSuccessfulOutboundQuery above the max threshold
// because that existing peer will get replaced
require
.
True
(
t
,
rt
.
UpdateLastSuccessfulOutboundQuery
(
p2
,
time
.
Now
()
.
AddDate
(
0
,
0
,
-
1
)))
b
,
err
=
rt
.
TryAddPeer
(
p3
,
true
)
...
...
@@ -285,7 +285,7 @@ func TestTryAddPeer(t *testing.T) {
rt
.
tabLock
.
Lock
()
pi
:=
rt
.
buckets
[
rt
.
bucketIdForPeer
(
p6
)]
.
getPeer
(
p6
)
require
.
NotNil
(
t
,
p6
)
require
.
True
(
t
,
pi
.
l
astSuccessfulOutboundQuery
.
IsZero
())
require
.
True
(
t
,
pi
.
L
astSuccessfulOutboundQuery
.
IsZero
())
rt
.
tabLock
.
Unlock
()
}
...
...
@@ -506,9 +506,9 @@ func TestGetPeerInfos(t *testing.T) {
}
require
.
Equal
(
t
,
p1
,
ms
[
p1
]
.
Id
)
require
.
True
(
t
,
ms
[
p1
]
.
l
astSuccessfulOutboundQuery
.
IsZero
())
require
.
True
(
t
,
ms
[
p1
]
.
L
astSuccessfulOutboundQuery
.
IsZero
())
require
.
Equal
(
t
,
p2
,
ms
[
p2
]
.
Id
)
require
.
False
(
t
,
ms
[
p2
]
.
l
astSuccessfulOutboundQuery
.
IsZero
())
require
.
False
(
t
,
ms
[
p2
]
.
L
astSuccessfulOutboundQuery
.
IsZero
())
}
func
BenchmarkAddPeer
(
b
*
testing
.
B
)
{
...
...
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