mirror of
https://github.com/octoleo/syncthing.git
synced 2024-12-23 03:18:59 +00:00
Don't accumulate goroutines forever
This commit is contained in:
parent
746d52930d
commit
f5ea00b297
@ -86,6 +86,8 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/calmh/syncthing/buffers"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -217,12 +219,18 @@ func (d *Discoverer) externalLookup(node string) (string, bool) {
|
|||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
|
|
||||||
var res = make(chan string, 1)
|
|
||||||
conn, err := net.DialUDP("udp", nil, extIP)
|
conn, err := net.DialUDP("udp", nil, extIP)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("discover/external: %v; no external lookup", err)
|
log.Printf("discover/external: %v; no external lookup", err)
|
||||||
return "", false
|
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}))
|
_, err = conn.Write(EncodePacket(Packet{QueryMagic, 0, node, nil}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -230,34 +238,27 @@ func (d *Discoverer) externalLookup(node string) (string, bool) {
|
|||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
var buf = buffers.Get(1024)
|
||||||
var buf = make([]byte, 1024)
|
defer buffers.Put(buf)
|
||||||
n, err := conn.Read(buf)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("discover/external/read: %v; no external lookup", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
pkt, err := DecodePacket(buf[:n])
|
n, err := conn.Read(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("discover/external/read: %v; no external lookup", err)
|
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):
|
|
||||||
return "", false
|
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) {
|
func (d *Discoverer) Lookup(node string) (string, bool) {
|
||||||
|
Loading…
Reference in New Issue
Block a user