mirror of
https://github.com/eolinker/apinto
synced 2025-11-03 02:53:33 +08:00
优化 newHttpContext 性能
This commit is contained in:
@@ -22,11 +22,11 @@ var _ http_service.IHttpContext = (*HttpContext)(nil)
|
||||
// HttpContext fasthttpRequestCtx
|
||||
type HttpContext struct {
|
||||
fastHttpRequestCtx *fasthttp.RequestCtx
|
||||
proxyRequest *ProxyRequest
|
||||
proxyRequest ProxyRequest
|
||||
proxyRequests []http_service.IRequest
|
||||
requestID string
|
||||
response *Response
|
||||
requestReader *RequestReader
|
||||
response Response
|
||||
requestReader RequestReader
|
||||
ctx context.Context
|
||||
completeHandler eoscContext.CompleteHandler
|
||||
finishHandler eoscContext.FinishHandler
|
||||
@@ -118,14 +118,14 @@ func (ctx *HttpContext) Proxies() []http_service.IRequest {
|
||||
}
|
||||
|
||||
func (ctx *HttpContext) Response() http_service.IResponse {
|
||||
return ctx.response
|
||||
return &ctx.response
|
||||
}
|
||||
|
||||
func (ctx *HttpContext) SendTo(address string, timeout time.Duration) error {
|
||||
|
||||
_, host := readAddress(address)
|
||||
request := ctx.proxyRequest.Request()
|
||||
ctx.proxyRequests = append(ctx.proxyRequests, newRequestAgent(ctx.proxyRequest, host))
|
||||
ctx.proxyRequests = append(ctx.proxyRequests, newRequestAgent(&ctx.proxyRequest, host))
|
||||
|
||||
passHost, targethost := ctx.GetUpstreamHostHandler().PassHost()
|
||||
switch passHost {
|
||||
@@ -158,31 +158,36 @@ func (ctx *HttpContext) WithValue(key, val interface{}) {
|
||||
}
|
||||
|
||||
func (ctx *HttpContext) Proxy() http_service.IRequest {
|
||||
return ctx.proxyRequest
|
||||
return &ctx.proxyRequest
|
||||
}
|
||||
|
||||
func (ctx *HttpContext) Request() http_service.IRequestReader {
|
||||
|
||||
return ctx.requestReader
|
||||
return &ctx.requestReader
|
||||
}
|
||||
|
||||
// NewContext 创建Context
|
||||
func NewContext(ctx *fasthttp.RequestCtx, port int) *HttpContext {
|
||||
|
||||
requestID := uuid.New().String()
|
||||
newCtx := &HttpContext{
|
||||
fastHttpRequestCtx: ctx,
|
||||
requestID: requestID,
|
||||
requestReader: NewRequestReader(&ctx.Request, ctx.RemoteAddr().String()),
|
||||
proxyRequest: NewProxyRequest(&ctx.Request, ctx.RemoteAddr().String()),
|
||||
proxyRequests: make([]http_service.IRequest, 0, 5),
|
||||
response: NewResponse(ctx),
|
||||
labels: make(map[string]string),
|
||||
port: port,
|
||||
}
|
||||
remoteAddr := ctx.RemoteAddr().String()
|
||||
|
||||
httpContext := pool.Get().(*HttpContext)
|
||||
|
||||
httpContext.fastHttpRequestCtx = ctx
|
||||
httpContext.requestID = uuid.New().String()
|
||||
|
||||
httpContext.requestReader.reset(&ctx.Request, remoteAddr)
|
||||
httpContext.proxyRequest.reset(&ctx.Request, remoteAddr)
|
||||
httpContext.proxyRequests = httpContext.proxyRequests[:0]
|
||||
httpContext.response.reset(&ctx.Response)
|
||||
httpContext.labels = make(map[string]string)
|
||||
httpContext.port = port
|
||||
//记录请求时间
|
||||
newCtx.WithValue("request_time", ctx.Time())
|
||||
return newCtx
|
||||
httpContext.ctx = context.Background()
|
||||
httpContext.WithValue("request_time", ctx.Time())
|
||||
|
||||
return httpContext
|
||||
|
||||
}
|
||||
|
||||
// RequestId 请求ID
|
||||
@@ -198,9 +203,19 @@ func (ctx *HttpContext) FastFinish() {
|
||||
return
|
||||
}
|
||||
|
||||
ctx.port = 0
|
||||
ctx.ctx = nil
|
||||
ctx.app = nil
|
||||
ctx.balance = nil
|
||||
ctx.upstreamHostHandler = nil
|
||||
ctx.finishHandler = nil
|
||||
ctx.completeHandler = nil
|
||||
|
||||
ctx.requestReader.Finish()
|
||||
ctx.proxyRequest.Finish()
|
||||
|
||||
ctx.response.Finish()
|
||||
ctx.fastHttpRequestCtx = nil
|
||||
pool.Put(ctx)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user