Skip to content

Commit

Permalink
doc: improve context doc (#722)
Browse files Browse the repository at this point in the history
  • Loading branch information
chaoranz758 committed Jul 18, 2023
1 parent 1fb7335 commit fca6c87
Show file tree
Hide file tree
Showing 6 changed files with 337 additions and 937 deletions.
16 changes: 7 additions & 9 deletions content/en/docs/hertz/tutorials/basic-feature/context/_index.md
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

1 comment on commit fca6c87

@vercel
Copy link

@vercel vercel bot commented on fca6c87 Jul 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.