Skip to content

Commit

Permalink
fixed the fasthttp ctx race condition problem
Browse files Browse the repository at this point in the history
  • Loading branch information
ReneWerner87 committed Feb 27, 2024
1 parent a81b36e commit 5a0167a
Show file tree
Hide file tree
Showing 15 changed files with 311 additions and 321 deletions.
6 changes: 2 additions & 4 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ func New(config ...Config) *App {
// Create Ctx pool
app.pool = sync.Pool{
New: func() any {
return app.NewCtx(&fasthttp.RequestCtx{})
return app.newCtx()
},
}

Expand Down Expand Up @@ -1071,9 +1071,7 @@ func (app *App) ErrorHandler(ctx Ctx, err error) error {
// errors before calling the application's error handler method.
func (app *App) serverErrorHandler(fctx *fasthttp.RequestCtx, err error) {
// Acquire Ctx with fasthttp request from pool
c := app.AcquireCtx()
c.Reset(fctx)

c := app.AcquireCtx(fctx)
defer app.ReleaseCtx(c)

var (
Expand Down
9 changes: 4 additions & 5 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func Test_App_serverErrorHandler_Internal_Error(t *testing.T) {
t.Parallel()
app := New()
msg := "test err"
c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed
c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed

app.serverErrorHandler(c.fasthttp, errors.New(msg))
require.Equal(t, string(c.fasthttp.Response.Body()), msg)
Expand All @@ -324,7 +324,7 @@ func Test_App_serverErrorHandler_Internal_Error(t *testing.T) {
func Test_App_serverErrorHandler_Network_Error(t *testing.T) {
t.Parallel()
app := New()
c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed
c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed

app.serverErrorHandler(c.fasthttp, &net.DNSError{
Err: "test error",
Expand Down Expand Up @@ -1400,8 +1400,7 @@ func Test_App_Next_Method(t *testing.T) {
func Benchmark_AcquireCtx(b *testing.B) {
app := New()
for n := 0; n < b.N; n++ {
c := app.AcquireCtx()
c.Reset(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})

app.ReleaseCtx(c)
}
Expand Down Expand Up @@ -1765,7 +1764,7 @@ func Test_App_SetTLSHandler(t *testing.T) {
app := New()
app.SetTLSHandler(tlsHandler)

c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)

require.Equal(t, "example.golang", c.ClientHelloInfo().ServerName)
Expand Down
74 changes: 37 additions & 37 deletions bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const helloWorld = "hello world"
func Test_Bind_Query(t *testing.T) {
t.Parallel()
app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})

type Query struct {
ID int
Expand Down Expand Up @@ -98,7 +98,7 @@ func Test_Bind_Query_Map(t *testing.T) {
t.Parallel()

app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})

c.Request().SetBody([]byte(``))
c.Request().Header.SetContentType("")
Expand Down Expand Up @@ -156,7 +156,7 @@ func Test_Bind_Query_WithSetParserDecoder(t *testing.T) {
})

app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})

type NonRFCTimeInput struct {
Date NonRFCTime `query:"date"`
Expand Down Expand Up @@ -188,7 +188,7 @@ func Test_Bind_Query_WithSetParserDecoder(t *testing.T) {
func Test_Bind_Query_Schema(t *testing.T) {
t.Parallel()
app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})

type Query1 struct {
Name string `query:"name,required"`
Expand Down Expand Up @@ -289,7 +289,7 @@ func Test_Bind_Query_Schema(t *testing.T) {
func Test_Bind_Header(t *testing.T) {
t.Parallel()
app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})

type Header struct {
ID int
Expand Down Expand Up @@ -362,7 +362,7 @@ func Test_Bind_Header_Map(t *testing.T) {
t.Parallel()

app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})

c.Request().SetBody([]byte(``))
c.Request().Header.SetContentType("")
Expand Down Expand Up @@ -410,7 +410,7 @@ func Test_Bind_Header_WithSetParserDecoder(t *testing.T) {
})

app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})

type NonRFCTimeInput struct {
Date NonRFCTime `req:"date"`
Expand Down Expand Up @@ -445,7 +445,7 @@ func Test_Bind_Header_WithSetParserDecoder(t *testing.T) {
func Test_Bind_Header_Schema(t *testing.T) {
t.Parallel()
app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})

type Header1 struct {
Name string `header:"Name,required"`
Expand Down Expand Up @@ -530,7 +530,7 @@ func Test_Bind_Header_Schema(t *testing.T) {
func Test_Bind_RespHeader(t *testing.T) {
t.Parallel()
app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})

type Header struct {
ID int
Expand Down Expand Up @@ -603,7 +603,7 @@ func Test_Bind_RespHeader_Map(t *testing.T) {
t.Parallel()

app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})

c.Request().SetBody([]byte(``))
c.Request().Header.SetContentType("")
Expand Down Expand Up @@ -632,7 +632,7 @@ func Benchmark_Bind_Query(b *testing.B) {
var err error

app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})

type Query struct {
ID int
Expand All @@ -656,7 +656,7 @@ func Benchmark_Bind_Query_Map(b *testing.B) {
var err error

app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})

c.Request().SetBody([]byte(``))
c.Request().Header.SetContentType("")
Expand All @@ -675,7 +675,7 @@ func Benchmark_Bind_Query_WithParseParam(b *testing.B) {
var err error

app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})

type Person struct {
Name string `query:"name"`
Expand Down Expand Up @@ -705,7 +705,7 @@ func Benchmark_Bind_Query_Comma(b *testing.B) {
var err error

app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})

type Query struct {
ID int
Expand All @@ -730,7 +730,7 @@ func Benchmark_Bind_Header(b *testing.B) {
var err error

app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})

type ReqHeader struct {
ID int
Expand All @@ -757,7 +757,7 @@ func Benchmark_Bind_Header(b *testing.B) {
func Benchmark_Bind_Header_Map(b *testing.B) {
var err error
app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})

c.Request().SetBody([]byte(``))
c.Request().Header.SetContentType("")
Expand All @@ -780,7 +780,7 @@ func Benchmark_Bind_RespHeader(b *testing.B) {
var err error

app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})

type ReqHeader struct {
ID int
Expand All @@ -807,7 +807,7 @@ func Benchmark_Bind_RespHeader(b *testing.B) {
func Benchmark_Bind_RespHeader_Map(b *testing.B) {
var err error
app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})

c.Request().SetBody([]byte(``))
c.Request().Header.SetContentType("")
Expand All @@ -829,7 +829,7 @@ func Benchmark_Bind_RespHeader_Map(b *testing.B) {
func Test_Bind_Body(t *testing.T) {
t.Parallel()
app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})

type Demo struct {
Name string `json:"name" xml:"name" form:"name" query:"name"`
Expand Down Expand Up @@ -926,7 +926,7 @@ func Test_Bind_Body_WithSetParserDecoder(t *testing.T) {
})

app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})

type Demo struct {
Date CustomTime `form:"date"`
Expand Down Expand Up @@ -958,7 +958,7 @@ func Benchmark_Bind_Body_JSON(b *testing.B) {
var err error

app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})

type Demo struct {
Name string `json:"name"`
Expand All @@ -984,7 +984,7 @@ func Benchmark_Bind_Body_XML(b *testing.B) {
var err error

app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})

type Demo struct {
Name string `xml:"name"`
Expand All @@ -1010,7 +1010,7 @@ func Benchmark_Bind_Body_Form(b *testing.B) {
var err error

app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})

type Demo struct {
Name string `form:"name"`
Expand All @@ -1036,7 +1036,7 @@ func Benchmark_Bind_Body_MultipartForm(b *testing.B) {
var err error

app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})

type Demo struct {
Name string `form:"name"`
Expand All @@ -1063,7 +1063,7 @@ func Benchmark_Bind_Body_Form_Map(b *testing.B) {
var err error

app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})

body := []byte("name=john")
c.Request().SetBody(body)
Expand Down Expand Up @@ -1131,7 +1131,7 @@ func Benchmark_Bind_URI(b *testing.B) {
var err error

app := New()
c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed
c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed

c.route = &Route{
Params: []string{
Expand Down Expand Up @@ -1168,7 +1168,7 @@ func Benchmark_Bind_URI_Map(b *testing.B) {
var err error

app := New()
c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed
c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed

c.route = &Route{
Params: []string{
Expand Down Expand Up @@ -1200,7 +1200,7 @@ func Test_Bind_Cookie(t *testing.T) {
t.Parallel()

app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})

type Cookie struct {
ID int
Expand Down Expand Up @@ -1273,7 +1273,7 @@ func Test_Bind_Cookie_Map(t *testing.T) {
t.Parallel()

app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})

c.Request().SetBody([]byte(``))
c.Request().Header.SetContentType("")
Expand Down Expand Up @@ -1321,7 +1321,7 @@ func Test_Bind_Cookie_WithSetParserDecoder(t *testing.T) {
})

app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})

type NonRFCTimeInput struct {
Date NonRFCTime `cerez:"date"`
Expand Down Expand Up @@ -1357,7 +1357,7 @@ func Test_Bind_Cookie_Schema(t *testing.T) {
t.Parallel()

app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})

type Cookie1 struct {
Name string `cookie:"Name,required"`
Expand Down Expand Up @@ -1443,7 +1443,7 @@ func Benchmark_Bind_Cookie(b *testing.B) {
var err error

app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})

type Cookie struct {
ID int
Expand Down Expand Up @@ -1472,7 +1472,7 @@ func Benchmark_Bind_Cookie_Map(b *testing.B) {
var err error

app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})

c.Request().SetBody([]byte(``))
c.Request().Header.SetContentType("")
Expand Down Expand Up @@ -1509,7 +1509,7 @@ func (*customBinder) Parse(c Ctx, out any) error {
// go test -run Test_Bind_CustomBinder
func Test_Bind_CustomBinder(t *testing.T) {
app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})

// Register binder
customBinder := &customBinder{}
Expand All @@ -1533,7 +1533,7 @@ func Test_Bind_CustomBinder(t *testing.T) {
// go test -run Test_Bind_Must
func Test_Bind_Must(t *testing.T) {
app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})

type RequiredQuery struct {
Name string `query:"name,required"`
Expand Down Expand Up @@ -1573,7 +1573,7 @@ type simpleQuery struct {
// go test -run Test_Bind_StructValidator
func Test_Bind_StructValidator(t *testing.T) {
app := New(Config{StructValidator: &structValidator{}})
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})

rq := new(simpleQuery)
c.Request().URI().SetQueryString("name=efe")
Expand All @@ -1588,7 +1588,7 @@ func Test_Bind_StructValidator(t *testing.T) {
func Test_Bind_RepeatParserWithSameStruct(t *testing.T) {
t.Parallel()
app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)

type Request struct {
Expand Down
Loading

0 comments on commit 5a0167a

Please sign in to comment.