mirror of https://github.com/gofiber/fiber.git
v3: Fix issue with default logger when creating RequestCtx (#3134)
Use Noop Logger when creating RequestCtxpull/3140/head
parent
0ef8d716ee
commit
fbc24e83d6
|
@ -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
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
Loading…
Reference in New Issue