fixup! Update go.mod and go.sum (#48)

This commit is contained in:
2026-02-20 15:39:27 +00:00
parent 0aaf2dc78c
commit 8e5eaab0af
43 changed files with 5271 additions and 129 deletions
+270
View File
@@ -0,0 +1,270 @@
package httplog
import (
"bytes"
"encoding/json"
"io"
"net/http"
"testing"
)
// BenchmarkLoggerLog benchmarks the Log function with sync.Pool
func BenchmarkLoggerLog(b *testing.B) {
l := &Logger{
forwardID: "benchmark",
maxBodyLen: 1024,
output: io.Discard,
}
entry := Entry{
Direction: "request",
RequestID: "req-123",
Method: "POST",
Path: "/api/users",
BodySize: 256,
Body: `{"name":"test user","email":"test@example.com","data":"some payload data here"}`,
StatusCode: 200,
LatencyMs: 42,
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = l.Log(entry)
}
}
// BenchmarkLoggerLogNoPool simulates logging without sync.Pool
func BenchmarkLoggerLogNoPool(b *testing.B) {
l := &Logger{
forwardID: "benchmark",
maxBodyLen: 1024,
output: io.Discard,
}
entry := Entry{
Direction: "request",
RequestID: "req-123",
Method: "POST",
Path: "/api/users",
BodySize: 256,
Body: `{"name":"test user","email":"test@example.com","data":"some payload data here"}`,
StatusCode: 200,
LatencyMs: 42,
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
// Simulate old behavior: allocate new buffer each time
data, _ := json.Marshal(entry)
_, _ = l.output.Write(append(data, '\n'))
}
}
// BenchmarkReadBodyLimited benchmarks reading body with sync.Pool
func BenchmarkReadBodyLimited(b *testing.B) {
bodyData := bytes.Repeat([]byte("a"), 1024)
transport := &loggingTransport{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
// Create a new ReadCloser for each iteration
body := io.NopCloser(bytes.NewReader(bodyData))
_, _ = transport.readBodyLimited(body, 2048)
}
}
// BenchmarkReadBodyLimitedSmall benchmarks with small bodies (typical API requests)
func BenchmarkReadBodyLimitedSmall(b *testing.B) {
bodyData := []byte(`{"id":123,"name":"test","active":true}`)
transport := &loggingTransport{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
body := io.NopCloser(bytes.NewReader(bodyData))
_, _ = transport.readBodyLimited(body, 1024)
}
}
// BenchmarkReadBodyLimitedLarge benchmarks with large bodies
func BenchmarkReadBodyLimitedLarge(b *testing.B) {
bodyData := bytes.Repeat([]byte("x"), 65536) // 64KB
transport := &loggingTransport{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
body := io.NopCloser(bytes.NewReader(bodyData))
_, _ = transport.readBodyLimited(body, 65536)
}
}
// BenchmarkBufferPoolGetPut benchmarks the buffer pool itself
func BenchmarkBufferPoolGetPut(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
bufPtr := bufferPool.Get().(*[]byte)
// Reset and use the buffer to simulate real usage
*bufPtr = (*bufPtr)[:0]
*bufPtr = append(*bufPtr, "test data..."...)
bufferPool.Put(bufPtr)
}
})
}
// BenchmarkLogBufferPoolGetPut benchmarks the log buffer pool
func BenchmarkLogBufferPoolGetPut(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
buf := logBufferPool.Get().(*bytes.Buffer)
buf.Reset()
buf.WriteString("test log entry")
logBufferPool.Put(buf)
}
})
}
// BenchmarkFlattenHeaders benchmarks header flattening with pooling
func BenchmarkFlattenHeaders(b *testing.B) {
headers := http.Header{
"Content-Type": []string{"application/json"},
"Accept": []string{"text/html", "application/json"},
"User-Agent": []string{"test-client/1.0"},
"X-Request-ID": []string{"abc-123-def"},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = flattenHeaders(headers)
}
}
// BenchmarkTruncateBody benchmarks body truncation with pooled buffers
func BenchmarkTruncateBody(b *testing.B) {
body := "this is a very long body that should be truncated for logging purposes"
maxLen := 20
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = truncateBody(body, maxLen)
}
}
// BenchmarkTruncateBodyNoPool simulates truncation without pooling
func BenchmarkTruncateBodyNoPool(b *testing.B) {
body := "this is a very long body that should be truncated for logging purposes"
maxLen := 20
b.ResetTimer()
for i := 0; i < b.N; i++ {
if len(body) > maxLen {
_ = body[:maxLen] + "...(truncated)"
}
}
}
// BenchmarkLoggerLogWithTruncation benchmarks logging with body truncation
func BenchmarkLoggerLogWithTruncation(b *testing.B) {
l := &Logger{
forwardID: "benchmark",
maxBodyLen: 50,
output: io.Discard,
}
entry := Entry{
Direction: "request",
RequestID: "req-123",
Method: "POST",
Path: "/api/users",
Body: `{"name":"test user","email":"test@example.com","data":"some payload data here for truncation"}`,
BodySize: 100,
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = l.Log(entry)
}
}
// BenchmarkReadBufferPool benchmarks the read buffer pool
func BenchmarkReadBufferPool(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
bufPtr := readBufferPool.Get().(*[]byte)
buf := *bufPtr
_ = len(buf) // Use the buffer
readBufferPool.Put(bufPtr)
}
})
}
// BenchmarkReadBodyLimitedParallel benchmarks body reading under concurrent load
func BenchmarkReadBodyLimitedParallel(b *testing.B) {
bodyData := bytes.Repeat([]byte("x"), 4096)
transport := &loggingTransport{}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
body := io.NopCloser(bytes.NewReader(bodyData))
_, _ = transport.readBodyLimited(body, 8192)
}
})
}
// BenchmarkLoggerLogParallel benchmarks logging under concurrent load
func BenchmarkLoggerLogParallel(b *testing.B) {
l := &Logger{
forwardID: "benchmark",
maxBodyLen: 1024,
output: io.Discard,
}
entry := Entry{
Direction: "request",
RequestID: "req-123",
Method: "POST",
Path: "/api/users",
Body: `{"name":"test user"}`,
BodySize: 100,
}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_ = l.Log(entry)
}
})
}
// BenchmarkCompleteFlow benchmarks the complete logging flow
func BenchmarkCompleteFlow(b *testing.B) {
l := &Logger{
forwardID: "benchmark",
maxBodyLen: 1024,
output: io.Discard,
}
headers := http.Header{
"Content-Type": []string{"application/json"},
"Accept": []string{"application/json"},
}
bodyData := []byte(`{"id":123,"name":"test"}`)
transport := &loggingTransport{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
// Simulate full request logging flow
entry := Entry{
Direction: "request",
RequestID: "req-123",
Method: "POST",
Path: "/api/users",
Headers: flattenHeaders(headers),
BodySize: len(bodyData),
Body: string(bodyData),
}
_ = l.Log(entry)
// Simulate body reading
body := io.NopCloser(bytes.NewReader(bodyData))
_, _ = transport.readBodyLimited(body, 2048)
}
}
+47 -6
View File
@@ -13,6 +13,7 @@
package httplog
import (
"bytes"
"encoding/json"
"io"
"os"
@@ -20,6 +21,14 @@ import (
"time"
)
// logBufferPool is used to reuse byte buffers for JSON encoding.
// This reduces allocations when serializing log entries.
var logBufferPool = sync.Pool{
New: func() interface{} {
return bytes.NewBuffer(make([]byte, 0, 4096))
},
}
// Entry represents a single HTTP log entry
type Entry struct {
Timestamp time.Time `json:"timestamp"`
@@ -89,18 +98,50 @@ func (l *Logger) ClearCallbacks() {
l.callbacks = nil
}
// Log writes a log entry as JSON
// stringBuilderPool provides reusable string builders for body truncation.
// This reduces allocations when building truncated body strings.
var stringBuilderPool = sync.Pool{
New: func() interface{} {
return &bytes.Buffer{}
},
}
// truncateBody truncates a body string to maxLen, adding a suffix if truncated.
// Uses a pooled buffer to avoid allocations during truncation.
func truncateBody(body string, maxLen int) string {
if len(body) <= maxLen {
return body
}
// Use pooled buffer for truncation
buf := stringBuilderPool.Get().(*bytes.Buffer)
buf.Reset()
defer stringBuilderPool.Put(buf)
// Write truncated content
buf.WriteString(body[:maxLen])
buf.WriteString("...(truncated)")
return buf.String()
}
// Log writes a log entry as JSON using a pooled buffer to reduce allocations.
func (l *Logger) Log(entry Entry) error {
entry.ForwardID = l.forwardID
entry.Timestamp = time.Now()
// Truncate body if too large
// Truncate body if too large using pooled buffer
if len(entry.Body) > l.maxBodyLen {
entry.Body = entry.Body[:l.maxBodyLen] + "...(truncated)"
entry.Body = truncateBody(entry.Body, l.maxBodyLen)
}
data, err := json.Marshal(entry)
if err != nil {
// Get a buffer from the pool
buf := logBufferPool.Get().(*bytes.Buffer)
buf.Reset() // Clear any previous content
defer logBufferPool.Put(buf)
// Encode JSON directly into the pooled buffer
encoder := json.NewEncoder(buf)
if err := encoder.Encode(entry); err != nil {
return err
}
@@ -112,7 +153,7 @@ func (l *Logger) Log(entry Entry) error {
cb(entry)
}
_, err = l.output.Write(append(data, '\n'))
_, err := l.output.Write(buf.Bytes())
return err
}
+75 -9
View File
@@ -14,10 +14,29 @@ import (
"sync/atomic"
"time"
"github.com/nvm/kportal/internal/config"
"github.com/nvm/kportal/internal/logger"
"github.com/lukaszraczylo/kportal/internal/config"
"github.com/lukaszraczylo/kportal/internal/logger"
)
// bufferPool is used to reuse byte buffers for body reading.
// This significantly reduces GC pressure under high load.
// Using *([]byte) to avoid allocations when storing/retrieving from pool (SA6002).
var bufferPool = sync.Pool{
New: func() interface{} {
buf := make([]byte, 0, 8192) // Start with 8KB capacity
return &buf
},
}
// readBufferPool provides fixed-size buffers for io.Reader operations.
// Using a pool eliminates per-read allocations of temporary buffers.
var readBufferPool = sync.Pool{
New: func() interface{} {
buf := make([]byte, 4096) // 4KB fixed-size read buffer
return &buf
},
}
// Proxy is an HTTP reverse proxy with logging capabilities
type Proxy struct {
listener net.Listener
@@ -218,27 +237,73 @@ func (t *loggingTransport) RoundTrip(req *http.Request) (*http.Response, error)
// Returns the body content (up to maxSize bytes) and the actual content length.
// If the body exceeds maxSize, it reads only maxSize bytes for logging but
// consumes the entire body to get the true size for BodySize reporting.
// Uses sync.Pool to reuse buffers and reduce allocations.
func (t *loggingTransport) readBodyLimited(body io.ReadCloser, maxSize int) ([]byte, int) {
// Get a buffer from the pool for accumulating body content
bufPtr := bufferPool.Get().(*[]byte)
buf := *bufPtr
buf = buf[:0] // Reset length but keep capacity
defer bufferPool.Put(bufPtr)
// Get a pooled read buffer to eliminate per-read allocation
tmpPtr := readBufferPool.Get().(*[]byte)
tmp := *tmpPtr
defer readBufferPool.Put(tmpPtr)
// Read up to maxSize+1 to detect if there's more
limitedReader := io.LimitReader(body, int64(maxSize+1))
data, err := io.ReadAll(limitedReader)
if err != nil {
return nil, 0
// Read into the pooled buffer
var totalRead int
for {
n, err := limitedReader.Read(tmp)
if n > 0 {
buf = append(buf, tmp[:n]...)
totalRead += n
}
if err != nil {
break
}
}
actualSize := len(data)
actualSize := len(buf)
wasTruncated := actualSize > maxSize
// If we read exactly maxSize+1, there might be more data
// Discard the rest but count the bytes for accurate BodySize
if wasTruncated {
data = data[:maxSize] // Keep only maxSize bytes for logging
// Count remaining bytes without storing them
remaining, _ := io.Copy(io.Discard, body)
actualSize = maxSize + int(remaining)
// Return a copy of just the maxSize bytes for logging
resultPtr := bufferPool.Get().(*[]byte)
result := *resultPtr
result = result[:maxSize]
copy(result, buf)
return result, actualSize
}
return data, actualSize
// For small results, allocate minimally. For larger results, use pooled buffer.
resultLen := len(buf)
var result []byte
if resultLen <= 4096 {
// Small body: allocate exact size to avoid holding large buffers
result = make([]byte, resultLen)
copy(result, buf)
} else {
// Larger body: try to use pooled buffer
resultPtr := bufferPool.Get().(*[]byte)
result = *resultPtr
if cap(result) >= resultLen {
result = result[:resultLen]
copy(result, buf)
} else {
// Pooled buffer too small, allocate new and don't return to pool
result = make([]byte, resultLen)
copy(result, buf)
}
}
return result, actualSize
}
// shouldLog checks if the request path matches the filter
@@ -274,7 +339,8 @@ func (p *Proxy) logError(req *http.Request, err error) {
_ = p.logger.Log(entry)
}
// flattenHeaders converts http.Header to map[string]string
// flattenHeaders converts http.Header to map[string]string.
// Pre-allocates the map with the exact size needed to avoid reallocations.
func flattenHeaders(h http.Header) map[string]string {
result := make(map[string]string, len(h))
for k, v := range h {
+1 -1
View File
@@ -8,7 +8,7 @@ import (
"os"
"testing"
"github.com/nvm/kportal/internal/config"
"github.com/lukaszraczylo/kportal/internal/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)