Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v3: Fix issue with default logger when creating RequestCtx #3134

Merged
merged 2 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
"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 @@
fctx.Response.Reset()
fctx.Request.Reset()
defer ctxPool.Put(fctx)
fctx.Init(req, remoteAddr, nil)
fctx.Init(req, remoteAddr, &disableLogger{})
gaby marked this conversation as resolved.
Show resolved Hide resolved

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
Loading