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

@@ -47,7 +47,7 @@ func newStaticClient(uri *url.URL, certs []tls.Certificate, invitations chan pro
}
func (c *staticClient) serve(ctx context.Context) error {
if err := c.connect(); err != nil {
if err := c.connect(ctx); err != nil {
l.Infof("Could not connect to relay %s: %s", c.uri, err)
return err
}
@@ -146,13 +146,15 @@ func (c *staticClient) URI() *url.URL {
return c.uri
}
func (c *staticClient) connect() error {
func (c *staticClient) connect(ctx context.Context) error {
if c.uri.Scheme != "relay" {
return fmt.Errorf("unsupported relay scheme: %v", c.uri.Scheme)
}
t0 := time.Now()
tcpConn, err := dialer.DialTimeout("tcp", c.uri.Host, c.connectTimeout)
timeoutCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
tcpConn, err := dialer.DialContext(timeoutCtx, "tcp", c.uri.Host)
if err != nil {
return err
}