@@ -22,37 +22,21 @@ type Server struct {
2222 listeners map [string ]* internal.SSHListener
2323 // This contains the addresses of the nodes that we are currently connecting
2424 // to, so we don't try to connect to them twice at the same time.
25- currently_connecting map [string ]bool
25+ currently_connecting map [types. NodeAddress ]bool
2626 // Mutex to protect accesses to te currently_connecting map.
27- connecting_mutex sync.RWMutex
27+ connecting_mutex sync.Mutex
2828 listen_address string
2929}
3030
3131func NewServer (key Key , m types.Money ) * Server {
3232 n := internal .NewNode (key .k , m , time .Tick (30 * time .Second ), time .Tick (30 * time .Second ))
3333 return & Server {n , make (map [string ]* internal.SSHListener ),
34- make (map [string ]bool ), sync.RWMutex {}, "" }
34+ make (map [types. NodeAddress ]bool ), sync.Mutex {}, "" }
3535}
3636
3737func (s * Server ) Connect (addr string ) error {
38- // Check that we should be connecting.
39- s .connecting_mutex .RLock ()
40- _ , addr_present := s .currently_connecting [addr ]
41- s .connecting_mutex .RUnlock ()
42- if addr_present {
43- return errors .New (fmt .Sprintf ("Already connecting to %s.\n " , addr ))
44- }
45- s .connecting_mutex .Lock ()
46- s .currently_connecting [addr ] = true
47- s .connecting_mutex .Unlock ()
48-
4938 c , err := net .Dial ("tcp6" , addr )
5039
51- // Now that we've tried connecting, remove it as a pending connection.
52- s .connecting_mutex .Lock ()
53- delete (s .currently_connecting , addr )
54- s .connecting_mutex .Unlock ()
55-
5640 if err != nil {
5741 return err
5842 }
@@ -133,7 +117,23 @@ func (s *Server) findNeighbors(dev net.Interface, ll_addr net.IP, port uint16) {
133117 log .Print ("Warning: Ignoring connection from self.\n " )
134118 continue
135119 }
136- err := s .Connect (fmt .Sprintf ("[%s%%%s]:%v" , neighbor .LLAddrStr , dev .Name , neighbor .Port ))
120+
121+ // Check that we should be connecting.
122+ s .connecting_mutex .Lock ()
123+ _ , addr_present := s .currently_connecting [neighbor .FullNodeAddr ]
124+ if addr_present {
125+ log .Printf ("Already connecting to %s.\n " , neighbor .FullNodeAddr )
126+ continue
127+ }
128+ s .currently_connecting [neighbor .FullNodeAddr ] = true
129+
130+ err := s .Connect (fmt .Sprintf ("[%s%%%s]:%v" , neighbor .LLAddrStr , dev .Name ,
131+ neighbor .Port ))
132+
133+ // Now that we've tried connecting, remove it as a pending connection.
134+ delete (s .currently_connecting , neighbor .FullNodeAddr )
135+ s .connecting_mutex .Unlock ()
136+
137137 if err != nil {
138138 log .Printf ("Error connecting: %v" , err )
139139 continue
0 commit comments