登录
首页 >  Golang >  Go问答

Golang 在信号中断时干净关闭

来源:stackoverflow

时间:2024-02-19 17:00:15 116浏览 收藏

“纵有疾风来,人生不言弃”,这句话送给正在学习Golang的朋友们,也希望在阅读本文《Golang 在信号中断时干净关闭》后,能够真的帮助到大家。我也会在后续的文章中,陆续更新Golang相关的技术文章,有好的建议欢迎大家在评论留言,非常感谢!

问题内容

一旦收到操作系统中断信号,我正在尝试找到一种有效的方法来关闭所有 go 例程。在这里,我轮询事件(比如来自某个队列)并在 goroutine 中处理它。但是当我收到操作系统中断时,我想确保正在运行的作业在终止之前完成。只有在所有 goroutine 完成后,我还需要做一些额外的事情。下面的代码似乎对我来说工作得很好,但是有没有更好/有效的方法来做到这一点?

package main

import (
    "fmt"
    "os"
    "os/signal"
    "sync"
    "syscall"
    "time" // or "runtime"
)

func something(wg *sync.WaitGroup){
    defer wg.Done()
    fmt.Println("something is happening here...")
    time.Sleep(10 * time.Second)
    fmt.Println("job done...")
}

func main() {

    c := make(chan os.Signal)
    mutex := sync.Mutex{}
    stop := make(chan int, 1)
    signal.Notify(c, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
    wg := sync.WaitGroup{}
    count := 0
    go func() {
        <-c
        currentTime := time.Now()
        fmt.Println("Interrupt signal got at: ", currentTime.String())
        // do not let the code shutdown without running everything we needed to do
        mutex.Lock()
        stop <- 1
        fmt.Println("Done .. try shutting down")
        wg.Wait()

        // do cleanup
        time.Sleep(3*time.Second)

        fmt.Println("All cleanup completed .. shut down")
        currentTime = time.Now()
        fmt.Println("Kill at : ", currentTime.String())
        mutex.Unlock()
    }()

    // This for loop is for reading messages from queue like sqs, and it has to be infinite loop because there might be scenarios where there are no events for period of time.
    for {            
        // read off of queue
        select {
        case stop <- 1:
            fmt.Println("Not stopped yet")
            wg.Add(1)
            go something(&wg)
            <- stop
            count ++
        default:
            // try getting the lock before exiting (so that other cleanups are done)
            mutex.Lock()
            fmt.Println("Done! All jobs completed: Jobs count",count)
            return
        }
        fmt.Println("Processing job -", count)
        time.Sleep(1 * time.Second)
    }
}

解决方案


我认为您的解决方案过于复杂。这可能是正确的,但这要简单得多:

func goroutine(wg *sync.WaitGroup,stop chan struct{}) {
  defer wg.Done()
   for {
     select {
       case <-stop:
          return
       default:
     }
    // do stuff
  }
}

func main() {
 stop:=make(chan struct{})

 // Setup signal handlers
 go func() {
        <-c
        // This will send the stop signal to all goroutines
        close(stop)
 }()
 // Start goroutines
 ...
 // wait for them to finish
wg.Wait()

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《Golang 在信号中断时干净关闭》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>