Home  >  Article  >  Backend Development  >  What is golang context used for?

What is golang context used for?

(*-*)浩
(*-*)浩Original
2019-12-03 09:52:453075browse

What is golang context used for?

#context has been included in the standard library since Go1.7. Its main use, in one sentence, is to control the life cycle of goroutine. When a computing task is taken over by a goroutine, and we want to abort the goroutine's computing task for some reason (timeout, or forced exit), then this Context is used.                                                                                                                                                   (Recommended learning: go)

For Golang developers, the context package must be familiar. But many times, we are lazy and just see it or what role it can play, and do not delve into it.

Application scenario: In the Server of the Go http package, each request has a corresponding goroutine to handle it.

Request processing functions usually start additional goroutines to access back-end services, such as databases and RPC services.

The goroutine used to process a request usually needs to access some data specific to the request, such as the end user's identity authentication information, verification-related tokens, and the request's deadline.

When a request is canceled or times out, all goroutines used to process the request should exit quickly, and then the system can release the resources occupied by these goroutines

Context Principle

Context calls should be chained, and new Contexts are derived through WithCancel, WithDeadline, WithTimeout or WithValue. When a parent Context is canceled, all Contexts derived from it will be canceled.

Passing context.WithXXX will return new Context and CancelFunc. Calling CancelFunc will cancel the child, remove the parent's reference to the child, and stop all timers. Failure to call CancelFunc will leak children until the parent is canceled or a timer fires. The go vet tool checks all process control paths using CancelFuncs.

Follow the rules

Follow the following rules to keep interfaces consistent between packages and enable static analysis tools to check context propagation.

Do not put Contexts into the structure. Instead, context should be passed in as the first parameter and named ctx. func DoSomething(ctx context.Context, arg Arg) error { // ... use ctx ... }

Do not pass in nil Context even if the function allows it. If you don't know which Context to use, you can use context.TODO().

The Value-related methods using context should only be used for request-related metadata passed in programs and interfaces. Do not use it to pass some optional parameters.

The same Context can be used Passed to different goroutines; Context is concurrency safe.

The above is the detailed content of What is golang context used for?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn