This repository has been archived by the owner on May 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from brave/faster-proxy
Make nitriding's reverse proxy faster.
- Loading branch information
Showing
4 changed files
with
62 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package nitriding | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
// bufSize represents the buffer size for our reverse proxy. It's identical to | ||
// the buffer size used in Go's reverse proxy implementation: | ||
// https://cs.opensource.google/go/go/+/refs/tags/go1.20.3:src/net/http/httputil/reverseproxy.go;l=634 | ||
const bufSize = 32 * 1024 | ||
|
||
// bufPool implements the httputil.BufferPool interface. The implementation is | ||
// based on sync.Pool. | ||
type bufPool struct { | ||
sync.Pool | ||
} | ||
|
||
func newBufPool() *bufPool { | ||
return &bufPool{ | ||
Pool: sync.Pool{ | ||
New: func() any { | ||
// The Pool's New function should generally only return pointer | ||
// types, since a pointer can be put into the return interface | ||
// value without an allocation: | ||
s := make([]byte, bufSize) | ||
return &s | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (p *bufPool) Get() []byte { | ||
s := p.Pool.Get() | ||
return *s.(*[]byte) | ||
} | ||
|
||
func (p *bufPool) Put(buf []byte) { | ||
p.Pool.Put(&buf) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package nitriding | ||
|
||
import "testing" | ||
|
||
func TestBufPool(t *testing.T) { | ||
p := newBufPool() | ||
s1 := p.Get() | ||
p.Put(s1) | ||
s2 := p.Get() | ||
|
||
if len(s1) != len(s2) { | ||
t.Fatalf("Byte slices have different lengths (%d vs %d).", len(s1), len(s2)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters