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
b37bee77
Unverified
Commit
b37bee77
authored
May 15, 2020
by
Steven Allen
Committed by
GitHub
May 15, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: add an AddedAt timestamp (#84)
`AddedAt` field for a peer in the Routing Table.
parent
260be2a2
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
34 additions
and
8 deletions
+34
-8
bucket.go
bucket.go
+3
-0
table.go
table.go
+24
-7
table_test.go
table_test.go
+7
-1
No files found.
bucket.go
View file @
b37bee77
...
@@ -21,6 +21,9 @@ type PeerInfo struct {
...
@@ -21,6 +21,9 @@ type PeerInfo struct {
// successful query response from the peer.
// successful query response from the peer.
LastSuccessfulOutboundQueryAt
time
.
Time
LastSuccessfulOutboundQueryAt
time
.
Time
// AddedAt is the time this peer was added to the routing table.
AddedAt
time
.
Time
// Id of the peer in the DHT XOR keyspace
// Id of the peer in the DHT XOR keyspace
dhtId
ID
dhtId
ID
}
}
...
...
table.go
View file @
b37bee77
...
@@ -136,9 +136,11 @@ func (rt *RoutingTable) TryAddPeer(p peer.ID, queryPeer bool) (bool, error) {
...
@@ -136,9 +136,11 @@ func (rt *RoutingTable) TryAddPeer(p peer.ID, queryPeer bool) (bool, error) {
func
(
rt
*
RoutingTable
)
addPeer
(
p
peer
.
ID
,
queryPeer
bool
)
(
bool
,
error
)
{
func
(
rt
*
RoutingTable
)
addPeer
(
p
peer
.
ID
,
queryPeer
bool
)
(
bool
,
error
)
{
bucketID
:=
rt
.
bucketIdForPeer
(
p
)
bucketID
:=
rt
.
bucketIdForPeer
(
p
)
bucket
:=
rt
.
buckets
[
bucketID
]
bucket
:=
rt
.
buckets
[
bucketID
]
now
:=
time
.
Now
()
var
lastUsefulAt
time
.
Time
var
lastUsefulAt
time
.
Time
if
queryPeer
{
if
queryPeer
{
lastUsefulAt
=
time
.
Now
()
lastUsefulAt
=
now
}
}
// peer already exists in the Routing Table.
// peer already exists in the Routing Table.
...
@@ -159,8 +161,13 @@ func (rt *RoutingTable) addPeer(p peer.ID, queryPeer bool) (bool, error) {
...
@@ -159,8 +161,13 @@ func (rt *RoutingTable) addPeer(p peer.ID, queryPeer bool) (bool, error) {
// We have enough space in the bucket (whether spawned or grouped).
// We have enough space in the bucket (whether spawned or grouped).
if
bucket
.
len
()
<
rt
.
bucketsize
{
if
bucket
.
len
()
<
rt
.
bucketsize
{
bucket
.
pushFront
(
&
PeerInfo
{
Id
:
p
,
LastUsefulAt
:
lastUsefulAt
,
LastSuccessfulOutboundQueryAt
:
time
.
Now
(),
bucket
.
pushFront
(
&
PeerInfo
{
dhtId
:
ConvertPeerID
(
p
)})
Id
:
p
,
LastUsefulAt
:
lastUsefulAt
,
LastSuccessfulOutboundQueryAt
:
now
,
AddedAt
:
now
,
dhtId
:
ConvertPeerID
(
p
),
})
rt
.
PeerAdded
(
p
)
rt
.
PeerAdded
(
p
)
return
true
,
nil
return
true
,
nil
}
}
...
@@ -174,8 +181,13 @@ func (rt *RoutingTable) addPeer(p peer.ID, queryPeer bool) (bool, error) {
...
@@ -174,8 +181,13 @@ func (rt *RoutingTable) addPeer(p peer.ID, queryPeer bool) (bool, error) {
// push the peer only if the bucket isn't overflowing after slitting
// push the peer only if the bucket isn't overflowing after slitting
if
bucket
.
len
()
<
rt
.
bucketsize
{
if
bucket
.
len
()
<
rt
.
bucketsize
{
bucket
.
pushFront
(
&
PeerInfo
{
Id
:
p
,
LastUsefulAt
:
lastUsefulAt
,
LastSuccessfulOutboundQueryAt
:
time
.
Now
(),
bucket
.
pushFront
(
&
PeerInfo
{
dhtId
:
ConvertPeerID
(
p
)})
Id
:
p
,
LastUsefulAt
:
lastUsefulAt
,
LastSuccessfulOutboundQueryAt
:
now
,
AddedAt
:
now
,
dhtId
:
ConvertPeerID
(
p
),
})
rt
.
PeerAdded
(
p
)
rt
.
PeerAdded
(
p
)
return
true
,
nil
return
true
,
nil
}
}
...
@@ -190,8 +202,13 @@ func (rt *RoutingTable) addPeer(p peer.ID, queryPeer bool) (bool, error) {
...
@@ -190,8 +202,13 @@ func (rt *RoutingTable) addPeer(p peer.ID, queryPeer bool) (bool, error) {
if
time
.
Since
(
minLast
.
LastUsefulAt
)
>
rt
.
usefulnessGracePeriod
{
if
time
.
Since
(
minLast
.
LastUsefulAt
)
>
rt
.
usefulnessGracePeriod
{
// let's evict it and add the new peer
// let's evict it and add the new peer
if
bucket
.
remove
(
minLast
.
Id
)
{
if
bucket
.
remove
(
minLast
.
Id
)
{
bucket
.
pushFront
(
&
PeerInfo
{
Id
:
p
,
LastUsefulAt
:
lastUsefulAt
,
LastSuccessfulOutboundQueryAt
:
time
.
Now
(),
bucket
.
pushFront
(
&
PeerInfo
{
dhtId
:
ConvertPeerID
(
p
)})
Id
:
p
,
LastUsefulAt
:
lastUsefulAt
,
LastSuccessfulOutboundQueryAt
:
now
,
AddedAt
:
now
,
dhtId
:
ConvertPeerID
(
p
),
})
rt
.
PeerAdded
(
p
)
rt
.
PeerAdded
(
p
)
return
true
,
nil
return
true
,
nil
}
}
...
...
table_test.go
View file @
b37bee77
...
@@ -34,7 +34,13 @@ func TestBucket(t *testing.T) {
...
@@ -34,7 +34,13 @@ func TestBucket(t *testing.T) {
peers
:=
make
([]
peer
.
ID
,
100
)
peers
:=
make
([]
peer
.
ID
,
100
)
for
i
:=
0
;
i
<
100
;
i
++
{
for
i
:=
0
;
i
<
100
;
i
++
{
peers
[
i
]
=
test
.
RandPeerIDFatal
(
t
)
peers
[
i
]
=
test
.
RandPeerIDFatal
(
t
)
b
.
pushFront
(
&
PeerInfo
{
peers
[
i
],
testTime1
,
testTime2
,
ConvertPeerID
(
peers
[
i
])})
b
.
pushFront
(
&
PeerInfo
{
Id
:
peers
[
i
],
LastUsefulAt
:
testTime1
,
LastSuccessfulOutboundQueryAt
:
testTime2
,
AddedAt
:
testTime1
,
dhtId
:
ConvertPeerID
(
peers
[
i
]),
})
}
}
local
:=
test
.
RandPeerIDFatal
(
t
)
local
:=
test
.
RandPeerIDFatal
(
t
)
...
...
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