graceful http server for golang. (平滑重启)
优雅的使用 HTTP Server
Zero-Downtime
);Server
添加(包含 HTTP
、HTTPS
);server
添加;server
进行过载保护,通过设置合适的阀值,避免 too many open files
错误;go get github.com/fevin/gracehttp
@see main_test/gracehttp_main.go
kill -HUP pid
或 kill -HUP pid
kill -QUIT pid
gracehttp.SetErrorLogCallback(logger.LogConfigLoadError)
此处提供了三个 Set*
方法,分别对应不同的日志等级:
参考实现:golang/net/netutil 实现原理:
channel-buffer
来控制并发量:每个请求都会获取一个缓冲单元,直到缓冲区满;buffer
的请求才能进行 accept
;conn.close
或者 缓冲区有空闲✗ go test -v github.com/fevin/gracehttp
=== RUN TestHTTPServer
--- PASS: TestHTTPServer (0.00s)
gracehttp_test.go:106: ******* test multi server *******
gracehttp_test.go:77: [test http server 1]
gracehttp_test.go:87: http server 1 success, response: pong by pid:5385
gracehttp_test.go:92: [test http server 2]
gracehttp_test.go:102: http server 2 success, response: pong by pid:5385
PASS
ok github.com/fevin/gracehttp 0.016s
// 以下操作需要开启 go mod
✗ git clone https://github.com/fevin/gracehttp.git
✗ go build main_test/gracehttp_main.go
✗ nohup ./gracehttp_main 2>&1 > gracehttp.log &
✗ curl http://localhost:9090/ping
pong by pid:86703
✗ kill -USR1 86703
[1] + 86703 done nohup ./bin/gracehttp_main 2>&1 > gracehttp.log
✗ cat gracehttp.log
2019/12/20 12:07:38 [gracehtto-log][Info] [Received SIG. [PID:86703, SIG:user defined signal 1]]
2019/12/20 12:07:38 [gracehtto-log][Info] [start new process success, pid 86818.]
# 再次执行 curl,发现 pid 已经变化
✗ curl http://localhost:9090/ping
pong by pid:86818
✗ kill -QUIT 86818
✗ cat gracehttp.log
2019/12/20 12:07:38 [gracehtto-log][Info] [Received SIG. [PID:86703, SIG:user defined signal 1]]
2019/12/20 12:07:38 [gracehtto-log][Info] [start new process success, pid 86818.]
2019/12/20 12:08:56 [gracehtto-log][Info] [Received SIG. [PID:86818, SIG:quit]]
@see test ./gracehttp_test.go.
@see demo ./main_test/gracehttp_main.go