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:
committed by
Audrius Butkevicius
parent
4e151d380c
commit
1bae4b7f50
@@ -153,7 +153,7 @@ func relayAddressesOrder(ctx context.Context, input []string) []string {
|
||||
buckets := make(map[int][]string)
|
||||
|
||||
for _, relay := range input {
|
||||
latency, err := osutil.GetLatencyForURL(relay)
|
||||
latency, err := osutil.GetLatencyForURL(ctx, relay)
|
||||
if err != nil {
|
||||
latency = time.Hour
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net"
|
||||
@@ -16,12 +17,14 @@ import (
|
||||
"github.com/syncthing/syncthing/lib/relay/protocol"
|
||||
)
|
||||
|
||||
func GetInvitationFromRelay(uri *url.URL, id syncthingprotocol.DeviceID, certs []tls.Certificate, timeout time.Duration) (protocol.SessionInvitation, error) {
|
||||
func GetInvitationFromRelay(ctx context.Context, uri *url.URL, id syncthingprotocol.DeviceID, certs []tls.Certificate, timeout time.Duration) (protocol.SessionInvitation, error) {
|
||||
if uri.Scheme != "relay" {
|
||||
return protocol.SessionInvitation{}, fmt.Errorf("Unsupported relay scheme: %v", uri.Scheme)
|
||||
}
|
||||
|
||||
rconn, err := dialer.DialTimeout("tcp", uri.Host, timeout)
|
||||
ctx, cancel := context.WithTimeout(ctx, timeout)
|
||||
defer cancel()
|
||||
rconn, err := dialer.DialContext(ctx, "tcp", uri.Host)
|
||||
if err != nil {
|
||||
return protocol.SessionInvitation{}, err
|
||||
}
|
||||
@@ -63,10 +66,12 @@ func GetInvitationFromRelay(uri *url.URL, id syncthingprotocol.DeviceID, certs [
|
||||
}
|
||||
}
|
||||
|
||||
func JoinSession(invitation protocol.SessionInvitation) (net.Conn, error) {
|
||||
func JoinSession(ctx context.Context, invitation protocol.SessionInvitation) (net.Conn, error) {
|
||||
addr := net.JoinHostPort(net.IP(invitation.Address).String(), strconv.Itoa(int(invitation.Port)))
|
||||
|
||||
conn, err := dialer.Dial("tcp", addr)
|
||||
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
||||
defer cancel()
|
||||
conn, err := dialer.DialContext(ctx, "tcp", addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -99,7 +104,7 @@ func JoinSession(invitation protocol.SessionInvitation) (net.Conn, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRelay(uri *url.URL, certs []tls.Certificate, sleep, timeout time.Duration, times int) bool {
|
||||
func TestRelay(ctx context.Context, uri *url.URL, certs []tls.Certificate, sleep, timeout time.Duration, times int) bool {
|
||||
id := syncthingprotocol.NewDeviceID(certs[0].Certificate[0])
|
||||
invs := make(chan protocol.SessionInvitation, 1)
|
||||
c, err := NewClient(uri, certs, invs, timeout)
|
||||
@@ -114,7 +119,7 @@ func TestRelay(uri *url.URL, certs []tls.Certificate, sleep, timeout time.Durati
|
||||
}()
|
||||
|
||||
for i := 0; i < times; i++ {
|
||||
_, err := GetInvitationFromRelay(uri, id, certs, timeout)
|
||||
_, err := GetInvitationFromRelay(ctx, uri, id, certs, timeout)
|
||||
if err == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user