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-swarm
Commits
b49b1c48
Unverified
Commit
b49b1c48
authored
Apr 02, 2020
by
Will Scott
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
simplify data structure
parent
a68f7aeb
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
11 additions
and
17 deletions
+11
-17
swarm_dial.go
swarm_dial.go
+11
-17
No files found.
swarm_dial.go
View file @
b49b1c48
...
@@ -97,12 +97,10 @@ const DefaultPerPeerRateLimit = 8
...
@@ -97,12 +97,10 @@ const DefaultPerPeerRateLimit = 8
// * It's thread-safe.
// * It's thread-safe.
// * It's *not* safe to move this type after using.
// * It's *not* safe to move this type after using.
type
DialBackoff
struct
{
type
DialBackoff
struct
{
entries
map
[
peer
.
ID
]
backoff
Pee
r
entries
map
[
peer
.
ID
]
map
[
string
]
*
backoff
Add
r
lock
sync
.
RWMutex
lock
sync
.
RWMutex
}
}
type
backoffPeer
map
[
ma
.
Multiaddr
]
*
backoffAddr
type
backoffAddr
struct
{
type
backoffAddr
struct
{
tries
int
tries
int
until
time
.
Time
until
time
.
Time
...
@@ -110,17 +108,17 @@ type backoffAddr struct {
...
@@ -110,17 +108,17 @@ type backoffAddr struct {
func
(
db
*
DialBackoff
)
init
(
ctx
context
.
Context
)
{
func
(
db
*
DialBackoff
)
init
(
ctx
context
.
Context
)
{
if
db
.
entries
==
nil
{
if
db
.
entries
==
nil
{
db
.
entries
=
make
(
map
[
peer
.
ID
]
backoff
Pee
r
)
db
.
entries
=
make
(
map
[
peer
.
ID
]
map
[
string
]
*
backoff
Add
r
)
}
}
go
db
.
background
(
ctx
)
go
db
.
background
(
ctx
)
}
}
func
(
db
*
DialBackoff
)
background
(
ctx
context
.
Context
)
{
func
(
db
*
DialBackoff
)
background
(
ctx
context
.
Context
)
{
ticker
:=
time
.
NewTicker
(
BackoffMax
)
ticker
:=
time
.
NewTicker
(
BackoffMax
)
defer
ticker
.
Stop
()
for
{
for
{
select
{
select
{
case
<-
ctx
.
Done
()
:
case
<-
ctx
.
Done
()
:
ticker
.
Stop
()
return
return
case
<-
ticker
.
C
:
case
<-
ticker
.
C
:
db
.
cleanup
()
db
.
cleanup
()
...
@@ -134,9 +132,8 @@ func (db *DialBackoff) Backoff(p peer.ID, addr ma.Multiaddr) (backoff bool) {
...
@@ -134,9 +132,8 @@ func (db *DialBackoff) Backoff(p peer.ID, addr ma.Multiaddr) (backoff bool) {
db
.
lock
.
Lock
()
db
.
lock
.
Lock
()
defer
db
.
lock
.
Unlock
()
defer
db
.
lock
.
Unlock
()
bp
,
found
:=
db
.
entries
[
p
]
bp
,
found
:=
db
.
entries
[
p
]
if
found
&&
bp
!=
nil
{
if
found
{
ap
,
found
:=
bp
[
addr
]
ap
,
found
:=
bp
[
string
(
addr
.
Bytes
())]
// TODO: cleanup out of date entries.
if
found
&&
time
.
Now
()
.
Before
(
ap
.
until
)
{
if
found
&&
time
.
Now
()
.
Before
(
ap
.
until
)
{
return
true
return
true
}
}
...
@@ -165,20 +162,21 @@ var BackoffMax = time.Minute * 5
...
@@ -165,20 +162,21 @@ var BackoffMax = time.Minute * 5
//
//
// Where PriorBackoffs is the number of previous backoffs.
// Where PriorBackoffs is the number of previous backoffs.
func
(
db
*
DialBackoff
)
AddBackoff
(
p
peer
.
ID
,
addr
ma
.
Multiaddr
)
{
func
(
db
*
DialBackoff
)
AddBackoff
(
p
peer
.
ID
,
addr
ma
.
Multiaddr
)
{
saddr
:=
string
(
addr
.
Bytes
())
db
.
lock
.
Lock
()
db
.
lock
.
Lock
()
defer
db
.
lock
.
Unlock
()
defer
db
.
lock
.
Unlock
()
bp
,
ok
:=
db
.
entries
[
p
]
bp
,
ok
:=
db
.
entries
[
p
]
if
!
ok
{
if
!
ok
{
db
.
entries
[
p
]
=
backoffPeer
(
make
(
map
[
ma
.
Multiaddr
]
*
backoffAddr
)
)
db
.
entries
[
p
]
=
make
(
map
[
string
]
*
backoffAddr
)
db
.
entries
[
p
][
addr
]
=
&
backoffAddr
{
db
.
entries
[
p
][
s
addr
]
=
&
backoffAddr
{
tries
:
1
,
tries
:
1
,
until
:
time
.
Now
()
.
Add
(
BackoffBase
),
until
:
time
.
Now
()
.
Add
(
BackoffBase
),
}
}
return
return
}
}
ba
,
ok
:=
bp
[
addr
]
ba
,
ok
:=
bp
[
s
addr
]
if
!
ok
{
if
!
ok
{
bp
[
addr
]
=
&
backoffAddr
{
bp
[
s
addr
]
=
&
backoffAddr
{
tries
:
1
,
tries
:
1
,
until
:
time
.
Now
()
.
Add
(
BackoffBase
),
until
:
time
.
Now
()
.
Add
(
BackoffBase
),
}
}
...
@@ -205,7 +203,6 @@ func (db *DialBackoff) cleanup() {
...
@@ -205,7 +203,6 @@ func (db *DialBackoff) cleanup() {
db
.
lock
.
Lock
()
db
.
lock
.
Lock
()
defer
db
.
lock
.
Unlock
()
defer
db
.
lock
.
Unlock
()
now
:=
time
.
Now
()
now
:=
time
.
Now
()
deletePeers
:=
[]
peer
.
ID
{}
for
p
,
e
:=
range
db
.
entries
{
for
p
,
e
:=
range
db
.
entries
{
good
:=
false
good
:=
false
for
_
,
backoff
:=
range
e
{
for
_
,
backoff
:=
range
e
{
...
@@ -215,12 +212,9 @@ func (db *DialBackoff) cleanup() {
...
@@ -215,12 +212,9 @@ func (db *DialBackoff) cleanup() {
}
}
}
}
if
!
good
{
if
!
good
{
delete
Peers
=
append
(
deletePeer
s
,
p
)
delete
(
db
.
entrie
s
,
p
)
}
}
}
}
for
_
,
p
:=
range
deletePeers
{
delete
(
db
.
entries
,
p
)
}
}
}
// DialPeer connects to a peer.
// DialPeer connects to a peer.
...
...
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