当前位置:网站首页>Golang select priority execution
Golang select priority execution
2022-04-23 05:04:00 【canger_】
select key word
go In language select Keywords are used to monitor case The statement corresponds to chan, And execute the corresponding code under it , until case The execution of the next code will end chan The following code . It's kind of like switch.
select {
case x := <-ch1:
fmt.Println(x)
case ch2 <- 1:
fmt.Println("push 1")
}
empty select
select{
}
Empty select Statement directly blocks the current goroutine, Make the goroutine Enter a sleep state that cannot be awakened .
only one case
select {
case <-ch1:
fmt.Println("recv")
}
The above code , When ch1 Print operation will be performed when it is readable , Otherwise, it will block .
Yes default sentence
select {
case <-ch1:
fmt.Println("recv")
default:
time.Sleep(time.Second)
}
The above code , When ch1 Print operation will be performed when it is readable , Otherwise, it will be executed default Code in statement , This is equivalent to doing a non blocking channel Read operation
summary
- select There is no such thing as case: Permanent blocking current goroutine
- select There is only one case: Blocked sending / receive
- select There are multiple case: Randomly select one that meets the conditions case perform
- select There is default, other case When they are not satisfied : perform default Code in statement
Realization select Priority in
Reference resources k8s Priority in the source code select To deal with
func (tc *NoExecuteTaintManager) worker(worker int, done func(), stopCh <-chan struct{
}) {
defer done()
// When processing events we want to prioritize Node updates over Pod updates,
// as NodeUpdates that interest NoExecuteTaintManager should be handled as soon as possible -
// we don't want user (or system) to wait until PodUpdate queue is drained before it can
// start evicting Pods from tainted Nodes.
for {
select {
case <-stopCh:
return
case nodeUpdate := <-tc.nodeUpdateChannels[worker]:
tc.handleNodeUpdate(nodeUpdate)
tc.nodeUpdateQueue.Done(nodeUpdate)
case podUpdate := <-tc.podUpdateChannels[worker]:
// If we found a Pod update we need to empty Node queue first.
priority:
for {
select {
case nodeUpdate := <-tc.nodeUpdateChannels[worker]:
tc.handleNodeUpdate(nodeUpdate)
tc.nodeUpdateQueue.Done(nodeUpdate)
default:
break priority
}
}
// After Node queue is emptied we process podUpdate.
tc.handlePodUpdate(podUpdate)
tc.podUpdateQueue.Done(podUpdate)
}
}
}
I need this priority right now select The application scenario is generally , When my program catch When the abnormal signal needs to exit , I'll put... Before I quit select Let's do something else chan Exit after all execution
// Abnormal exit signal
var kExitSignals = []os.Signal{
syscall.SIGTERM,
syscall.SIGINT,
syscall.SIGSEGV,
syscall.SIGBUS,
syscall.SIGQUIT,
}
int main() {
exitChan := make(chan os.Signal, 1)
signal.Notify(exitChan, kExitSignals...)
handleCh := make(chan struct{
}, 10)
//goroutine with input handleCh
for {
select {
case <-handleCh:
//do some handle
case signl := <-exitChan:
// Received an exit signal , Also make sure to put chan Exit after processing the data in
priority:
for {
select {
case <-handleCh:
// do some handle
default:
break priority
}
}
log.Info("catch signal:", signl)
// Do some cleanup and resource recycling
//os.Exit(1)
// If it is a server, you can use server.Stop() perhaps server.GraceStop() To quit
}
}
}
版权声明
本文为[canger_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220552063893.html
边栏推荐
- Progress of innovation training (IV)
- Innovation training (VII) FBV view & CBV view
- 退出vim的方法
- DIY is an excel version of subnet calculator
- redis和mysql区别
- Sword finger offer: symmetric binary tree (recursive iteration leetcode 101)
- QPushButton slot function is triggered multiple times
- The vscode ipynb file does not have code highlighting and code completion solutions
- PHP 统计指定文件夹下文件的数量
- View analysis of scenic spots in ArcGIS
猜你喜欢
[2021] Spatio-Temporal Graph Contrastive Learning
直播带货表格模板-自动显示图片-自动关联系列商品
MySQL - index
POI export message list (including pictures)
泰克示波器DPO3054自校准SPC失败维修
Learning Android from scratch -- Introduction
Define defines constants and macros, pointers and structures
【数据库】MySQL多表查询(一)
[database] MySQL single table query
View, modify and delete [database] table
随机推荐
Wechat payment function
CLion+OpenCV identify ID number - detect ID number
Get the number of days between dates, get the Chinese date, get the date of the next Monday of the date, get the working day, get the rest day
Introduction to load balancing
MySQL memo (for your own query)
Solve valueerror: argument must be a deny tensor: 0 - got shape [198602], but wanted [198602, 16]
机器学习---线性回归
Barcode generation and decoding, QR code generation and decoding
Deep learning notes - semantic segmentation and data sets
Spell it! Two A-level universities and six B-level universities have abolished master's degree programs in software engineering!
Mac 进入mysql终端命令
View, modify and delete [database] table
Uglifyjs compress JS
Download PDF from HowNet (I don't want to use CAJViewer anymore!!!)
js 判斷數字字符串中是否含有字符
Basic concepts of multithreading (concurrency and parallelism, threads and processes) and entry cases
Wine (COM) - basic concept
Use AES encryption - reuse the wisdom of predecessors
Harmonious dormitory (linear DP / interval DP)
Backup MySQL database with Navicat