From a849603d8becb6dd976039d93192048dfba89cb8 Mon Sep 17 00:00:00 2001 From: ahmetlutfu Date: Wed, 6 Dec 2023 23:07:52 +0300 Subject: [PATCH 1/2] added timeout for http client --- handlers/proxy.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/handlers/proxy.go b/handlers/proxy.go index bedfbde..1668216 100644 --- a/handlers/proxy.go +++ b/handlers/proxy.go @@ -9,6 +9,7 @@ import ( "os" "regexp" "strings" + "time" "ladder/pkg/ruleset" @@ -181,7 +182,9 @@ func fetchSite(urlpath string, queries map[string]string) (string, *http.Request } // Fetch the site - client := &http.Client{} + client := &http.Client{ + Timeout: time.Second * 15, + } req, _ := http.NewRequest("GET", url, nil) if rule.Headers.UserAgent != "" { From 92b52332571c8828a1acaed20cce609fd6cd5085 Mon Sep 17 00:00:00 2001 From: ahmetlutfu Date: Wed, 6 Dec 2023 23:11:58 +0300 Subject: [PATCH 2/2] overriding default timeout via HTTP_TIMEOUT env variable. --- handlers/proxy.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/handlers/proxy.go b/handlers/proxy.go index 1668216..e73c1a4 100644 --- a/handlers/proxy.go +++ b/handlers/proxy.go @@ -8,6 +8,7 @@ import ( "net/url" "os" "regexp" + "strconv" "strings" "time" @@ -22,6 +23,7 @@ var ( ForwardedFor = getenv("X_FORWARDED_FOR", "66.249.66.1") rulesSet = ruleset.NewRulesetFromEnv() allowedDomains = []string{} + defaultTimeout = 15 // in seconds ) func init() { @@ -29,6 +31,9 @@ func init() { if os.Getenv("ALLOWED_DOMAINS_RULESET") == "true" { allowedDomains = append(allowedDomains, rulesSet.Domains()...) } + if timeoutStr := os.Getenv("HTTP_TIMEOUT"); timeoutStr != "" { + defaultTimeout, _ = strconv.Atoi(timeoutStr) + } } // extracts a URL from the request ctx. If the URL in the request @@ -183,7 +188,7 @@ func fetchSite(urlpath string, queries map[string]string) (string, *http.Request // Fetch the site client := &http.Client{ - Timeout: time.Second * 15, + Timeout: time.Second * time.Duration(defaultTimeout), } req, _ := http.NewRequest("GET", url, nil)