Skip to content

Commit

Permalink
Minimal improve
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio Andres Virviescas Santana committed Jul 2, 2020
1 parent 3430e5a commit 6b40c09
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 18 deletions.
7 changes: 2 additions & 5 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,12 @@ func AcquireRequestCtx(ctx *fasthttp.RequestCtx) *RequestCtx {
// It is forbidden accessing ctx and/or its' members after returning
// it to request pool.
func ReleaseRequestCtx(ctx *RequestCtx) {
ctx.reset()
requestCtxPool.Put(ctx)
}

func (ctx *RequestCtx) reset() {
ctx.next = false
ctx.skipView = false
ctx.searchingOnAttachedCtx = false
ctx.RequestCtx = nil

requestCtxPool.Put(ctx)
}

// RequestID returns the "X-Request-ID" header value.
Expand Down
18 changes: 6 additions & 12 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,13 @@ func Test_ReleaseRequestCtx(t *testing.T) {
ctx := new(fasthttp.RequestCtx)
actx := AcquireRequestCtx(ctx)

ReleaseRequestCtx(actx)

if actx.RequestCtx != nil {
t.Errorf("ReleaseRequestCtx() *fasthttp.RequestCtx = %p, want %v", actx.RequestCtx, nil)
}
}

func TestRequestCtx_reset(t *testing.T) {
ctx := new(fasthttp.RequestCtx)
actx := AcquireRequestCtx(ctx)

if err := actx.Next(); err != nil {
t.Fatalf("Error calling next. %+v", err)
}

actx.SkipView()
actx.reset()

ReleaseRequestCtx(actx)

if actx.next {
t.Errorf("reset() next is not 'false'")
Expand All @@ -50,6 +40,10 @@ func TestRequestCtx_reset(t *testing.T) {
if actx.RequestCtx != nil {
t.Errorf("reset() *fasthttp.RequestCtx = %p, want %v", actx.RequestCtx, nil)
}

if actx.RequestCtx != nil {
t.Errorf("ReleaseRequestCtx() *fasthttp.RequestCtx = %p, want %v", actx.RequestCtx, nil)
}
}

func TestRequestCtx_RequestID(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion router.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,12 @@ func (r *Router) handler(fn View, middle Middlewares) fasthttp.RequestHandler {
return ctx.Next()
})
chain = append(chain, middle.After...)
chainLen := len(chain)

return func(ctx *fasthttp.RequestCtx) {
actx := AcquireRequestCtx(ctx)

for i := range chain {
for i := 0; i < chainLen; i++ {
if err := chain[i](actx); err != nil {
statusCode := actx.Response.StatusCode()
if statusCode == fasthttp.StatusOK {
Expand Down

0 comments on commit 6b40c09

Please sign in to comment.