diff --git a/middleware/adaptor/adaptor.go b/middleware/adaptor/adaptor.go index 539aa3e24b..a5040b39ae 100644 --- a/middleware/adaptor/adaptor.go +++ b/middleware/adaptor/adaptor.go @@ -14,6 +14,11 @@ import ( "github.com/valyala/fasthttp/fasthttpadaptor" ) +type disableLogger struct{} + +func (*disableLogger) Printf(string, ...any) { +} + var ctxPool = sync.Pool{ New: func() any { return new(fasthttp.RequestCtx) @@ -171,7 +176,7 @@ func handlerFunc(app *fiber.App, h ...fiber.Handler) http.HandlerFunc { fctx.Response.Reset() fctx.Request.Reset() defer ctxPool.Put(fctx) - fctx.Init(req, remoteAddr, nil) + fctx.Init(req, remoteAddr, &disableLogger{}) if len(h) > 0 { // New fiber Ctx diff --git a/middleware/adaptor/adaptor_test.go b/middleware/adaptor/adaptor_test.go index 32abc64d93..c703d38436 100644 --- a/middleware/adaptor/adaptor_test.go +++ b/middleware/adaptor/adaptor_test.go @@ -86,7 +86,7 @@ func Test_HTTPHandler(t *testing.T) { remoteAddr, err := net.ResolveTCPAddr("tcp", expectedRemoteAddr) require.NoError(t, err) - fctx.Init(&req, remoteAddr, nil) + fctx.Init(&req, remoteAddr, &disableLogger{}) app := fiber.New() ctx := app.AcquireCtx(&fctx) defer app.ReleaseCtx(ctx) @@ -412,6 +412,10 @@ func Benchmark_FiberHandlerFunc(b *testing.B) { name string bodyContent []byte }{ + { + name: "No Content", + bodyContent: nil, // No body content case + }, { name: "100KB", bodyContent: make([]byte, 100*1024), @@ -450,7 +454,14 @@ func Benchmark_FiberHandlerFunc(b *testing.B) { for _, bm := range benchmarks { b.Run(bm.name, func(b *testing.B) { w := httptest.NewRecorder() - bodyBuffer := bytes.NewBuffer(bm.bodyContent) + var bodyBuffer *bytes.Buffer + + // Handle the "No Content" case where bodyContent is nil + if bm.bodyContent != nil { + bodyBuffer = bytes.NewBuffer(bm.bodyContent) + } else { + bodyBuffer = bytes.NewBuffer([]byte{}) // Empty buffer for no content + } r := http.Request{ Method: http.MethodPost, @@ -476,6 +487,10 @@ func Benchmark_FiberHandlerFunc_Parallel(b *testing.B) { name string bodyContent []byte }{ + { + name: "No Content", + bodyContent: nil, // No body content case + }, { name: "100KB", bodyContent: make([]byte, 100*1024), @@ -513,7 +528,15 @@ func Benchmark_FiberHandlerFunc_Parallel(b *testing.B) { for _, bm := range benchmarks { b.Run(bm.name, func(b *testing.B) { - bodyBuffer := bytes.NewBuffer(bm.bodyContent) + var bodyBuffer *bytes.Buffer + + // Handle the "No Content" case where bodyContent is nil + if bm.bodyContent != nil { + bodyBuffer = bytes.NewBuffer(bm.bodyContent) + } else { + bodyBuffer = bytes.NewBuffer([]byte{}) // Empty buffer for no content + } + b.ReportAllocs() b.ResetTimer()