From 17bc0c332130cc260906e3faa5899400f5a49933 Mon Sep 17 00:00:00 2001 From: FireMasterK <20838718+FireMasterK@users.noreply.github.com> Date: Fri, 12 Mar 2021 12:29:53 +0530 Subject: [PATCH] Add request timeouts. --- main.go | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index d1df008..e47f24d 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( "os" "strings" "syscall" + "time" "github.com/lucas-clemente/quic-go/http3" ) @@ -20,7 +21,18 @@ var h3client = &http.Client{ } // http/2 client -var h2client = &http.Client{} +var h2client = &http.Client{ + Transport: &http.Transport{ + Dial: (&net.Dialer{ + Timeout: 30 * time.Second, + KeepAlive: 30 * time.Second, + }).Dial, + TLSHandshakeTimeout: 10 * time.Second, + ResponseHeaderTimeout: 10 * time.Second, + ExpectContinueTimeout: 1 * time.Second, + IdleConnTimeout: 120 * time.Second, + }, +} // user agent to use var ua = "Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101" @@ -141,12 +153,18 @@ func main() { socket := "socket" + string(os.PathSeparator) + "http-proxy.sock" syscall.Unlink(socket) listener, err := net.Listen("unix", socket) + srv := &http.Server{ + ReadTimeout: 5 * time.Second, + WriteTimeout: 10 * time.Second, + Addr: ":8080", + Handler: &requesthandler{}, + } if err != nil { fmt.Println("Failed to bind to UDS, falling back to TCP/IP") fmt.Println(err.Error()) - http.ListenAndServe(":8080", &requesthandler{}) + srv.ListenAndServe() } else { defer listener.Close() - http.Serve(listener, &requesthandler{}) + srv.Serve(listener) } }