aboutsummaryrefslogtreecommitdiff
path: root/fasthttpproxy
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 /fasthttpproxy
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 'fasthttpproxy')
-rw-r--r--fasthttpproxy/http.go20
1 files changed, 19 insertions, 1 deletions
diff --git a/fasthttpproxy/http.go b/fasthttpproxy/http.go
index 62de1d0..c838d58 100644
--- a/fasthttpproxy/http.go
+++ b/fasthttpproxy/http.go
@@ -6,6 +6,7 @@ import (
"fmt"
"net"
"strings"
+ "time"
"github.com/valyala/fasthttp"
)
@@ -18,6 +19,17 @@ import (
// Dial: fasthttpproxy.FasthttpHTTPDialer("username:password@localhost:9050"),
// }
func FasthttpHTTPDialer(proxy string) fasthttp.DialFunc {
+ return FasthttpHTTPDialerTimeout(proxy, 0)
+}
+
+// FasthttpHTTPDialer returns a fasthttp.DialFunc that dials using
+// the provided HTTP proxy using the given timeout.
+//
+// Example usage:
+// c := &fasthttp.Client{
+// Dial: fasthttpproxy.FasthttpHTTPDialerTimeout("username:password@localhost:9050", time.Second * 2),
+// }
+func FasthttpHTTPDialerTimeout(proxy string, timeout time.Duration) fasthttp.DialFunc {
var auth string
if strings.Contains(proxy, "@") {
split := strings.Split(proxy, "@")
@@ -26,7 +38,13 @@ func FasthttpHTTPDialer(proxy string) fasthttp.DialFunc {
}
return func(addr string) (net.Conn, error) {
- conn, err := fasthttp.Dial(proxy)
+ var conn net.Conn
+ var err error
+ if timeout == 0 {
+ conn, err = fasthttp.Dial(proxy)
+ } else {
+ conn, err = fasthttp.DialTimeout(proxy, timeout)
+ }
if err != nil {
return nil, err
}