RequestContext

Request Context RequestContext is a context used to save HTTP requests and set HTTP responses. It provides many convenient API interfaces to help users develop.

Hertz provides both a standard context.Context and a RequestContext as input parameters for the HandlerFunc design. Function signature is:

type HandlerFunc func(c context.Context, ctx *RequestContext)

Context Passing and Concurrency Security

  1. Metadata Storage

Both contexts have the ability to store values, and there is a simple basis for choosing which context to use: the lifecycle of the stored value and the chosen context should match.

  1. Concurrent Security

ctx is mainly used to store request level variables, which are reclaimed upon completion of the request. It’s characteristics are high query efficiency (with the underlying map), unsafe coroutines, and no implementation of the context.Context interface.

c is passed as a context between middleware /handler. Having all the semantics of context.Context, coroutine security. For all places that require the context.Context interface as input parameters, simply pass c directly.

  1. Asynchronous Delivery

In addition, if there is a scenario of asynchronous transmission of ctx, hertz also provides the ctx.Copy() interface, making it easy for businesses to obtain a secure copy of the protocol.


Last modified July 18, 2023 : doc:impove context (4f905b1)