From f5ea00b297bb394f6359d5830599716e07e9579e Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Tue, 24 Dec 2013 11:10:49 -0500 Subject: [PATCH] Don't accumulate goroutines forever --- discover/discover.go | 53 ++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/discover/discover.go b/discover/discover.go index b0a816c8c..5b07b6e38 100644 --- a/discover/discover.go +++ b/discover/discover.go @@ -86,6 +86,8 @@ import ( "net" "sync" "time" + + "github.com/calmh/syncthing/buffers" ) const ( @@ -217,12 +219,18 @@ func (d *Discoverer) externalLookup(node string) (string, bool) { return "", false } - var res = make(chan string, 1) conn, err := net.DialUDP("udp", nil, extIP) if err != nil { log.Printf("discover/external: %v; no external lookup", err) return "", false } + defer conn.Close() + + err = conn.SetDeadline(time.Now().Add(5 * time.Second)) + if err != nil { + log.Printf("discover/external: %v; no external lookup", err) + return "", false + } _, err = conn.Write(EncodePacket(Packet{QueryMagic, 0, node, nil})) if err != nil { @@ -230,34 +238,27 @@ func (d *Discoverer) externalLookup(node string) (string, bool) { return "", false } - go func() { - var buf = make([]byte, 1024) - n, err := conn.Read(buf) - if err != nil { - log.Printf("discover/external/read: %v; no external lookup", err) - return - } + var buf = buffers.Get(1024) + defer buffers.Put(buf) - pkt, err := DecodePacket(buf[:n]) - if err != nil { - log.Printf("discover/external/read: %v; no external lookup", err) - return - } - - if pkt.Magic != AnnouncementMagic { - log.Printf("discover/external/read: bad magic; no external lookup", err) - return - } - - res <- fmt.Sprintf("%s:%d", ipStr(pkt.IP), pkt.Port) - }() - - select { - case r := <-res: - return r, true - case <-time.After(5 * time.Second): + n, err := conn.Read(buf) + if err != nil { + log.Printf("discover/external/read: %v; no external lookup", err) return "", false } + + pkt, err := DecodePacket(buf[:n]) + if err != nil { + log.Printf("discover/external/read: %v; no external lookup", err) + return "", false + } + + if pkt.Magic != AnnouncementMagic { + log.Printf("discover/external/read: bad magic; no external lookup", err) + return "", false + } + + return fmt.Sprintf("%s:%d", ipStr(pkt.IP), pkt.Port), true } func (d *Discoverer) Lookup(node string) (string, bool) {