From 2f10a4b33edf382d5c475b15819ec7856c851e71 Mon Sep 17 00:00:00 2001 From: eleksir Date: Thu, 1 Aug 2024 01:15:34 +0300 Subject: [PATCH] Add timeouts to reverse proxy and to http server, reduce timeout in config --- joyproxy_sample.ini | 2 +- main.go | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/joyproxy_sample.ini b/joyproxy_sample.ini index 7c18a7c..be25d13 100644 --- a/joyproxy_sample.ini +++ b/joyproxy_sample.ini @@ -6,7 +6,7 @@ proto = http port = 9980 ; timeout for upstream connection -timeout = 30 +timeout = 5 ; loglevel - info or debug, info is what you want loglevel = debug diff --git a/main.go b/main.go index 3cd898e..e292b7d 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "fmt" "html" "math/rand" + "net" "net/http" "net/http/httputil" "net/url" @@ -227,6 +228,13 @@ func joyproxyHandler(w http.ResponseWriter, r *http.Request) { } joyproxy := httputil.NewSingleHostReverseProxy(target) + joyproxy.Transport = &http.Transport{ + Dial: (&net.Dialer{ + Timeout: time.Duration(cfg.timeout) * time.Second, + KeepAlive: time.Duration(cfg.timeout) * time.Second, + }).Dial, + TLSHandshakeTimeout: time.Duration(cfg.timeout) * time.Second, + } joyproxy.ServeHTTP(w, dlReq) } @@ -307,7 +315,10 @@ func main() { // timeout and sits here forever. server := &http.Server{ Addr: cfg.port, - ReadHeaderTimeout: 3 * time.Second, + ReadHeaderTimeout: time.Duration(cfg.timeout) * time.Second, + ReadTimeout: time.Duration(cfg.timeout) * time.Second, + WriteTimeout: time.Duration(cfg.timeout) * time.Second, + IdleTimeout: time.Duration(cfg.timeout) * time.Second, } log.Warn(server.ListenAndServe())