Skip to content

Commit

Permalink
re-use proxy buffers and also use 128K higher than 32K default (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
harshavardhana authored Mar 21, 2023
1 parent 5ed7703 commit 1eaca30
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 9 deletions.
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
}

0 comments on commit 1eaca30

Please sign in to comment.