亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我正常停止http.ListenAndServe失敗

我正常停止http.ListenAndServe失敗

Go
臨摹微笑 2021-04-09 22:18:02
我是Go程序員的新手。我正在編寫一個Web應用程序,我需要服務器等待活動請求發出后再關閉。我寫了一個處理程序,等待5秒鐘回答。如果我發出請求并停止服務器(在5秒鐘之前),則會收到“無法連接”錯誤。有沒有一種方法可以停止監聽新請求并等待活動請求完成?這是我的例子func main() {    log.Infof("Starting (PID %d)...", os.Getpid())    stop := make(chan os.Signal, 1)    signal.Notify(stop, syscall.SIGTERM)    signal.Notify(stop, syscall.SIGINT)    listenAt := "127.0.0.1:8000"    r := newRouter()    h := &http.Server{Addr: listenAt, Handler: r}    go func() {        log.Info("Serving on http://", listenAt)        if err := h.ListenAndServe(); err != nil {            log.Fatal(err)        }    }()    <-stop    log.Info("Stoping ...")    h.Shutdown(context.Background())    log.Info("Bye :)")}示例處理程序func handler(w http.ResponseWriter, r *http.Request) {    time.Sleep(5 * time.Second)    log.Info("new request")    fmt.Fprintf(w, "Hola!")}完整示例@ https://gist.github.com/nachopro/d80fa71ae49527e1ddcaf359b4ff488b
查看完整描述

2 回答

  • 2 回答
  • 0 關注
  • 545 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號