mirror of
https://github.com/octoleo/syncthing.git
synced 2024-12-22 19:08:58 +00:00
Don't accumulate goroutines forever
This commit is contained in:
parent
746d52930d
commit
f5ea00b297
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user