mirror of
https://github.com/fosrl/gerbil.git
synced 2026-05-20 15:19:53 +00:00
Merge pull request #63 from fosrl/proxy-perf-improvements
perf(proxy): Add HTTP client reuse and buffer pooling
This commit is contained in:
@@ -70,6 +70,12 @@ type SNIProxy struct {
|
|||||||
|
|
||||||
// Trusted upstream proxies that can send PROXY protocol
|
// Trusted upstream proxies that can send PROXY protocol
|
||||||
trustedUpstreams map[string]struct{}
|
trustedUpstreams map[string]struct{}
|
||||||
|
|
||||||
|
// Reusable HTTP client for API requests
|
||||||
|
httpClient *http.Client
|
||||||
|
|
||||||
|
// Buffer pool for connection piping
|
||||||
|
bufferPool *sync.Pool
|
||||||
}
|
}
|
||||||
|
|
||||||
type activeTunnel struct {
|
type activeTunnel struct {
|
||||||
@@ -375,6 +381,20 @@ func NewSNIProxy(port int, remoteConfigURL, publicKey, localProxyAddr string, lo
|
|||||||
localOverrides: overridesMap,
|
localOverrides: overridesMap,
|
||||||
activeTunnels: make(map[string]*activeTunnel),
|
activeTunnels: make(map[string]*activeTunnel),
|
||||||
trustedUpstreams: trustedMap,
|
trustedUpstreams: trustedMap,
|
||||||
|
httpClient: &http.Client{
|
||||||
|
Timeout: 5 * time.Second,
|
||||||
|
Transport: &http.Transport{
|
||||||
|
MaxIdleConns: 100,
|
||||||
|
MaxIdleConnsPerHost: 10,
|
||||||
|
IdleConnTimeout: 90 * time.Second,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
bufferPool: &sync.Pool{
|
||||||
|
New: func() interface{} {
|
||||||
|
buf := make([]byte, 32*1024)
|
||||||
|
return &buf
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
return proxy, nil
|
return proxy, nil
|
||||||
@@ -697,8 +717,8 @@ func (p *SNIProxy) getRoute(hostname, clientAddr string) (*RouteRecord, error) {
|
|||||||
|
|
||||||
// Make HTTP request
|
// Make HTTP request
|
||||||
apiStart := time.Now()
|
apiStart := time.Now()
|
||||||
client := &http.Client{Timeout: 5 * time.Second}
|
// Make HTTP request using reusable client
|
||||||
resp, err := client.Do(req)
|
resp, err := p.httpClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
metrics.RecordSNIRouteAPIRequest("error")
|
metrics.RecordSNIRouteAPIRequest("error")
|
||||||
return nil, fmt.Errorf("API request failed: %w", err)
|
return nil, fmt.Errorf("API request failed: %w", err)
|
||||||
@@ -793,9 +813,15 @@ func (p *SNIProxy) pipe(hostname string, clientConn, targetConn net.Conn, client
|
|||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
defer closeConns()
|
defer closeConns()
|
||||||
|
|
||||||
// Use a large buffer for better performance
|
// Get buffer from pool and return when done
|
||||||
buf := make([]byte, 32*1024)
|
bufPtr := p.bufferPool.Get().(*[]byte)
|
||||||
bytesCopied, err := io.CopyBuffer(targetConn, clientReader, buf)
|
defer func() {
|
||||||
|
// Clear buffer before returning to pool to prevent data leakage
|
||||||
|
clear(*bufPtr)
|
||||||
|
p.bufferPool.Put(bufPtr)
|
||||||
|
}()
|
||||||
|
|
||||||
|
bytesCopied, err := io.CopyBuffer(targetConn, clientReader, *bufPtr)
|
||||||
metrics.RecordProxyBytesTransmitted(hostname, "client_to_target", bytesCopied)
|
metrics.RecordProxyBytesTransmitted(hostname, "client_to_target", bytesCopied)
|
||||||
if err != nil && err != io.EOF {
|
if err != nil && err != io.EOF {
|
||||||
logger.Debug("Copy client->target error: %v", err)
|
logger.Debug("Copy client->target error: %v", err)
|
||||||
@@ -807,9 +833,15 @@ func (p *SNIProxy) pipe(hostname string, clientConn, targetConn net.Conn, client
|
|||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
defer closeConns()
|
defer closeConns()
|
||||||
|
|
||||||
// Use a large buffer for better performance
|
// Get buffer from pool and return when done
|
||||||
buf := make([]byte, 32*1024)
|
bufPtr := p.bufferPool.Get().(*[]byte)
|
||||||
bytesCopied, err := io.CopyBuffer(clientConn, targetConn, buf)
|
defer func() {
|
||||||
|
// Clear buffer before returning to pool to prevent data leakage
|
||||||
|
clear(*bufPtr)
|
||||||
|
p.bufferPool.Put(bufPtr)
|
||||||
|
}()
|
||||||
|
|
||||||
|
bytesCopied, err := io.CopyBuffer(clientConn, targetConn, *bufPtr)
|
||||||
metrics.RecordProxyBytesTransmitted(hostname, "target_to_client", bytesCopied)
|
metrics.RecordProxyBytesTransmitted(hostname, "target_to_client", bytesCopied)
|
||||||
if err != nil && err != io.EOF {
|
if err != nil && err != io.EOF {
|
||||||
logger.Debug("Copy target->client error: %v", err)
|
logger.Debug("Copy target->client error: %v", err)
|
||||||
|
|||||||
Reference in New Issue
Block a user