• The brain is a g...
    2023-02-20 来自广东
    收到结束信号后,还会接受新的请求吗,这样无限请求进来是不是结束不了?

    作者回复: 不会的,有个inShutdown的标记位,标记了就没有请求进来了

    
    
  • 友
    2021-12-03
    ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) 即可

    作者回复: yes,u are right

    
    
  • 安喆||工程师||电...
    2021-09-24
    老师,这个课程的开发环境是什么,如何搭建?

    编辑回复: 第1节课有详细说明,Golang 源码的版本是 1.15.5,实现的步骤就是按文章顺序读,代码可以参考文中的代码和GitHub地址。

    
    
  • 贺鹏
    2021-09-26
    quit := make(chan os.Signal) signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT) <-quit ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() if err := srv.Shutdown(ctx); err != nil { log.Fatal("server shutdown error:", err) } select { case <-ctx.Done(): log.Println("timeout of 5 seconds") } log.Println("server exiting")
    共 2 条评论
    6
  • void
    2021-11-12
    “它们发送的信号是进入 main 函数的,即只有 main 函数所在的 Goroutine 会接收到,所以必须在 main 函数所在的 Goroutine 监听信号。” 这个说法不太对吧? 别的goroutine里也是可以收到信号的,只不过在主协程里接收信息比较容易收尾后退出整个进程。
    
    5
  • 陈亦凡
    2021-09-26
    第一节已经值回票价,希望老师快点更新
    
    4
  • Geek_5244fa
    2021-09-24
    必须在 main 函数所在的 Goroutine 监听信号吗?在另一个 goroutine 理处理也可以吧: func main() { done := make(chan struct{}) go func() { quit := make(chan os.Signal) signal.Notify(quit, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT) fmt.Printf("singal %v", <-quit) close(done) }() <-done }
    
    3
  • 语法糖
    2022-10-15 来自江苏
    "10s 内在控制台执行 Ctrl+C 关闭程序观察控制台程序是否不会立刻结束,而是在 10s 后结束" 前提是请求开始到ctrl+c 之间超过5s, 否则留给剩余请求的时间大于5s, 超过了服务器关闭的最大超时5s, 会强制关闭服务器
    共 1 条评论
    2
  • 芒果少侠
    2021-10-03
    自己的答案,看完后发现skyhackvip同学的写法似乎更好,不用多起一个协程 quitSig := make(chan os.Signal) signal.Notify(quitSig, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM) <-quitSig cancelableCtx, cancel := context.WithCancel(context.Background()) inAdvance := time.After(time.Second * 5) go func() { select { case <-inAdvance: log.Print("server shutdown exceeds timeout") cancel() } }() if err := server.Shutdown(cancelableCtx); err != nil { log.Fatal("server shutdown failed: ", err) }
    
    1
  • Geek_eebc7f
    2023-06-06 来自俄罗斯
    func mian(){ .... cancelContext, _ := context.WithTimeout(context.Background(), time.Second*5) if err := server.Shutdown(cancelContext); err != nil { log.Fatal("Server Shutdown:", err) } }
    
    