douyun1546 2018-12-23 23:35
浏览 161
已采纳

Golang正常关闭HTTP服务器并进行错误处理

I'm in the process of getting my HTTP server to gracefully shut down. I've taken tips from the post here, and have set up my code like this so far:

func start() {

    //......

    //START HTTP/SOCKET LISTENER
    if settings.TLS {
        httpServer = makeServer("/wss", settings.TLS);
    } else {
        httpServer = makeServer("/ws", settings.TLS);
    }

    //...
}

func makeServer(handleDir string, tls bool) (*http.Server) {
    server := &http.Server{Addr: settings.IP+":"+strconv.Itoa(settings.Port)}
    http.HandleFunc(handleDir, socketInitializer)
    if tls {
        go func() {
            err := server.ListenAndServeTLS(settings.CertFile, settings.PrivKeyFile)
            if err != nil {
                serverError(err)
            }
        }()
    }else{
        go func() {
            err := server.ListenAndServe()
            if err != nil {
                serverError(err)
            }
        }()
    }

    //
    return server
}

So, I have some questions about how this all works now:

1) The answer I linked above states "NOTE: there is a chance that next line won't have time to run..." for the ListenAndServe() error handling inside the goroutine. How can I make sure that serverError() will guarantee completion? Do I need to do some sort of blocking on that thread or do I make a channel on the main thread that it can call?

2) As of right now, I'm not sure if I have a way of telling if I've Server.ShutDown() the http.Server, or if it threw an error and shut itself down. Does the ListenAndServe() error get thrown even if I used the Server.ShutDown() method? Is that why the accepted answer in the link uses err != http.ErrServerClosed when checking the ListenAndServe() error?

3) The start() function is (of course) ran on the main thread. Don't I need to block that thread somehow so the program doesn't return and exit, or does ListenAndServe() take care of that for me?

4) After I call Server.ShutDown() do I need to do anything to shut down the http.HandleFunc() listeners?

Any amount of help on any of these topics would be greatly appreciated, so don't worry if you can't touch on everything at once :)

EDIT: If anyone wants to see how I ended up getting this all to work right, you can look at this file, line 225 and on.

  • 写回答

1条回答 默认 最新

  • dongtidai6519 2018-12-24 00:19
    关注

    main function should somehow wait for all go routines which you want completely done.

    So, the 1) question: you should block on the main routine to make sure serverError can be executed.

    And the 3) question: absolutely you should block on start() or outside start() by yourself, or the program will exit immediately.

    As for 2). the doc points out:

    ListenAndServe always returns a non-nil error.

    so err != http.ErrServerClosed just works for situation Server.ShutDown(). Yet there's other errors may occur.

    4) Nothing to do to shut down the http.HandleFunc() because Server.ShutDown() will wait for requests which is processing done.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。