mirror of
https://github.com/octoleo/syncthing.git
synced 2024-11-14 01:04:14 +00:00
lib/nat: Make service termination faster (#6777)
* lib/nat: Make service termination faster * Newline
This commit is contained in:
parent
aee4b10d3a
commit
cbaef624cf
@ -15,6 +15,7 @@ import (
|
|||||||
|
|
||||||
"github.com/jackpal/gateway"
|
"github.com/jackpal/gateway"
|
||||||
"github.com/jackpal/go-nat-pmp"
|
"github.com/jackpal/go-nat-pmp"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
"github.com/syncthing/syncthing/lib/nat"
|
"github.com/syncthing/syncthing/lib/nat"
|
||||||
"github.com/syncthing/syncthing/lib/util"
|
"github.com/syncthing/syncthing/lib/util"
|
||||||
@ -44,11 +45,19 @@ func Discover(ctx context.Context, renewal, timeout time.Duration) []nat.Device
|
|||||||
c := natpmp.NewClientWithTimeout(ip, timeout)
|
c := natpmp.NewClientWithTimeout(ip, timeout)
|
||||||
// Try contacting the gateway, if it does not respond, assume it does not
|
// Try contacting the gateway, if it does not respond, assume it does not
|
||||||
// speak NAT-PMP.
|
// speak NAT-PMP.
|
||||||
_, err = c.GetExternalAddress()
|
err = util.CallWithContext(ctx, func() error {
|
||||||
if err != nil && strings.Contains(err.Error(), "Timed out") {
|
_, ierr := c.GetExternalAddress()
|
||||||
|
return ierr
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
if errors.Cause(err) == context.Canceled {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if strings.Contains(err.Error(), "Timed out") {
|
||||||
l.Debugln("Timeout trying to get external address, assume no NAT-PMP available")
|
l.Debugln("Timeout trying to get external address, assume no NAT-PMP available")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var localIP net.IP
|
var localIP net.IP
|
||||||
// Port comes from the natpmp package
|
// Port comes from the natpmp package
|
||||||
|
@ -119,7 +119,13 @@ func Discover(ctx context.Context, renewal, timeout time.Duration) []nat.Device
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
seenResults := make(map[string]bool)
|
seenResults := make(map[string]bool)
|
||||||
for result := range resultChan {
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case result, ok := <-resultChan:
|
||||||
|
if !ok {
|
||||||
|
return results
|
||||||
|
}
|
||||||
if seenResults[result.ID()] {
|
if seenResults[result.ID()] {
|
||||||
l.Debugf("Skipping duplicate result %s", result.ID())
|
l.Debugf("Skipping duplicate result %s", result.ID())
|
||||||
continue
|
continue
|
||||||
@ -129,9 +135,10 @@ func Discover(ctx context.Context, renewal, timeout time.Duration) []nat.Device
|
|||||||
seenResults[result.ID()] = true
|
seenResults[result.ID()] = true
|
||||||
|
|
||||||
l.Debugf("UPnP discovery result %s", result.ID())
|
l.Debugf("UPnP discovery result %s", result.ID())
|
||||||
|
case <-ctx.Done():
|
||||||
|
return nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return results
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Search for UPnP InternetGatewayDevices for <timeout> seconds.
|
// Search for UPnP InternetGatewayDevices for <timeout> seconds.
|
||||||
@ -213,7 +220,11 @@ loop:
|
|||||||
}
|
}
|
||||||
for _, igd := range igds {
|
for _, igd := range igds {
|
||||||
igd := igd // Copy before sending pointer to the channel.
|
igd := igd // Copy before sending pointer to the channel.
|
||||||
results <- &igd
|
select {
|
||||||
|
case results <- &igd:
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
l.Debugln("Discovery for device type", deviceType, "on", intf.Name, "finished.")
|
l.Debugln("Discovery for device type", deviceType, "on", intf.Name, "finished.")
|
||||||
|
Loading…
Reference in New Issue
Block a user