Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

re-use proxy buffers and also use 128K higher than 32K default #78

Merged
merged 1 commit into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/vulncheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
37 changes: 33 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion main_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion main_others.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}