dht/server.go | 27 +++++++++++---------------- diff --git a/dht/server.go b/dht/server.go index 8a9b5adeecc93a0baf4ae9063f0efac65f6df422..7551a9b07c1d75b5ae03fd6252b55485c9202b07 100644 --- a/dht/server.go +++ b/dht/server.go @@ -36,7 +36,7 @@ transactions map[transactionKey]*Transaction transactionIDInt uint64 nodes map[string]*node // Keyed by dHTAddr.String(). mu sync.Mutex - closed chan struct{} + closed missinggo.Event ipBlockList iplist.Ranger badNodes *boom.BloomFilter @@ -100,10 +100,10 @@ return } go func() { err := s.serve() - select { - case <-s.closed: + s.mu.Lock() + defer s.mu.Unlock() + if s.closed.IsSet() { return - default: } if err != nil { panic(err) @@ -112,11 +112,11 @@ }() go func() { err := s.bootstrap() if err != nil { - select { - case <-s.closed: - default: + s.mu.Lock() + if !s.closed.IsSet() { log.Printf("error bootstrapping DHT: %s", err) } + s.mu.Unlock() } }() return @@ -143,7 +143,6 @@ err = s.setDefaults() if err != nil { return } - s.closed = make(chan struct{}) s.transactions = make(map[transactionKey]*Transaction) return } @@ -586,7 +585,7 @@ close(noOutstanding) }() s.mu.Unlock() select { - case <-s.closed: + case <-s.closed.C(): s.mu.Lock() return case <-time.After(15 * time.Second): @@ -639,13 +638,9 @@ // Stops the server network activity. This is all that's required to clean-up a Server. func (s *Server) Close() { s.mu.Lock() - select { - case <-s.closed: - default: - close(s.closed) - s.socket.Close() - } - s.mu.Unlock() + defer s.mu.Unlock() + s.closed.Set() + s.socket.Close() } func (s *Server) setDefaults() (err error) {