0

Add timeouts to reverse proxy and to http server, reduce timeout in config

This commit is contained in:
eleksir
2024-08-01 01:15:34 +03:00
parent 67c56388fb
commit 2f10a4b33e
2 changed files with 13 additions and 2 deletions

View File

@@ -6,7 +6,7 @@ proto = http
port = 9980 port = 9980
; timeout for upstream connection ; timeout for upstream connection
timeout = 30 timeout = 5
; loglevel - info or debug, info is what you want ; loglevel - info or debug, info is what you want
loglevel = debug loglevel = debug

13
main.go
View File

@@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"html" "html"
"math/rand" "math/rand"
"net"
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
"net/url" "net/url"
@@ -227,6 +228,13 @@ func joyproxyHandler(w http.ResponseWriter, r *http.Request) {
} }
joyproxy := httputil.NewSingleHostReverseProxy(target) 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) joyproxy.ServeHTTP(w, dlReq)
} }
@@ -307,7 +315,10 @@ func main() {
// timeout and sits here forever. // timeout and sits here forever.
server := &http.Server{ server := &http.Server{
Addr: cfg.port, 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()) log.Warn(server.ListenAndServe())