cubefs
1// Copyright 2019 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package proxy6
7import (8"context"9"net"10)
11
12// A ContextDialer dials using a context.
13type ContextDialer interface {14DialContext(ctx context.Context, network, address string) (net.Conn, error)15}
16
17// Dial works like DialContext on net.Dialer but using a dialer returned by FromEnvironment.
18//
19// The passed ctx is only used for returning the Conn, not the lifetime of the Conn.
20//
21// Custom dialers (registered via RegisterDialerType) that do not implement ContextDialer
22// can leak a goroutine for as long as it takes the underlying Dialer implementation to timeout.
23//
24// A Conn returned from a successful Dial after the context has been cancelled will be immediately closed.
25func Dial(ctx context.Context, network, address string) (net.Conn, error) {26d := FromEnvironment()27if xd, ok := d.(ContextDialer); ok {28return xd.DialContext(ctx, network, address)29}30return dialContext(ctx, d, network, address)31}
32
33// WARNING: this can leak a goroutine for as long as the underlying Dialer implementation takes to timeout
34// A Conn returned from a successful Dial after the context has been cancelled will be immediately closed.
35func dialContext(ctx context.Context, d Dialer, network, address string) (net.Conn, error) {36var (37conn net.Conn38done = make(chan struct{}, 1)39err error40)41go func() {42conn, err = d.Dial(network, address)43close(done)44if conn != nil && ctx.Err() != nil {45conn.Close()46}47}()48select {49case <-ctx.Done():50err = ctx.Err()51case <-done:52}53return conn, err54}
55