Skip to content

Commit 1eaca30

Browse files
re-use proxy buffers and also use 128K higher than 32K default (#78)
1 parent 5ed7703 commit 1eaca30

File tree

6 files changed

+38
-9
lines changed

6 files changed

+38
-9
lines changed

.github/workflows/go.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
runs-on: ${{ matrix.os }}
1515
strategy:
1616
matrix:
17-
go-version: [1.19.x, 1.20.x]
17+
go-version: [1.20.x]
1818
os: [ubuntu-latest]
1919
steps:
2020
- name: Set up Go ${{ matrix.go-version }} on ${{ matrix.os }}

.github/workflows/vulncheck.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
runs-on: ubuntu-latest
1515
strategy:
1616
matrix:
17-
go-version: [ 1.19.x, 1.20.x ]
17+
go-version: [ 1.20.x ]
1818
steps:
1919
- name: Check out code into the Go module directory
2020
uses: actions/checkout@v3

cache.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ func isFresh(cacheCC, reqCC *cacheControl, lastModified time.Time) bool {
549549
return freshCache && freshReq
550550
}
551551

552-
func cacheHandler(w http.ResponseWriter, r *http.Request, b *Backend) http.HandlerFunc {
552+
func cacheHandler(b *Backend) http.HandlerFunc {
553553
return func(w http.ResponseWriter, r *http.Request) {
554554
clnt := b.cacheClient
555555
if clnt == nil || !clnt.isCacheable(r.Method) || !clnt.isOnline() {

main.go

+33-4
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ type BackendStats struct {
193193

194194
// ErrorHandler called by httputil.ReverseProxy for errors.
195195
// Avoid canceled context error since it means the client disconnected.
196-
func (b *Backend) ErrorHandler(w http.ResponseWriter, r *http.Request, err error) {
196+
func (b *Backend) ErrorHandler(_ http.ResponseWriter, _ *http.Request, err error) {
197197
if err != nil && !errors.Is(err, context.Canceled) {
198198
if globalLoggingEnabled {
199199
logMsg(logMessage{Endpoint: b.endpoint, Status: "down", Error: err})
@@ -418,7 +418,7 @@ func (s *site) ServeHTTP(w http.ResponseWriter, r *http.Request) {
418418
if backend != nil && backend.Online() {
419419
cacheHandlerFn := func(w http.ResponseWriter, r *http.Request) {
420420
if backend.cacheClient != nil {
421-
cacheHandler(w, r, backend)(w, r)
421+
cacheHandler(backend)(w, r)
422422
} else {
423423
backend.proxy.ServeHTTP(w, r)
424424
}
@@ -589,6 +589,34 @@ func checkMain(ctx *cli.Context) {
589589
}
590590
}
591591

592+
func modifyResponse() func(*http.Response) error {
593+
return func(resp *http.Response) error {
594+
resp.Header.Set("X-Proxy", "true")
595+
return nil
596+
}
597+
}
598+
599+
type bufPool struct {
600+
pool sync.Pool
601+
}
602+
603+
func (b *bufPool) Put(buf []byte) {
604+
b.pool.Put(buf)
605+
}
606+
607+
func (b *bufPool) Get() []byte {
608+
return b.pool.Get().([]byte)
609+
}
610+
611+
func newBufPool(sz int) httputil.BufferPool {
612+
return &bufPool{pool: sync.Pool{
613+
New: func() interface{} {
614+
buf := make([]byte, sz)
615+
return buf
616+
},
617+
}}
618+
}
619+
592620
func configureSite(ctx *cli.Context, siteNum int, siteStrs []string, healthCheckPath string, healthCheckPort int, healthCheckDuration time.Duration) *site {
593621
var endpoints []string
594622

@@ -649,9 +677,10 @@ func configureSite(ctx *cli.Context, siteNum int, siteStrs []string, healthCheck
649677
r.URL.Scheme = target.Scheme
650678
r.URL.Host = target.Host
651679
},
652-
Transport: transport,
680+
Transport: transport,
681+
BufferPool: newBufPool(128 << 10),
682+
ModifyResponse: modifyResponse(),
653683
}
654-
655684
stats := BackendStats{MinLatency: 24 * time.Hour, MaxLatency: 0}
656685
healthCheckURL, err := getHealthCheckURL(endpoint, healthCheckPath, healthCheckPort)
657686
if err != nil {

main_linux.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"golang.org/x/sys/unix"
2525
)
2626

27-
func setTCPParameters(network, address string, c syscall.RawConn) error {
27+
func setTCPParameters(_, _ string, c syscall.RawConn) error {
2828
c.Control(func(fdPtr uintptr) {
2929
// got socket file descriptor to set parameters.
3030
fd := int(fdPtr)

main_others.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ package main
2020

2121
import "syscall"
2222

23-
func setTCPParameters(network, address string, c syscall.RawConn) error {
23+
func setTCPParameters(_, _ string, _ syscall.RawConn) error {
2424
return nil
2525
}

0 commit comments

Comments
 (0)