p2p: use RLock instead of Lock for pre-dial checks
This commit is contained in:
parent
b3c058a9e4
commit
56977c225e
|
@ -86,12 +86,12 @@ type Server struct {
|
||||||
|
|
||||||
ourHandshake *protoHandshake
|
ourHandshake *protoHandshake
|
||||||
|
|
||||||
lock sync.RWMutex
|
lock sync.RWMutex // protects running and peers
|
||||||
running bool
|
running bool
|
||||||
listener net.Listener
|
|
||||||
peers map[discover.NodeID]*Peer
|
peers map[discover.NodeID]*Peer
|
||||||
|
|
||||||
ntab *discover.Table
|
ntab *discover.Table
|
||||||
|
listener net.Listener
|
||||||
|
|
||||||
quit chan struct{}
|
quit chan struct{}
|
||||||
loopWG sync.WaitGroup // {dial,listen,nat}Loop
|
loopWG sync.WaitGroup // {dial,listen,nat}Loop
|
||||||
|
@ -293,16 +293,17 @@ func (srv *Server) dialLoop() {
|
||||||
|
|
||||||
// TODO: maybe limit number of active dials
|
// TODO: maybe limit number of active dials
|
||||||
dial := func(dest *discover.Node) {
|
dial := func(dest *discover.Node) {
|
||||||
srv.lock.Lock()
|
|
||||||
ok, _ := srv.checkPeer(dest.ID)
|
|
||||||
srv.lock.Unlock()
|
|
||||||
// Don't dial nodes that would fail the checks in addPeer.
|
// Don't dial nodes that would fail the checks in addPeer.
|
||||||
// This is important because the connection handshake is a lot
|
// This is important because the connection handshake is a lot
|
||||||
// of work and we'd rather avoid doing that work for peers
|
// of work and we'd rather avoid doing that work for peers
|
||||||
// that can't be added.
|
// that can't be added.
|
||||||
|
srv.lock.RLock()
|
||||||
|
ok, _ := srv.checkPeer(dest.ID)
|
||||||
|
srv.lock.RUnlock()
|
||||||
if !ok || dialing[dest.ID] {
|
if !ok || dialing[dest.ID] {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
dialing[dest.ID] = true
|
dialing[dest.ID] = true
|
||||||
srv.peerWG.Add(1)
|
srv.peerWG.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
|
@ -315,9 +316,10 @@ func (srv *Server) dialLoop() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-refresh.C:
|
case <-refresh.C:
|
||||||
srv.lock.Lock()
|
// Grab some nodes to connect to if we're not at capacity.
|
||||||
|
srv.lock.RLock()
|
||||||
needpeers := len(srv.peers) < srv.MaxPeers
|
needpeers := len(srv.peers) < srv.MaxPeers
|
||||||
srv.lock.Unlock()
|
srv.lock.RUnlock()
|
||||||
if needpeers {
|
if needpeers {
|
||||||
go func() {
|
go func() {
|
||||||
var target discover.NodeID
|
var target discover.NodeID
|
||||||
|
|
Loading…
Reference in New Issue