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-bitswap
Commits
64ecba67
Commit
64ecba67
authored
Aug 20, 2019
by
Dirk McCormick
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: session peer manager ordering
parent
6a1362ca
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
19 deletions
+18
-19
sessionpeermanager/sessionpeermanager.go
sessionpeermanager/sessionpeermanager.go
+17
-18
sessionpeermanager/sessionpeermanager_test.go
sessionpeermanager/sessionpeermanager_test.go
+1
-1
No files found.
sessionpeermanager/sessionpeermanager.go
View file @
64ecba67
...
...
@@ -280,6 +280,8 @@ type getPeersMessage struct {
// Get all optimized peers in order followed by randomly ordered unoptimized
// peers, with a limit of maxOptimizedPeers
func
(
prm
*
getPeersMessage
)
handle
(
spm
*
SessionPeerManager
)
{
randomOrder
:=
rand
.
Perm
(
len
(
spm
.
unoptimizedPeersArr
))
// Number of peers to get in total: unoptimized + optimized
// limited by maxOptimizedPeers
maxPeers
:=
len
(
spm
.
unoptimizedPeersArr
)
+
len
(
spm
.
optimizedPeersArr
)
...
...
@@ -287,8 +289,8 @@ func (prm *getPeersMessage) handle(spm *SessionPeerManager) {
maxPeers
=
maxOptimizedPeers
}
// The best peer latency is
1 if we have recorded at least one peer's
//
latency, 0 otherwise
// The best peer latency is
the first optimized peer's latency.
//
If we haven't recorded any peer's latency, use 0.
var
bestPeerLatency
float64
if
len
(
spm
.
optimizedPeersArr
)
>
0
{
bestPeerLatency
=
float64
(
spm
.
activePeers
[
spm
.
optimizedPeersArr
[
0
]]
.
latency
)
...
...
@@ -297,23 +299,20 @@ func (prm *getPeersMessage) handle(spm *SessionPeerManager) {
}
optimizedPeers
:=
make
([]
bssd
.
OptimizedPeer
,
0
,
maxPeers
)
//
A
dd optimized peers in order
for
i
:=
0
;
i
<
maxPeers
&&
i
<
len
(
spm
.
optimizedPeersArr
)
;
i
++
{
for
i
:=
0
;
i
<
maxPeers
;
i
++
{
//
First a
dd optimized peers in order
if
i
<
len
(
spm
.
optimizedPeersArr
)
{
p
:=
spm
.
optimizedPeersArr
[
i
]
optimizedPeers
=
append
(
optimizedPeers
,
bssd
.
OptimizedPeer
{
Peer
:
p
,
OptimizationRating
:
bestPeerLatency
/
float64
(
spm
.
activePeers
[
p
]
.
latency
),
})
}
// Add unoptimized peers in random order
randomOrder
:=
rand
.
Perm
(
len
(
spm
.
unoptimizedPeersArr
))
remaining
:=
maxPeers
-
len
(
optimizedPeers
)
for
i
:=
0
;
i
<
remaining
;
i
++
{
p
:=
spm
.
unoptimizedPeersArr
[
randomOrder
[
i
]]
}
else
{
// Then add unoptimized peers in random order
p
:=
spm
.
unoptimizedPeersArr
[
randomOrder
[
i
-
len
(
spm
.
optimizedPeersArr
)]]
optimizedPeers
=
append
(
optimizedPeers
,
bssd
.
OptimizedPeer
{
Peer
:
p
,
OptimizationRating
:
0.0
})
}
}
prm
.
resp
<-
optimizedPeers
}
...
...
sessionpeermanager/sessionpeermanager_test.go
View file @
64ecba67
...
...
@@ -167,7 +167,7 @@ func TestOrderingPeers(t *testing.T) {
case
<-
ctx
.
Done
()
:
t
.
Fatal
(
"Did not finish finding providers"
)
}
time
.
Sleep
(
20
*
time
.
Millisecond
)
time
.
Sleep
(
5
*
time
.
Millisecond
)
// record broadcast
sessionPeerManager
.
RecordPeerRequests
(
nil
,
c
)
...
...
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