Merge pull request #1149 from filecoin-project/fix/peermgr-ts

make expand peers thread safe
This commit is contained in:
Łukasz Magiera 2020-01-24 17:38:35 +01:00 committed by GitHub
commit 9e95372e5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -42,7 +42,7 @@ type PeerMgr struct {
maxFilPeers int
minFilPeers int
expanding bool
expanding chan struct{}
h host.Host
dht *dht.IpfsDHT
@ -56,7 +56,8 @@ func NewPeerMgr(h host.Host, dht *dht.IpfsDHT, bootstrap dtypes.BootstrapPeers)
dht: dht,
bootstrappers: bootstrap,
peers: make(map[peer.ID]time.Duration),
peers: make(map[peer.ID]time.Duration),
expanding: make(chan struct{}, 1),
maxFilPeers: MaxFilPeers,
minFilPeers: MinFilPeers,
@ -125,18 +126,19 @@ func (pmgr *PeerMgr) getPeerCount() int {
}
func (pmgr *PeerMgr) expandPeers() {
if pmgr.expanding {
select {
case pmgr.expanding <- struct{}{}:
default:
return
}
pmgr.expanding = true
go func() {
defer func() {
pmgr.expanding = false
}()
ctx, cancel := context.WithTimeout(context.TODO(), time.Second*30)
defer cancel()
pmgr.doExpand(ctx)
<-pmgr.expanding
}()
}