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-peerstore
Commits
96639ef5
Unverified
Commit
96639ef5
authored
Apr 28, 2019
by
Steven Allen
Committed by
GitHub
Apr 28, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #76 from libp2p/fix/issue-75
don't delete under the read lock
parents
32910204
f21f6eac
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
36 additions
and
13 deletions
+36
-13
pstoremem/addr_book.go
pstoremem/addr_book.go
+36
-13
No files found.
pstoremem/addr_book.go
View file @
96639ef5
...
@@ -36,24 +36,53 @@ type memoryAddrBook struct {
...
@@ -36,24 +36,53 @@ type memoryAddrBook struct {
// drastically increase the space waste. In our case, by 6x.
// drastically increase the space waste. In our case, by 6x.
addrs
map
[
peer
.
ID
]
map
[
string
]
*
expiringAddr
addrs
map
[
peer
.
ID
]
map
[
string
]
*
expiringAddr
nextGC
time
.
Time
ctx
context
.
Context
cancel
func
()
subManager
*
AddrSubManager
subManager
*
AddrSubManager
}
}
func
NewAddrBook
()
pstore
.
AddrBook
{
func
NewAddrBook
()
pstore
.
AddrBook
{
return
&
memoryAddrBook
{
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
ab
:=
&
memoryAddrBook
{
addrs
:
make
(
map
[
peer
.
ID
]
map
[
string
]
*
expiringAddr
),
addrs
:
make
(
map
[
peer
.
ID
]
map
[
string
]
*
expiringAddr
),
subManager
:
NewAddrSubManager
(),
subManager
:
NewAddrSubManager
(),
ctx
:
ctx
,
cancel
:
cancel
,
}
go
ab
.
background
()
return
ab
}
// background periodically schedules a gc
func
(
mab
*
memoryAddrBook
)
background
()
{
ticker
:=
time
.
NewTicker
(
1
*
time
.
Hour
)
defer
ticker
.
Stop
()
for
{
select
{
case
<-
ticker
.
C
:
mab
.
gc
()
case
<-
mab
.
ctx
.
Done
()
:
return
}
}
}
}
}
// gc garbage collects the in-memory address book. The caller *must* hold the addrmu lock.
func
(
mab
*
memoryAddrBook
)
Close
()
error
{
mab
.
cancel
()
return
nil
}
// gc garbage collects the in-memory address book.
func
(
mab
*
memoryAddrBook
)
gc
()
{
func
(
mab
*
memoryAddrBook
)
gc
()
{
mab
.
addrmu
.
Lock
()
defer
mab
.
addrmu
.
Unlock
()
now
:=
time
.
Now
()
now
:=
time
.
Now
()
if
!
now
.
After
(
mab
.
nextGC
)
{
return
}
for
p
,
amap
:=
range
mab
.
addrs
{
for
p
,
amap
:=
range
mab
.
addrs
{
for
k
,
addr
:=
range
amap
{
for
k
,
addr
:=
range
amap
{
if
addr
.
ExpiredBy
(
now
)
{
if
addr
.
ExpiredBy
(
now
)
{
...
@@ -64,7 +93,6 @@ func (mab *memoryAddrBook) gc() {
...
@@ -64,7 +93,6 @@ func (mab *memoryAddrBook) gc() {
delete
(
mab
.
addrs
,
p
)
delete
(
mab
.
addrs
,
p
)
}
}
}
}
mab
.
nextGC
=
time
.
Now
()
.
Add
(
pstore
.
AddressTTL
)
}
}
func
(
mab
*
memoryAddrBook
)
PeersWithAddrs
()
peer
.
IDSlice
{
func
(
mab
*
memoryAddrBook
)
PeersWithAddrs
()
peer
.
IDSlice
{
...
@@ -114,7 +142,6 @@ func (mab *memoryAddrBook) AddAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Du
...
@@ -114,7 +142,6 @@ func (mab *memoryAddrBook) AddAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Du
mab
.
subManager
.
BroadcastAddr
(
p
,
addr
)
mab
.
subManager
.
BroadcastAddr
(
p
,
addr
)
}
}
}
}
mab
.
gc
()
}
}
// SetAddr calls mgr.SetAddrs(p, addr, ttl)
// SetAddr calls mgr.SetAddrs(p, addr, ttl)
...
@@ -151,7 +178,6 @@ func (mab *memoryAddrBook) SetAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Du
...
@@ -151,7 +178,6 @@ func (mab *memoryAddrBook) SetAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Du
delete
(
amap
,
addrstr
)
delete
(
amap
,
addrstr
)
}
}
}
}
mab
.
gc
()
}
}
// UpdateAddrs updates the addresses associated with the given peer that have
// UpdateAddrs updates the addresses associated with the given peer that have
...
@@ -173,7 +199,6 @@ func (mab *memoryAddrBook) UpdateAddrs(p peer.ID, oldTTL time.Duration, newTTL t
...
@@ -173,7 +199,6 @@ func (mab *memoryAddrBook) UpdateAddrs(p peer.ID, oldTTL time.Duration, newTTL t
amap
[
k
]
=
addr
amap
[
k
]
=
addr
}
}
}
}
mab
.
gc
()
}
}
// Addresses returns all known (and valid) addresses for a given
// Addresses returns all known (and valid) addresses for a given
...
@@ -188,11 +213,9 @@ func (mab *memoryAddrBook) Addrs(p peer.ID) []ma.Multiaddr {
...
@@ -188,11 +213,9 @@ func (mab *memoryAddrBook) Addrs(p peer.ID) []ma.Multiaddr {
now
:=
time
.
Now
()
now
:=
time
.
Now
()
good
:=
make
([]
ma
.
Multiaddr
,
0
,
len
(
amap
))
good
:=
make
([]
ma
.
Multiaddr
,
0
,
len
(
amap
))
for
k
,
m
:=
range
amap
{
for
_
,
m
:=
range
amap
{
if
!
m
.
ExpiredBy
(
now
)
{
if
!
m
.
ExpiredBy
(
now
)
{
good
=
append
(
good
,
m
.
Addr
)
good
=
append
(
good
,
m
.
Addr
)
}
else
{
delete
(
amap
,
k
)
}
}
}
}
...
...
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