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
3c0c02e2
Unverified
Commit
3c0c02e2
authored
Jun 17, 2020
by
acud
Committed by
GitHub
Jun 17, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
kademlia: change pslice mutex to use rwmutex (#313)
parent
26d2f835
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
19 additions
and
13 deletions
+19
-13
kademlia.go
pkg/kademlia/kademlia.go
+8
-2
pslice.go
pkg/kademlia/pslice/pslice.go
+11
-11
No files found.
pkg/kademlia/kademlia.go
View file @
3c0c02e2
...
...
@@ -91,19 +91,24 @@ func New(o Options) *Kad {
// manage is a forever loop that manages the connection to new peers
// once they get added or once others leave.
func
(
k
*
Kad
)
manage
()
{
var
peerToRemove
swarm
.
Address
var
(
peerToRemove
swarm
.
Address
start
time
.
Time
)
defer
close
(
k
.
done
)
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
go
func
()
{
<-
k
.
quit
cancel
()
}()
for
{
select
{
case
<-
k
.
quit
:
return
case
<-
k
.
manageC
:
start
=
time
.
Now
()
err
:=
k
.
knownPeers
.
EachBinRev
(
func
(
peer
swarm
.
Address
,
po
uint8
)
(
bool
,
bool
,
error
)
{
if
k
.
connectedPeers
.
Exists
(
peer
)
{
return
false
,
false
,
nil
...
...
@@ -166,6 +171,7 @@ func (k *Kad) manage() {
// be made before checking the next peer, so we iterate to next
return
false
,
false
,
nil
})
k
.
logger
.
Tracef
(
"kademlia iterator took %s to finish"
,
time
.
Since
(
start
))
if
err
!=
nil
{
if
errors
.
Is
(
err
,
errMissingAddressBookEntry
)
{
...
...
pkg/kademlia/pslice/pslice.go
View file @
3c0c02e2
...
...
@@ -19,7 +19,7 @@ type PSlice struct {
peers
[]
swarm
.
Address
// the slice of peers
bins
[]
uint
// the indexes of every proximity order in the peers slice, index is po, value is index of peers slice
sync
.
Mutex
sync
.
RW
Mutex
}
// New creates a new PSlice.
...
...
@@ -32,8 +32,8 @@ func New(maxBins int) *PSlice {
// iterates over all peers from deepest bin to shallowest.
func
(
s
*
PSlice
)
EachBin
(
pf
topology
.
EachPeerFunc
)
error
{
s
.
Lock
()
defer
s
.
Unlock
()
s
.
R
Lock
()
defer
s
.
R
Unlock
()
if
len
(
s
.
peers
)
==
0
{
return
nil
...
...
@@ -63,8 +63,8 @@ func (s *PSlice) EachBin(pf topology.EachPeerFunc) error {
// EachBinRev iterates over all peers from shallowest bin to deepest.
func
(
s
*
PSlice
)
EachBinRev
(
pf
topology
.
EachPeerFunc
)
error
{
s
.
Lock
()
defer
s
.
Unlock
()
s
.
R
Lock
()
defer
s
.
R
Unlock
()
if
len
(
s
.
peers
)
==
0
{
return
nil
...
...
@@ -96,8 +96,8 @@ func (s *PSlice) EachBinRev(pf topology.EachPeerFunc) error {
}
func
(
s
*
PSlice
)
Length
()
int
{
s
.
Lock
()
defer
s
.
Unlock
()
s
.
R
Lock
()
defer
s
.
R
Unlock
()
return
len
(
s
.
peers
)
}
...
...
@@ -105,8 +105,8 @@ func (s *PSlice) Length() int {
// ShallowestEmpty returns the shallowest empty bin if one exists.
// If such bin does not exists, returns true as bool value.
func
(
s
*
PSlice
)
ShallowestEmpty
()
(
bin
uint8
,
none
bool
)
{
s
.
Lock
()
defer
s
.
Unlock
()
s
.
R
Lock
()
defer
s
.
R
Unlock
()
binCp
:=
make
([]
uint
,
len
(
s
.
bins
)
+
1
)
copy
(
binCp
,
s
.
bins
)
...
...
@@ -122,8 +122,8 @@ func (s *PSlice) ShallowestEmpty() (bin uint8, none bool) {
// Exists checks if a peer exists.
func
(
s
*
PSlice
)
Exists
(
addr
swarm
.
Address
)
bool
{
s
.
Lock
()
defer
s
.
Unlock
()
s
.
R
Lock
()
defer
s
.
R
Unlock
()
b
,
_
:=
s
.
exists
(
addr
)
return
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