当前位置:网站首页>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
边栏推荐
- Arduino UNO r3+LCD1602+DHT11
- AQS源码阅读
- Learning Android II from scratch - activity
- Cross border e-commerce | Facebook and instagram: which social media is more suitable for you?
- [database] MySQL basic operation (basic operation ~)
- Wechat payment function
- 洛谷P2731骑马修栅栏
- C# List字段排序含有数字和字符
- [WinUI3]編寫一個仿Explorer文件管理器
- Innovation training (V) mid term inspection
猜你喜欢
CLion+OpenCV identify ID number - detect ID number
泰克示波器DPO3054自校准SPC失败维修
A trinomial expression that causes a null pointer
Wechat payment function
#define 定义常量和宏,指针和结构体
Thoughts on a small program
Solve valueerror: argument must be a deny tensor: 0 - got shape [198602], but wanted [198602, 16]
直播带货表格模板-自动显示图片-自动关联系列商品
Learning Android V from scratch - UI
MySQL slow query
随机推荐
持续集成(CI)/持续交付(CD)如何彻底改变自动化测试
Barcode generation and decoding, QR code generation and decoding
Innovation training (XII) reptile
Analysis of POM files
View, modify and delete [database] table
Docker installation and mysql5 7 installation
Differences between redis and MySQL
什么是指令周期,机器周期,和时钟周期?
机器学习---线性回归
Introduction to load balancing
How can continuous integration (CI) / continuous delivery (CD) revolutionize automated testing
[winui3] Écrivez une copie du gestionnaire de fichiers Explorer
数据安全问题已成隐患,看vivo如何让“用户数据”重新披甲
Mac 进入mysql终端命令
Manually write smart pointer shared_ PTR function
TypeError: ‘Collection‘ object is not callable. If you meant to call the ......
Raspberry pie + opencv + opencv -- face detection ------- environment construction
MySQL 慢查询
Unity C# 网络学习(四)
信息学奥赛一本通 1212:LETTERS | OpenJudge 2.5 156:LETTERS