Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: improve context doc #722

Merged
merged 2 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,22 @@ description: >

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.

## Context Passing and Concurrency Security

### Description

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

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

## Context Passing and Concurrency Security

### 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.

#### Details

`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.
`c` is passed as a context between middleware `/handler`. it is coroutine security. For all places that require the `context.Context` interface as input parameters, simply pass `c` directly.

### concurrent security

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.
In addition, if there are scenarios where `ctx` is passed asynchronously or used concurrently, hertz also provides the `ctx.Copy()` interface, which makes it easy for the business to get a coprocess-safe copy.
Loading
Loading