当前位置:网站首页>Go language context explanation
Go language context explanation
2022-07-07 05:43:00 【Heavy dust】
Catalog
GO In language Context Bao explained in detail
Author mogd 2022-06-28
Update mogd 2022-07-04
AdageRreality is merely an illusion, albeit a very persistent one.
Preface
I don't know if there are any friends like me , I'm learning go When language is basic , Meet the need context
The information of , It's all direct context.TODO()
, No further study Context
Let the author deeply understand Context
It's because I'm writing a K8S CMDB Platform time , Think of the need to initialize the data ( Is to create admin user , And test users , And assign corresponding Casbin jurisdiction )
because user The module references gin-vue-admin, And in GVA
There is also a data initialization part in , This part of the code uses Context
Read it carefully , That's right Context
Bao has a deeper understanding
Studying hard is often just knowing its shape but not its meaning , Only by actually doing projects can we really master a language
The author wrote a K8S CMDB Platform backend interface ,gin-kubernetes
This is just beginning to write , It's very simple , You can have a look at what you are interested in ; I also hope the big guys can make suggestions
user Module reference gin-vue-admin
One 、Context Introduce
1.1 Context What is it? ?
Context
stay Go1.7
Then join Go
In the language standard library , yes Goroutine
The context of , contain Goroutine
Operating state 、 Environmental Science 、 Information about the scene
In Go servers, each incoming request is handled in its own goroutine. Request handlers often start additional goroutines to access backends such as databases and RPC services. The set of goroutines working on a request typically needs access to request-specific values such as the identity of the end user, authorization tokens, and the request’s deadline. When a request is canceled or times out, all the goroutines working on that request should exit quickly so the system can reclaim any resources they are using.
At Google, we developed a context package that makes it easy to pass request-scoped values, cancellation signals, and deadlines across API boundaries to all the goroutines involved in handling a requestGo
In the service of , Each request will have its owngoroutine
Handle , Everygoroutine
Usually start a newgoroutine
Perform some extra work , Such as database orRPC
Access to services . Same as in the requestgoroutine
Need to be able to share request data access , such as , User authentication , to grant authorizationtoken
, And request deadline . If the request is canceled or a timeout occurs , All within the scope of the requestgoroutine
Should quit immediately , Recycling
staycontext
My bag , Through it , We can easily requestgoroutine
Transfer request data between 、 Cancel signal and timeout information
Judging from the official introduction ,Context
Package for goroutine
To send messages . It's with us goroutine
Used between channel
It's like , But it's different . Because in different goroutine
Only done channel
It contains too little information
And with Context
Package Introduction , Many interfaces of the standard library are added Context
Parameters ,Context
Almost become the standard practice of concurrency control and timeout control
Context Use scenarios
- Context messaging (request-scoped), For example, deal with http request 、 Passing information along the request processing chain
- The controller goroutine Operation of
- Timeout control method call
- Method calls that can be cancelled
1.2 Context Interface
context
The core is Context
type
// A Context carries a deadline, cancellation signal, and request-scoped values
// across API boundaries. Its methods are safe for simultaneous use by multiple
// goroutines.
type Context interface {
// Done returns a channel that is closed when this Context is canceled
// or times out.
Done() <-chan struct{
}
// Err indicates why this context was canceled, after the Done channel
// is closed.
Err() error
// Deadline returns the time when this Context will be canceled, if any.
Deadline() (deadline time.Time, ok bool)
// Value returns the value associated with key or nil if none.
Value(key interface{
}) interface{
}
}
Done
Method returns achannel
, It can be used to receivecontext
The cancellation signal of . Whenchannel
close , monitorDone
The function of the signal will immediately give up the work currently being performed and returnErr
Method returns aerror
Variable , From it we can knowcontext
Why was it cancelled .pipeline and cancelation One article is rightDone channel
Made a detailed introductionDeadline
Method lets the function decide whether to start work , If the remaining time is too short , Then starting the work is not worth it . In the code , We can go throughdeadline
by IO Operation set timeoutValue
Method can makecontext
staygoroutine
Share data within the scope of the request , These data need to be concurrent and secure
If
Done
Has not beenclose
,Err
Method returnsnil
; IfDone
Byclose
,Err
Method will returnDone
Byclose
Why
Two 、Context Use
stay
Context
Pass as the first parameter . such , Even for different teamsGo
Code can also work well .Context
It's very convenientgoroutine
Timeout and cancellation control , And ensure the safe transmission of important data , Such as security credentials
context
Package mainly provides two methods to create Context
- context.Background()
- context.TODO()
These two functions are aliases to each other , There is no difference , The official definition of :
context.Background
Is the default value for the context , All other contexts should derive from it (Derived) come outcontext.TODO()
It should only be used when you are not sure which context to use
Created in these two ways context
, Without any function , Its realization depends on context
Provided by the package with
Series
func WithCancel(parent Context) (ctx Context, cancel CancelFunc)
func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc)
func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc)
func WithValue(parent Context, key, val interface{
}) Context
A project on Context
Use , Through these four derived functions , Form a Context
Count . Each node of the tree can have any number of child nodes , There can be as many nodes as you want , Each child node depends on the parent node
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-SslcluBn-1656939722939)(./images/context-derive.png)]
2.1 WithValue Carrying data 、Value get data
Here is an open source project GVA
As an example, the data initialization part of the code in , Deleted some judgment and data codes , Just keep context
part
// InitDB Create the database and initialize The main entrance
func (initDBService *InitDBService) InitDB(conf request.InitDB) (err error) {
ctx := context.TODO()
var initHandler TypedDBInitHandler
initHandler = NewMysqlInitHandler()
ctx = context.WithValue(ctx, "dbtype", "mysql")
ctx, _ = initHandler.EnsureDB(ctx, &conf)
db := ctx.Value("db").(*gorm.DB)
global.GVA_DB = db
if err = initHandler.WriteConfig(ctx); err != nil {
return err
}
if err = initHandler.InitTables(ctx, initializers); err != nil {
return err
}
if err = initHandler.InitData(ctx, initializers); err != nil {
return err
}
return nil
}
// EnsureDB Create the database and initialize mysql
func (h MysqlInitHandler) EnsureDB(ctx context.Context, conf *request.InitDB) (next context.Context, err error) {
if s, ok := ctx.Value("dbtype").(string); !ok || s != "mysql" {
return ctx, ErrDBTypeMismatch
}
c := conf.ToMysqlConfig()
next = context.WithValue(ctx, "config", c)
next = context.WithValue(next, "db", db)
}
// WriteConfig mysql Write back configuration
func (h MysqlInitHandler) WriteConfig(ctx context.Context) error {
c, _ := ctx.Value("config").(config.Mysql)
global.GVA_CONFIG.Mysql = c
return global.GVA_VP.WriteConfig()
}
Here is based on context.TODO()
Created a ctx
, adopt context.WithValue
take dbtype
, config
and db
write in context
, And then through context
Tree passing , Any derived from context
Can get these three information
In the code context
The process :
InitDB
write indbtype
,EnsureDB
adoptValue
obtaindbtype
Judge whether the database type is correctEnsureDB
write inconfig
anddb
InformationWriteConfig
obtainconfig
Content is written back to the configuration fileInitDB
obtaindb
Information , Assign to global variable , obtaingorm.DB
client
about withValue
There are four precautions for the use of :
- Not recommended
context
Values pass key parameters , The key parameters should be declared as shown , Should not be handled implicitly - carry
value
It's alsokey
、value
In the form of , for fear ofcontext
Because multiple packages are used at the same timecontext
And bring conflict ,key
Built in type is recommended - Get key value timing , We should start from the current
context
Search for , Not found will be from the fathercontext
Find the value corresponding to the key in until it is in a parentcontext
Back innil
Or find the corresponding value context
In the data transferredkey
、value
All areinterface
type , This type cannot be determined at compile time , So it's not very safe , So don't forget to ensure the robustness of the program when asserting types
2.2 WithCancel Cancel control
In daily business development, many meetings are held to complete a complex requirement goroutine
To do something , This leads us to open multiple... In one request goroutine
I really can't control them , And then we can use it withCancel
To derive a context
Pass to different goroutine
in , When you want these goroutine
Stop running , You can call cancel
To cancel
The example here is the follow-up to the above InitTables
and InitData
part
func (h MysqlInitHandler) InitTables(ctx context.Context, inits initSlice) error {
return createTables(ctx, inits)
}
// createTables Create table ( Default dbInitHandler.initTables Behavior )
func createTables(ctx context.Context, inits initSlice) error {
next, cancel := context.WithCancel(ctx)
defer func(c func()) {
c() }(cancel)
return nil
}
func (h MysqlInitHandler) InitData(ctx context.Context, inits initSlice) error {
next, cancel := context.WithCancel(ctx)
defer func(c func()) {
c() }(cancel)
return nil
}
however GVA
There are not many examples in this part of goroutine
Of , More is to show context
Context messaging
So cancel the control , Look at the next example to show more context
control goroutine
func main() {
ctx,cancel := context.WithCancel(context.Background())
go CancelText(ctx)
time.Sleep(10*time.Second)
cancel()
time.Sleep(1*time.Second)
}
func CancelText(ctx context.Context) {
for range time.Tick(time.Second){
select {
case <- ctx.Done():
fmt.Println("stop time")
return
default:
fmt.Println("start time")
}
}
}
Use WithCancel
Create one based on Background
Of ctx
; main
Function main coroutine call CancelText
Zixie Cheng , The sub process keeps printing start time
, until ctx.Done() Successfully closed
The main process is 10s After execution cancel,CancelText The subprocess exits after detecting the cancellation signal
Reference resources
[1] Go Concurrency Patterns: Context
[2] Xiaobai can also understand context Bao explained in detail : From entry to mastery
边栏推荐
- Egr-20uscm ground fault relay
- 消息队列:重复消息如何处理?
- [paper reading] semi supervised left atrium segmentation with mutual consistency training
- The navigation bar changes colors according to the route
- 论文阅读【Sensor-Augmented Egocentric-Video Captioning with Dynamic Modal Attention】
- 【已解决】记一次EasyExcel的报错【读取xls文件时全表读不报错,指定sheet名读取报错】
- How does mapbox switch markup languages?
- When deleting a file, the prompt "the length of the source file name is greater than the length supported by the system" cannot be deleted. Solution
- SAP ABAP BDC(批量数据通信)-018
- 拼多多新店如何获取免费流量,需要从哪些环节去优化,才能有效提升店内免费流量
猜你喜欢
[论文阅读] Semi-supervised Left Atrium Segmentation with Mutual Consistency Training
Introduction to distributed transactions
What is dependency injection (DI)
Simple case of SSM framework
Mapbox Chinese map address
The 2022 China low / no code Market Research and model selection evaluation report was released
Preliminary practice of niuke.com (9)
Message queue: how to deal with message backlog?
Realize GDB remote debugging function between different network segments
爬虫练习题(三)
随机推荐
Taobao commodity details page API interface, Taobao commodity list API interface, Taobao commodity sales API interface, Taobao app details API interface, Taobao details API interface
关于服装ERP,你知道多少?
集群、分布式、微服務的區別和介紹
MySQL数据库学习(7) -- pymysql简单介绍
Codeforces Round #416 (Div. 2) D. Vladik and Favorite Game
C#可空类型
Intelligent annotation scheme of entity recognition based on hugging Face Pre training model: generate doccano request JSON format
WEB架构设计过程
AI face editor makes Lena smile
5阶多项式轨迹
Educational Codeforces Round 22 B. The Golden Age
Leakage relay jd1-100
High voltage leakage relay bld-20
Five core elements of architecture design
1. AVL tree: left-right rotation -bite
JSP setting header information export to excel
说一说MVCC多版本并发控制器?
TCC of distributed transaction solutions
淘宝店铺发布API接口(新),淘宝oAuth2.0店铺商品API接口,淘宝商品发布API接口,淘宝商品上架API接口,一整套发布上架店铺接口对接分享
导航栏根据路由变换颜色