/
context.go
62 lines (51 loc) · 1.08 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package fastreq
import (
"context"
"sync"
"github.com/valyala/fasthttp"
)
// Ctx represents the Context which hold the HTTP request and response.
type Ctx struct {
Request *Request
Response *Response
ctx context.Context
client *Client
indexMiddleware int
}
// Next ..
func (c *Ctx) Next() (err error) {
// Increment handler index
c.indexMiddleware++
// Did we execute all route handlers?
if c.indexMiddleware < len(c.client.middlewares) {
// Continue route stack
return c.client.middlewares[c.indexMiddleware](c)
} else {
// Continue handler stack
return do(c)
}
}
func (c *Ctx) Release() {
c.Request.Release()
c.Response.Release()
c.client = nil
c.indexMiddleware = 0
ctxPool.Put(c)
}
func (c *Ctx) fastClient() *fasthttp.Client {
return c.client.Client
}
func (c *Ctx) fastRequest() *fasthttp.Request {
return c.Request.Request
}
func (c *Ctx) fastResponse() *fasthttp.Response {
return c.Response.Response
}
var ctxPool sync.Pool
func NewCtx() *Ctx {
v := ctxPool.Get()
if v == nil {
return &Ctx{}
}
return v.(*Ctx)
}