HTTP server connection draining(http server优雅的关闭)

转载:http://colobu.com/2016/11/05/golang-18-whats-coming/?utm_source=tuicool&utm_medium=referral


Brad Fitzpatrick最近关闭了一个将近四年的issue,这个issue请求实现http.Server的连接耗尽(draining)的功能。现在可以调用srv.Close可以立即停止http.Server,也可以调用srv.Shutdown(ctx)等待已有的连接处理完毕(耗尽,draining, github.com/tylerb/graceful 的用户应该熟悉这个特性)。

下面这个例子中,服务器当收到SIGINT信号后(^C)会优雅地关闭。


package main
import (
	"context"
	"io"
	"log"
	"net/http"
	"os"
	"os/signal"
	"time"
)
func main() {
	// subscribe to SIGINT signals
	stopChan := make(chan os.Signal)
	signal.Notify(stopChan, os.Interrupt)
	mux := http.NewServeMux()
	mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		time.Sleep(5 * time.Second)
		io.WriteString(w, "Finished!")
	}))
	srv := &http.Server{Addr: ":8081", Handler: mux}
	go func() {
		// service connections
		if err := srv.ListenAndServe(); err != nil {
			log.Printf("listen: %s\n", err)
		}
	}()
	<-stopChan // wait for SIGINT
	log.Println("Shutting down server...")
	// shut down gracefully, but wait no longer than 5 seconds before halting
	ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
	srv.Shutdown(ctx)
	log.Println("Server gracefully stopped")
}



一旦收到SIGINT信号,服务器会立即停止接受新的连接,srv.ListenAndServe()会返回http.ErrServerClosedsrv.Shutdown会一直阻塞,直到所有未完成的request都被处理完以及它们底层的连接被关闭。

更复杂的处理可以通过context实现,例如使用context.Timeout实现最大的关闭等待时间。你可以尝试复制 https://github.com/tylerchr/examples/tree/master/draining 中的例子并实现它。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值