Commit 36444ac7 authored by Brian Tiger Chow's avatar Brian Tiger Chow
Browse files

hold lock during map access

parent f54e0c74
...@@ -129,12 +129,16 @@ func (pn *peernet) connect(p peer.ID) (*conn, error) { ...@@ -129,12 +129,16 @@ func (pn *peernet) connect(p peer.ID) (*conn, error) {
// first, check if we already have live connections // first, check if we already have live connections
pn.RLock() pn.RLock()
cs, found := pn.connsByPeer[p] cs, found := pn.connsByPeer[p]
pn.RUnlock()
if found && len(cs) > 0 { if found && len(cs) > 0 {
for c := range cs { var chosen *conn
return c, nil for c := range cs { // because cs is a map
chosen = c // select first
break
} }
pn.RUnlock()
return chosen, nil
} }
pn.RUnlock()
log.Debugf("%s (newly) dialing %s", pn.peer, p) log.Debugf("%s (newly) dialing %s", pn.peer, p)
...@@ -267,12 +271,17 @@ func (pn *peernet) ConnsToPeer(p peer.ID) []inet.Conn { ...@@ -267,12 +271,17 @@ func (pn *peernet) ConnsToPeer(p peer.ID) []inet.Conn {
func (pn *peernet) ClosePeer(p peer.ID) error { func (pn *peernet) ClosePeer(p peer.ID) error {
pn.RLock() pn.RLock()
cs, found := pn.connsByPeer[p] cs, found := pn.connsByPeer[p]
pn.RUnlock()
if !found { if !found {
pn.RUnlock()
return nil return nil
} }
var conns []*conn
for c := range cs { for c := range cs {
conns = append(conns, c)
}
pn.RUnlock()
for _, c := range conns {
c.Close() c.Close()
} }
return nil return nil
...@@ -319,7 +328,6 @@ func (pn *peernet) NewStream(p peer.ID) (inet.Stream, error) { ...@@ -319,7 +328,6 @@ func (pn *peernet) NewStream(p peer.ID) (inet.Stream, error) {
pn.Unlock() pn.Unlock()
return nil, fmt.Errorf("no connection to peer") return nil, fmt.Errorf("no connection to peer")
} }
pn.Unlock()
// if many conns are found, how do we select? for now, randomly... // if many conns are found, how do we select? for now, randomly...
// this would be an interesting place to test logic that can measure // this would be an interesting place to test logic that can measure
...@@ -332,6 +340,7 @@ func (pn *peernet) NewStream(p peer.ID) (inet.Stream, error) { ...@@ -332,6 +340,7 @@ func (pn *peernet) NewStream(p peer.ID) (inet.Stream, error) {
} }
n-- n--
} }
pn.Unlock()
return c.NewStream() return c.NewStream()
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment