Merge pull request #1149 from filecoin-project/fix/peermgr-ts
make expand peers thread safe
This commit is contained in:
commit
9e95372e5d
@ -42,7 +42,7 @@ type PeerMgr struct {
|
|||||||
maxFilPeers int
|
maxFilPeers int
|
||||||
minFilPeers int
|
minFilPeers int
|
||||||
|
|
||||||
expanding bool
|
expanding chan struct{}
|
||||||
|
|
||||||
h host.Host
|
h host.Host
|
||||||
dht *dht.IpfsDHT
|
dht *dht.IpfsDHT
|
||||||
@ -56,7 +56,8 @@ func NewPeerMgr(h host.Host, dht *dht.IpfsDHT, bootstrap dtypes.BootstrapPeers)
|
|||||||
dht: dht,
|
dht: dht,
|
||||||
bootstrappers: bootstrap,
|
bootstrappers: bootstrap,
|
||||||
|
|
||||||
peers: make(map[peer.ID]time.Duration),
|
peers: make(map[peer.ID]time.Duration),
|
||||||
|
expanding: make(chan struct{}, 1),
|
||||||
|
|
||||||
maxFilPeers: MaxFilPeers,
|
maxFilPeers: MaxFilPeers,
|
||||||
minFilPeers: MinFilPeers,
|
minFilPeers: MinFilPeers,
|
||||||
@ -125,18 +126,19 @@ func (pmgr *PeerMgr) getPeerCount() int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (pmgr *PeerMgr) expandPeers() {
|
func (pmgr *PeerMgr) expandPeers() {
|
||||||
if pmgr.expanding {
|
select {
|
||||||
|
case pmgr.expanding <- struct{}{}:
|
||||||
|
default:
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
pmgr.expanding = true
|
|
||||||
go func() {
|
go func() {
|
||||||
defer func() {
|
|
||||||
pmgr.expanding = false
|
|
||||||
}()
|
|
||||||
ctx, cancel := context.WithTimeout(context.TODO(), time.Second*30)
|
ctx, cancel := context.WithTimeout(context.TODO(), time.Second*30)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
pmgr.doExpand(ctx)
|
pmgr.doExpand(ctx)
|
||||||
|
|
||||||
|
<-pmgr.expanding
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user