Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
mybee
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
vicotor
mybee
Commits
c85e55ad
Unverified
Commit
c85e55ad
authored
Jun 17, 2020
by
Petar Radovic
Committed by
GitHub
Jun 17, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Clean addressbook after failed connection attempts (#309)
* Forget about non connectable peers in kademlia
parent
3c0c02e2
Changes
3
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
169 additions
and
46 deletions
+169
-46
addressbook.go
pkg/addressbook/addressbook.go
+9
-0
kademlia.go
pkg/kademlia/kademlia.go
+36
-9
kademlia_test.go
pkg/kademlia/kademlia_test.go
+124
-37
No files found.
pkg/addressbook/addressbook.go
View file @
c85e55ad
...
...
@@ -19,6 +19,7 @@ var _ Interface = (*store)(nil)
type
Interface
interface
{
GetPutter
Remover
Overlays
()
([]
swarm
.
Address
,
error
)
Addresses
()
([]
bzz
.
Address
,
error
)
}
...
...
@@ -36,6 +37,10 @@ type Putter interface {
Put
(
overlay
swarm
.
Address
,
addr
bzz
.
Address
)
(
err
error
)
}
type
Remover
interface
{
Remove
(
overlay
swarm
.
Address
)
error
}
type
store
struct
{
store
storage
.
StateStorer
}
...
...
@@ -61,6 +66,10 @@ func (s *store) Put(overlay swarm.Address, addr bzz.Address) (err error) {
return
s
.
store
.
Put
(
key
,
&
addr
)
}
func
(
s
*
store
)
Remove
(
overlay
swarm
.
Address
)
error
{
return
s
.
store
.
Delete
(
keyPrefix
+
overlay
.
String
())
}
func
(
s
*
store
)
Overlays
()
(
overlays
[]
swarm
.
Address
,
err
error
)
{
err
=
s
.
store
.
Iterate
(
keyPrefix
,
func
(
key
,
_
[]
byte
)
(
stop
bool
,
err
error
)
{
k
:=
string
(
key
)
...
...
pkg/kademlia/kademlia.go
View file @
c85e55ad
...
...
@@ -25,6 +25,7 @@ import (
const
(
maxBins
=
16
nnLowWatermark
=
2
// the number of peers in consecutive deepest bins that constitute as nearest neighbours
maxConnAttempts
=
3
// when there is maxConnAttempts failed connect calls for a given peer it is considered non-connectable
)
var
(
...
...
@@ -56,13 +57,18 @@ type Kad struct {
depth
uint8
// current neighborhood depth
depthMu
sync
.
RWMutex
// protect depth changes
manageC
chan
struct
{}
// trigger the manage forever loop to connect to new peers
waitNext
map
[
string
]
time
.
Time
// sanction connections to a peer, key is overlay string and value is time to next retry
waitNext
map
[
string
]
retryInfo
// sanction connections to a peer, key is overlay string and value is a retry information
waitNextMu
sync
.
Mutex
// synchronize map
logger
logging
.
Logger
// logger
quit
chan
struct
{}
// quit channel
done
chan
struct
{}
// signal that `manage` has quit
}
type
retryInfo
struct
{
tryAfter
time
.
Time
failedAttempts
int
}
// New returns a new Kademlia.
func
New
(
o
Options
)
*
Kad
{
if
o
.
SaturationFunc
==
nil
{
...
...
@@ -78,7 +84,7 @@ func New(o Options) *Kad {
connectedPeers
:
pslice
.
New
(
maxBins
),
knownPeers
:
pslice
.
New
(
maxBins
),
manageC
:
make
(
chan
struct
{},
1
),
waitNext
:
make
(
map
[
string
]
time
.
Time
),
waitNext
:
make
(
map
[
string
]
retryInfo
),
logger
:
o
.
Logger
,
quit
:
make
(
chan
struct
{}),
done
:
make
(
chan
struct
{}),
...
...
@@ -115,7 +121,7 @@ func (k *Kad) manage() {
}
k
.
waitNextMu
.
Lock
()
if
next
,
ok
:=
k
.
waitNext
[
peer
.
String
()];
ok
&&
time
.
Now
()
.
Before
(
next
)
{
if
next
,
ok
:=
k
.
waitNext
[
peer
.
String
()];
ok
&&
time
.
Now
()
.
Before
(
next
.
tryAfter
)
{
k
.
waitNextMu
.
Unlock
()
return
false
,
false
,
nil
}
...
...
@@ -248,12 +254,33 @@ func (k *Kad) connect(ctx context.Context, peer swarm.Address, ma ma.Multiaddr,
if
errors
.
Is
(
err
,
p2p
.
ErrAlreadyConnected
)
{
return
nil
}
k
.
logger
.
Debugf
(
"error connecting to peer %s: %v"
,
peer
,
err
)
retryTime
:=
time
.
Now
()
.
Add
(
timeToRetry
)
var
e
*
p2p
.
ConnectionBackoffError
k
.
waitNextMu
.
Lock
()
k
.
waitNext
[
peer
.
String
()]
=
time
.
Now
()
.
Add
(
timeToRetry
)
k
.
waitNextMu
.
Unlock
()
failedAttempts
:=
0
if
errors
.
As
(
err
,
&
e
)
{
retryTime
=
e
.
TryAfter
()
}
else
{
info
,
ok
:=
k
.
waitNext
[
peer
.
String
()]
if
ok
{
failedAttempts
=
info
.
failedAttempts
}
failedAttempts
++
}
// TODO: somehow keep track of attempts and at some point forget about the peer
if
failedAttempts
>
maxConnAttempts
{
delete
(
k
.
waitNext
,
peer
.
String
())
if
err
:=
k
.
addressBook
.
Remove
(
peer
);
err
!=
nil
{
k
.
logger
.
Debugf
(
"could not remove peer from addressbook: %s"
,
peer
.
String
())
}
}
else
{
k
.
waitNext
[
peer
.
String
()]
=
retryInfo
{
tryAfter
:
retryTime
,
failedAttempts
:
failedAttempts
}
}
k
.
waitNextMu
.
Unlock
()
return
err
}
...
...
@@ -335,7 +362,7 @@ func (k *Kad) Disconnected(addr swarm.Address) {
k
.
connectedPeers
.
Remove
(
addr
,
po
)
k
.
waitNextMu
.
Lock
()
k
.
waitNext
[
addr
.
String
()]
=
time
.
Now
()
.
Add
(
timeToRetry
)
k
.
waitNext
[
addr
.
String
()]
=
retryInfo
{
tryAfter
:
time
.
Now
()
.
Add
(
timeToRetry
),
failedAttempts
:
0
}
k
.
waitNextMu
.
Unlock
()
k
.
depthMu
.
Lock
()
...
...
pkg/kademlia/kademlia_test.go
View file @
c85e55ad
This diff is collapsed.
Click to expand it.
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