当前位置:网站首页>How to solve the problem of golang select mechanism and timeout

How to solve the problem of golang select mechanism and timeout

2022-07-07 09:29:00 Yisu cloud

golang select How to solve the mechanism and timeout problems

This article mainly introduces golang select Mechanism and how to solve the timeout problem , The content is detailed and easy to understand , The operation is simple and fast , It has certain reference value , I believe that after reading this article golang select How to solve the problem of mechanism and timeout will be fruitful , Let's have a look .

golang The coroutine in is very convenient to use , But when the collaboration process ends is a control problem , It can be used select In combination with .

The communication between the child and parent processes usually uses context perhaps chan. I came across a common usage scenario , Try to process more than once in the subprocess , The parent process timed out waiting for a period of time , I choose to use chan Realization . I thought select and C++ in switch similar , So the initial code looks like this :

for {    select {        case <-ctx.Done():            // process ctx done        case <-time.After(time.Second * 3):            // process after        default:            // process code    }}

The test found that... Could not be realized timeout, Check the document carefully , Only then discovered golang in select There's another mystery . Don't talk nonsense , Summarize the main points directly :

  • select Medium case Must be carried out chan Manipulation of , So it can only be in case In the operation chan, And is   Non blocking reception  .

  • select Medium case Monitoring at the same time , Multiple case Simultaneous operation , Not switch Judge one by one . If more than one case Meet the requirements , Execute a random , If one does not exist, the current coroutine will be blocked ( No, default Under the circumstances ).  Is very similar Linux File symbol operation select semantics  .

  • There is no blockage mentioned above default Under the circumstances , If there is default, execute default, And then quit select, That is, the current process will not be blocked .

Go back to the above code , This is me. select Will continue to implement default, time.After  Generated chan Will not be blocked to judge , So I can't achieve the effect I want . After understanding, modify the code again :

done := make(char int)go func(c chan int) {    for {        // process code        if {            c <- 1            return        }    }    c <- 0}(done)select {    case <-ctx.Done():        // process ctx done    case <-time.After(time.Second * 3):        // process after    case <-done:        // process code}

Open a new process to try again and again , Three outside case There is a satisfaction , Will perform . But there is a problem that needs attention :  When does the subprocess exit ? .

because gorountine Can't be forced kill, So in the case of the above timeout ,select Statement execution  case time.After  Then quit , done  This chan There is no receiver , So there is no receiver , There is no buffer , combination chan Characteristics of , The subprocess will always be blocked and cannot exit , So in essence, this implementation will lead to the accumulation of sub processes , That is to say   Xiecheng leakage  , May run out of resources .

How to avoid the above problems ? A very simple idea is to provide buffers , done := make(char int, 1) , So even if there is no receiver , The subprocess can also be sent , Not blocked .

There's another way , It says ,select operation chan, And you can specify default, Is there a way of thinking ?

if {    select {        case done <- 1:        default:            return    }}

About “golang select How to solve the mechanism and timeout problems ” That's all for this article , Thank you for reading ! I'm sure you're right “golang select How to solve the mechanism and timeout problems ” Knowledge has a certain understanding , If you want to learn more , Welcome to the Yisu cloud industry information channel .

原网站

版权声明
本文为[Yisu cloud]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070645060991.html