From c6d41cf0274f70ddefab466975fee3c9e14580b3 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Sat, 6 Dec 2014 21:22:12 -0600 Subject: [PATCH] dht: Tidy up GetPeers() --- dht/getpeers.go | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/dht/getpeers.go b/dht/getpeers.go index 1f136c9c..8277a5c1 100644 --- a/dht/getpeers.go +++ b/dht/getpeers.go @@ -4,6 +4,7 @@ import ( "log" "net" "sync" + "time" "bitbucket.org/anacrolix/go.torrent/util" ) @@ -24,6 +25,21 @@ func (me *peerDiscovery) Close() { } func (s *Server) GetPeers(infoHash string) (*peerStream, error) { + s.mu.Lock() + startAddrs := func() (ret []net.Addr) { + for _, n := range s.closestGoodNodes(160, infoHash) { + ret = append(ret, n.addr) + } + return + }() + s.mu.Unlock() + if len(startAddrs) == 0 { + addr, err := bootstrapAddr() + if err != nil { + return nil, err + } + startAddrs = append(startAddrs, addr) + } disc := &peerDiscovery{ peerStream: &peerStream{ Values: make(chan peerStreamValue), @@ -36,22 +52,9 @@ func (s *Server) GetPeers(infoHash string) (*peerStream, error) { infoHash: infoHash, } go disc.loop() - s.mu.Lock() - startAddrs := func() (ret []net.Addr) { - for _, n := range s.closestGoodNodes(160, infoHash) { - ret = append(ret, n.addr) - } - return - }() - s.mu.Unlock() - for _, addr := range startAddrs { - disc.contact(addr) - } - if len(startAddrs) == 0 { - addr, err := bootstrapAddr() - if err != nil { - disc.Close() - return nil, err + for i, addr := range startAddrs { + if i != 0 { + time.Sleep(time.Microsecond) } disc.contact(addr) }