mirror of
https://github.com/datarhei/core.git
synced 2025-10-11 19:00:13 +08:00
Use buffer pool
This commit is contained in:
@@ -7,7 +7,7 @@ import (
|
||||
"github.com/lithammer/shortuuid/v4"
|
||||
)
|
||||
|
||||
func (h *handler) handleHTTP(c echo.Context, ctxuser string, data map[string]interface{}, next echo.HandlerFunc) error {
|
||||
func (h *handler) handleHTTP(c echo.Context, _ string, data map[string]interface{}, next echo.HandlerFunc) error {
|
||||
req := c.Request()
|
||||
res := c.Response()
|
||||
|
||||
@@ -30,13 +30,13 @@ func (h *handler) handleHTTP(c echo.Context, ctxuser string, data map[string]int
|
||||
id := shortuuid.New()
|
||||
|
||||
reader := req.Body
|
||||
r := &fakeReader{
|
||||
r := &bodysizeReader{
|
||||
reader: req.Body,
|
||||
}
|
||||
req.Body = r
|
||||
|
||||
writer := res.Writer
|
||||
w := &fakeWriter{
|
||||
w := &bodysizeWriter{
|
||||
ResponseWriter: res.Writer,
|
||||
}
|
||||
res.Writer = w
|
||||
@@ -44,19 +44,21 @@ func (h *handler) handleHTTP(c echo.Context, ctxuser string, data map[string]int
|
||||
h.httpCollector.RegisterAndActivate(id, "", location, referrer)
|
||||
h.httpCollector.Extra(id, data)
|
||||
|
||||
defer h.httpCollector.Close(id)
|
||||
|
||||
defer func() {
|
||||
buffer := h.bufferPool.Get()
|
||||
|
||||
req.Body = reader
|
||||
h.httpCollector.Ingress(id, r.size+headerSize(req.Header))
|
||||
}()
|
||||
h.httpCollector.Ingress(id, r.size+headerSize(req.Header, buffer))
|
||||
|
||||
defer func() {
|
||||
res.Writer = writer
|
||||
|
||||
h.httpCollector.Egress(id, w.size+headerSize(res.Header()))
|
||||
h.httpCollector.Egress(id, w.size+headerSize(res.Header(), buffer))
|
||||
data["code"] = res.Status
|
||||
h.httpCollector.Extra(id, data)
|
||||
|
||||
h.httpCollector.Close(id)
|
||||
|
||||
h.bufferPool.Put(buffer)
|
||||
}()
|
||||
|
||||
return next(c)
|
||||
|
Reference in New Issue
Block a user