medibloc/panacea-coracle

Add graceful shutdown

Closed this issue · 2 comments

Background

Despite the data-validator is a HTTP service, we haven't implemented graceful shutdown yet:
https://github.com/medibloc/panacea-data-market-validator/blob/7b68929cb47786277c78bda30d5cad5f78d16e83/cmd/datavald/main.go#L20

What needs to be done

  • Implement graceful shutdown by referring to https://github.com/gorilla/mux#graceful-shutdown.
    • You may need to know how goroutine and channel work.
  • When the graceful shutdown is triggered, we have to finalize not only the HTTP service, but also any other long-living resources such as gRPC client connections.
    • Pls note that it's so important to finalize the HTTP service first. Only after the HTTP service is finalized, the application doesn't have any running goroutine for handling HTTP requests. Then, you can finally finalize other resources.

I'm not sure I understand the meaning of finalizing the HTTP service exactly.
Please check what I understood is right.

When application shutdown while our handler func handles request (gRPC, encryption, etc.), we have to close in order of
all logic inside handler(immediate stop of all logic) -> gRPC connection -> server?
Or we don't have to consider the logic inside the handler?

@hansol-medi
Sorry for a late answer.

Fortunately, we can just call a http.Server.Shutdown() which all running connections (goroutines) become idle. So, you don't need to worry about how to shutting down all running HTTP goroutines.
I would call the http.Server.Shutdown() first. After that function is returned, there is no running or incoming connection. Then, you can safely all resources, such as gRPC client connections, which have been used by HTTP connections (goroutines).