Skip to content

Commit

Permalink
Use Noop Logger when creating RequestCtx
Browse files Browse the repository at this point in the history
  • Loading branch information
gaby committed Sep 14, 2024
1 parent 5c1a0ce commit c879135
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
7 changes: 6 additions & 1 deletion middleware/adaptor/adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import (
"github.com/valyala/fasthttp/fasthttpadaptor"
)

type disableLogger struct{}

func (*disableLogger) Printf(string, ...any) {

Check warning on line 19 in middleware/adaptor/adaptor.go

View check run for this annotation

Codecov / codecov/patch

middleware/adaptor/adaptor.go#L19

Added line #L19 was not covered by tests
}

var ctxPool = sync.Pool{
New: func() any {
return new(fasthttp.RequestCtx)
Expand Down Expand Up @@ -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
Expand Down
29 changes: 26 additions & 3 deletions middleware/adaptor/adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand All @@ -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),
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit c879135

Please sign in to comment.