all: Use context in lib/dialer (#6177)

* all: Use context in lib/dialer

* a bit slimmer

* https://github.com/syncthing/syncthing/pull/5753

* bot

* missed adding debug.go

* errors.Cause

* simultaneous dialing

* anti-leak
This commit is contained in:
Simon Frei
2019-11-26 08:39:51 +01:00
committed by Audrius Butkevicius
parent 4e151d380c
commit 1bae4b7f50
24 changed files with 175 additions and 204 deletions

View File

@@ -7,6 +7,7 @@
package pmp
import (
"context"
"fmt"
"net"
"strings"
@@ -21,7 +22,7 @@ func init() {
nat.Register(Discover)
}
func Discover(renewal, timeout time.Duration) []nat.Device {
func Discover(ctx context.Context, renewal, timeout time.Duration) []nat.Device {
ip, err := gateway.DiscoverGateway()
if err != nil {
l.Debugln("Failed to discover gateway", err)
@@ -44,7 +45,9 @@ func Discover(renewal, timeout time.Duration) []nat.Device {
var localIP net.IP
// Port comes from the natpmp package
conn, err := net.DialTimeout("udp", net.JoinHostPort(ip.String(), "5351"), timeout)
timeoutCtx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
conn, err := (&net.Dialer{}).DialContext(timeoutCtx, "udp", net.JoinHostPort(ip.String(), "5351"))
if err == nil {
conn.Close()
localIPAddress, _, err := net.SplitHostPort(conn.LocalAddr().String())