aboutsummaryrefslogtreecommitdiff
path: root/tcpdialer.go
diff options
context:
space:
mode:
authorGravatar Vitali Pikulik <v.pikulik@gmail.com> 2020-10-03 09:44:36 +0200
committerGravatar GitHub <noreply@github.com> 2020-10-03 09:44:36 +0200
commit56775f4d9fa114edf7f860abfe95294b58e61020 (patch)
tree701928c174cffb3d1c519626b5e3d48ce88455f1 /tcpdialer.go
parentBrotli support in FS handler. (#880) (diff)
downloadfasthttp-56775f4d9fa114edf7f860abfe95294b58e61020.tar.gz
fasthttp-56775f4d9fa114edf7f860abfe95294b58e61020.tar.bz2
fasthttp-56775f4d9fa114edf7f860abfe95294b58e61020.zip
tryDial timeout (#881)
* Change tryDial to handle timeout correctly * fasthttpproxy/http.go accepts timeout * Fix example doc Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com> * Improve import in httpproxy * Simplify tryDial * Cleanup * Wait for concurrencyCh Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com>
Diffstat (limited to 'tcpdialer.go')
-rw-r--r--tcpdialer.go43
1 files changed, 7 insertions, 36 deletions
diff --git a/tcpdialer.go b/tcpdialer.go
index b51bb21..8d6bbfb 100644
--- a/tcpdialer.go
+++ b/tcpdialer.go
@@ -324,48 +324,19 @@ func (d *TCPDialer) tryDial(network string, addr *net.TCPAddr, deadline time.Tim
return nil, ErrDialTimeout
}
}
+ defer func() { <-concurrencyCh }()
}
- chv := dialResultChanPool.Get()
- if chv == nil {
- chv = make(chan dialResult, 1)
- }
- ch := chv.(chan dialResult)
- go func() {
- var dr dialResult
- dr.conn, dr.err = net.DialTCP(network, d.LocalAddr, addr)
- ch <- dr
- if concurrencyCh != nil {
- <-concurrencyCh
- }
- }()
-
- var (
- conn net.Conn
- err error
- )
-
- tc := AcquireTimer(timeout)
- select {
- case dr := <-ch:
- conn = dr.conn
- err = dr.err
- dialResultChanPool.Put(ch)
- case <-tc.C:
- err = ErrDialTimeout
+ dialer := net.Dialer{LocalAddr: d.LocalAddr}
+ ctx, cancel_ctx := context.WithDeadline(context.Background(), deadline)
+ defer cancel_ctx()
+ conn, err := dialer.DialContext(ctx, network, addr.String())
+ if err != nil && ctx.Err() == context.DeadlineExceeded {
+ return nil, ErrDialTimeout
}
- ReleaseTimer(tc)
-
return conn, err
}
-var dialResultChanPool sync.Pool
-
-type dialResult struct {
- conn net.Conn
- err error
-}
-
// ErrDialTimeout is returned when TCP dialing is timed out.
var ErrDialTimeout = errors.New("dialing to the given TCP address timed out")