diff options
author | Felix Lange <fjl@twurst.com> | 2015-04-10 23:23:09 +0800 |
---|---|---|
committer | Felix Lange <fjl@twurst.com> | 2015-04-10 23:23:09 +0800 |
commit | 56977c225ecbf7834e380c03714c3e8d70e0b184 (patch) | |
tree | d07a06db2e6fa62bf187d3079352575a078e4870 /p2p | |
parent | b3c058a9e4e9296583ba516c537768b96a2fb8a0 (diff) | |
download | go-tangerine-56977c225ecbf7834e380c03714c3e8d70e0b184.tar.gz go-tangerine-56977c225ecbf7834e380c03714c3e8d70e0b184.tar.zst go-tangerine-56977c225ecbf7834e380c03714c3e8d70e0b184.zip |
p2p: use RLock instead of Lock for pre-dial checks
Diffstat (limited to 'p2p')
-rw-r--r-- | p2p/server.go | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/p2p/server.go b/p2p/server.go index 88f7ba2ec..7b880f77b 100644 --- a/p2p/server.go +++ b/p2p/server.go @@ -86,12 +86,12 @@ type Server struct { ourHandshake *protoHandshake - lock sync.RWMutex - running bool - listener net.Listener - peers map[discover.NodeID]*Peer + lock sync.RWMutex // protects running and peers + running bool + peers map[discover.NodeID]*Peer - ntab *discover.Table + ntab *discover.Table + listener net.Listener quit chan struct{} loopWG sync.WaitGroup // {dial,listen,nat}Loop @@ -293,16 +293,17 @@ func (srv *Server) dialLoop() { // TODO: maybe limit number of active dials 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. // This is important because the connection handshake is a lot // of work and we'd rather avoid doing that work for peers // that can't be added. + srv.lock.RLock() + ok, _ := srv.checkPeer(dest.ID) + srv.lock.RUnlock() if !ok || dialing[dest.ID] { return } + dialing[dest.ID] = true srv.peerWG.Add(1) go func() { @@ -315,9 +316,10 @@ func (srv *Server) dialLoop() { for { select { 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 - srv.lock.Unlock() + srv.lock.RUnlock() if needpeers { go func() { var target discover.NodeID |