The practice of graceful
Use the endless library to implement, such as Access gin:
r: = gin. Default () r.get ("/", index) endless. Listenandserve (":", R)
We write a handler function with a specified time-out:
Func index (c *gin. Context) { Duration:= c.query ("duration") Durationint, _:= StrConv. Atoi (Duration) * Time . Second) c.string (http. Statusok, duration)}
Curl http://localhost:3000/?duration=10
When using CTRL + C at the time of the test, all requests will be processed to exit, if it is run in the background, when we get the PID of the process, if used kill -9 $PID
, endless cannot catch the signal, need to usekill -s TERM $PID
Using Nginx upstream Load
Simple configuration of the Nignx:
Upstream graceful{Server127.0.0.1:8888; Server127.0.0.1:8889;} server {Listen8001; server_name localhost; Location^~ /{proxy_set_header X-real-IP $remote _addr; Proxy_set_header Host $host; Proxy_set_header http_referer $http _referer; Proxy_set_header X-forwarded-For $proxy _add_x_forwarded_for; Proxy_pass http://graceful/;Proxy_redirectdefault; } error_page - 502 503 504/50x.html; Location= /50x.html {root html; } Location~ /\.. +{deny all; }}
To make a simpler load-balancing, update the program in order to update, when the fact that a service is no longer available, the load to another, after the sequential update, to meet the needs of service hot update, of course, it is best to use a dedicated deployment script to achieve.
How do I do zero downtime restart go service?