diff options
author | Erik Dubbelboer <erik@dubbelboer.com> | 2020-03-10 12:28:47 +0100 |
---|---|---|
committer | Erik Dubbelboer <erik@dubbelboer.com> | 2020-03-10 12:28:47 +0100 |
commit | 11e8301d6c52f89fd80eaabe0639eb6851052ad7 (patch) | |
tree | f7192c6d7366049ebdcff0e33988f80ef2c2a5fe /tcpdialer.go | |
parent | feat: workflow to verify security using GoSec (#747) (diff) | |
download | fasthttp-11e8301d6c52f89fd80eaabe0639eb6851052ad7.tar.gz fasthttp-11e8301d6c52f89fd80eaabe0639eb6851052ad7.tar.bz2 fasthttp-11e8301d6c52f89fd80eaabe0639eb6851052ad7.zip |
Add LocalAddr to TCPDialer
Diffstat (limited to 'tcpdialer.go')
-rw-r--r-- | tcpdialer.go | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/tcpdialer.go b/tcpdialer.go index 8e432cf..b51bb21 100644 --- a/tcpdialer.go +++ b/tcpdialer.go @@ -135,6 +135,11 @@ type TCPDialer struct { // Changes made after the first Dial will not affect anything. Concurrency int + // LocalAddr is the local address to use when dialing an + // address. + // If nil, a local address is automatically chosen. + LocalAddr *net.TCPAddr + // This may be used to override DNS resolving policy, like this: // var dialer = &fasthttp.TCPDialer{ // Resolver: &net.Resolver{ @@ -284,7 +289,7 @@ func (d *TCPDialer) dial(addr string, dualStack bool, timeout time.Duration) (ne n := uint32(len(addrs)) deadline := time.Now().Add(timeout) for n > 0 { - conn, err = tryDial(network, &addrs[idx%n], deadline, d.concurrencyCh) + conn, err = d.tryDial(network, &addrs[idx%n], deadline, d.concurrencyCh) if err == nil { return conn, nil } @@ -297,7 +302,7 @@ func (d *TCPDialer) dial(addr string, dualStack bool, timeout time.Duration) (ne return nil, err } -func tryDial(network string, addr *net.TCPAddr, deadline time.Time, concurrencyCh chan struct{}) (net.Conn, error) { +func (d *TCPDialer) tryDial(network string, addr *net.TCPAddr, deadline time.Time, concurrencyCh chan struct{}) (net.Conn, error) { timeout := -time.Since(deadline) if timeout <= 0 { return nil, ErrDialTimeout @@ -328,7 +333,7 @@ func tryDial(network string, addr *net.TCPAddr, deadline time.Time, concurrencyC ch := chv.(chan dialResult) go func() { var dr dialResult - dr.conn, dr.err = net.DialTCP(network, nil, addr) + dr.conn, dr.err = net.DialTCP(network, d.LocalAddr, addr) ch <- dr if concurrencyCh != nil { <-concurrencyCh |