How to continue to provide services during golang http server restart (elegant restart)

The

architecture is as follows:
nginx acts as a reverse proxy to proxy pass the http request to the backend golang http server

 nginx==>httpserver

is it possible to restart gracefully in this way
1. Start a new golang http server to listen on other ports
2. Modify the nginx configuration file to change the previous proxy port to the port that the newly started server listens on
3.reload nginx
4. If there is no request processing in the old http server for a period of time, the kill is dropped

graceful restart as a whole? Is this feasible and is there a better plan?

Mar.18,2021

think of two methods:

method 1:
if the kernel version 3.9 or above is newer and supports SO_REUSEPORT, then you can:

  1. starts a new process and listens on the same port as well.
  2. send a signal to the old process when the new process starts.
  3. when the old process receives it, it stops receiving new connection requests (stop Accept, close Listen Socket),
    , etc.) and automatically exit all existing connections.

if SO_REUSEPORT, is not supported and different processes cannot listen to the same port at the same time, you need to fork a child process within the old process and pass the listener file descriptor to the new process.
this method can meet your needs, but it requires a lot of modifications to system libraries such as net/http encapsulated by Golang, and the technical complexity is relatively high. The advantage of
is that it does not require nginx involvement and is transparent to it.

method 2:
you can run two or more http server, at the same time to provide services and let nginx do load balancing. When one of them needs to be upgraded and restarted, send a signal and stop receiving new requests. After the existing requests have been processed, you can exit normally. This process does not require modification of the nginx configuration, nor does it require reload nginx.
this method also needs to change the net/http, encapsulated by Golang, but the amount of modification is much smaller than that of the method.

Menu