From 245f073350951b0806800fa815f778bbaff8d4f7 Mon Sep 17 00:00:00 2001 From: yusing Date: Fri, 4 Oct 2024 07:13:52 +0800 Subject: [PATCH] tuned some http settings, refactor --- internal/{common/http.go => net/http/common.go} | 13 ++++++++----- internal/net/http/middleware/custom_error_page.go | 5 ++--- internal/route/http.go | 5 ++--- 3 files changed, 12 insertions(+), 11 deletions(-) rename internal/{common/http.go => net/http/common.go} (58%) diff --git a/internal/common/http.go b/internal/net/http/common.go similarity index 58% rename from internal/common/http.go rename to internal/net/http/common.go index d2bfde7..cfce0e5 100644 --- a/internal/common/http.go +++ b/internal/net/http/common.go @@ -1,4 +1,4 @@ -package common +package http import ( "crypto/tls" @@ -13,10 +13,13 @@ var ( KeepAlive: 60 * time.Second, } DefaultTransport = &http.Transport{ - Proxy: http.ProxyFromEnvironment, - DialContext: defaultDialer.DialContext, - MaxIdleConnsPerHost: 1000, - IdleConnTimeout: 90 * time.Second, + Proxy: http.ProxyFromEnvironment, + DialContext: defaultDialer.DialContext, + ForceAttemptHTTP2: true, + MaxIdleConns: 100, + MaxIdleConnsPerHost: 10, + TLSHandshakeTimeout: 10 * time.Second, + ExpectContinueTimeout: 1 * time.Second, } DefaultTransportNoTLS = func() *http.Transport { var clone = DefaultTransport.Clone() diff --git a/internal/net/http/middleware/custom_error_page.go b/internal/net/http/middleware/custom_error_page.go index 95db5da..632fddb 100644 --- a/internal/net/http/middleware/custom_error_page.go +++ b/internal/net/http/middleware/custom_error_page.go @@ -10,7 +10,6 @@ import ( "github.com/sirupsen/logrus" "github.com/yusing/go-proxy/internal/api/v1/error_page" - "github.com/yusing/go-proxy/internal/common" gpHTTP "github.com/yusing/go-proxy/internal/net/http" ) @@ -47,8 +46,8 @@ func ServeStaticErrorPageFile(w http.ResponseWriter, r *http.Request) bool { if path != "" && path[0] != '/' { path = "/" + path } - if strings.HasPrefix(path, common.StaticFilePathPrefix) { - filename := path[len(common.StaticFilePathPrefix):] + if strings.HasPrefix(path, gpHTTP.StaticFilePathPrefix) { + filename := path[len(gpHTTP.StaticFilePathPrefix):] file, ok := error_page.GetStaticFile(filename) if !ok { errPageLogger.Errorf("unable to load resource %s", filename) diff --git a/internal/route/http.go b/internal/route/http.go index ac63673..7d99919 100755 --- a/internal/route/http.go +++ b/internal/route/http.go @@ -10,7 +10,6 @@ import ( "github.com/sirupsen/logrus" "github.com/yusing/go-proxy/internal/api/v1/error_page" - "github.com/yusing/go-proxy/internal/common" "github.com/yusing/go-proxy/internal/docker/idlewatcher" E "github.com/yusing/go-proxy/internal/error" . "github.com/yusing/go-proxy/internal/net/http" @@ -68,9 +67,9 @@ func NewHTTPRoute(entry *P.ReverseProxyEntry) (*HTTPRoute, E.NestedError) { var unregIdleWatcher func() if entry.NoTLSVerify { - trans = common.DefaultTransportNoTLS.Clone() + trans = DefaultTransportNoTLS.Clone() } else { - trans = common.DefaultTransport.Clone() + trans = DefaultTransport.Clone() } rp := NewReverseProxy(entry.URL, trans)