From 1eaca304fbd2b4dbcf8ebf59096e9d00be64be0e Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Tue, 21 Mar 2023 13:24:51 -0700 Subject: [PATCH] re-use proxy buffers and also use 128K higher than 32K default (#78) --- .github/workflows/go.yml | 2 +- .github/workflows/vulncheck.yml | 2 +- cache.go | 2 +- main.go | 37 +++++++++++++++++++++++++++++---- main_linux.go | 2 +- main_others.go | 2 +- 6 files changed, 38 insertions(+), 9 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 68732ae..25ec678 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -14,7 +14,7 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - go-version: [1.19.x, 1.20.x] + go-version: [1.20.x] os: [ubuntu-latest] steps: - name: Set up Go ${{ matrix.go-version }} on ${{ matrix.os }} diff --git a/.github/workflows/vulncheck.yml b/.github/workflows/vulncheck.yml index ff423bb..1d55f91 100644 --- a/.github/workflows/vulncheck.yml +++ b/.github/workflows/vulncheck.yml @@ -14,7 +14,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - go-version: [ 1.19.x, 1.20.x ] + go-version: [ 1.20.x ] steps: - name: Check out code into the Go module directory uses: actions/checkout@v3 diff --git a/cache.go b/cache.go index 8c4c639..4a37aef 100644 --- a/cache.go +++ b/cache.go @@ -549,7 +549,7 @@ func isFresh(cacheCC, reqCC *cacheControl, lastModified time.Time) bool { return freshCache && freshReq } -func cacheHandler(w http.ResponseWriter, r *http.Request, b *Backend) http.HandlerFunc { +func cacheHandler(b *Backend) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { clnt := b.cacheClient if clnt == nil || !clnt.isCacheable(r.Method) || !clnt.isOnline() { diff --git a/main.go b/main.go index 57dfd68..2762878 100644 --- a/main.go +++ b/main.go @@ -193,7 +193,7 @@ type BackendStats struct { // ErrorHandler called by httputil.ReverseProxy for errors. // Avoid canceled context error since it means the client disconnected. -func (b *Backend) ErrorHandler(w http.ResponseWriter, r *http.Request, err error) { +func (b *Backend) ErrorHandler(_ http.ResponseWriter, _ *http.Request, err error) { if err != nil && !errors.Is(err, context.Canceled) { if globalLoggingEnabled { logMsg(logMessage{Endpoint: b.endpoint, Status: "down", Error: err}) @@ -418,7 +418,7 @@ func (s *site) ServeHTTP(w http.ResponseWriter, r *http.Request) { if backend != nil && backend.Online() { cacheHandlerFn := func(w http.ResponseWriter, r *http.Request) { if backend.cacheClient != nil { - cacheHandler(w, r, backend)(w, r) + cacheHandler(backend)(w, r) } else { backend.proxy.ServeHTTP(w, r) } @@ -589,6 +589,34 @@ func checkMain(ctx *cli.Context) { } } +func modifyResponse() func(*http.Response) error { + return func(resp *http.Response) error { + resp.Header.Set("X-Proxy", "true") + return nil + } +} + +type bufPool struct { + pool sync.Pool +} + +func (b *bufPool) Put(buf []byte) { + b.pool.Put(buf) +} + +func (b *bufPool) Get() []byte { + return b.pool.Get().([]byte) +} + +func newBufPool(sz int) httputil.BufferPool { + return &bufPool{pool: sync.Pool{ + New: func() interface{} { + buf := make([]byte, sz) + return buf + }, + }} +} + func configureSite(ctx *cli.Context, siteNum int, siteStrs []string, healthCheckPath string, healthCheckPort int, healthCheckDuration time.Duration) *site { var endpoints []string @@ -649,9 +677,10 @@ func configureSite(ctx *cli.Context, siteNum int, siteStrs []string, healthCheck r.URL.Scheme = target.Scheme r.URL.Host = target.Host }, - Transport: transport, + Transport: transport, + BufferPool: newBufPool(128 << 10), + ModifyResponse: modifyResponse(), } - stats := BackendStats{MinLatency: 24 * time.Hour, MaxLatency: 0} healthCheckURL, err := getHealthCheckURL(endpoint, healthCheckPath, healthCheckPort) if err != nil { diff --git a/main_linux.go b/main_linux.go index abbf958..9f69195 100644 --- a/main_linux.go +++ b/main_linux.go @@ -24,7 +24,7 @@ import ( "golang.org/x/sys/unix" ) -func setTCPParameters(network, address string, c syscall.RawConn) error { +func setTCPParameters(_, _ string, c syscall.RawConn) error { c.Control(func(fdPtr uintptr) { // got socket file descriptor to set parameters. fd := int(fdPtr) diff --git a/main_others.go b/main_others.go index 92ab083..ad4a06d 100644 --- a/main_others.go +++ b/main_others.go @@ -20,6 +20,6 @@ package main import "syscall" -func setTCPParameters(network, address string, c syscall.RawConn) error { +func setTCPParameters(_, _ string, _ syscall.RawConn) error { return nil }