-
Notifications
You must be signed in to change notification settings - Fork 449
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
guides/features: add document for how to gracefully shutdown server #1388
base: main
Are you sure you want to change the base?
Changes from 6 commits
d2aa9ab
4f1bf98
37f05fc
2ed94db
07ec527
53a97a5
a144c96
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,88 @@ | ||||||||||||||||||||||||
--- | ||||||||||||||||||||||||
title: Graceful Shutdown | ||||||||||||||||||||||||
description: >- | ||||||||||||||||||||||||
Explains how to gracefully shut down [or stop, if you prefer] a gRPC server | ||||||||||||||||||||||||
to avoid causing RPC failures for connected clients. | ||||||||||||||||||||||||
--- | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
### Overview | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
gRPC servers often need to shut down gracefully, ensuring that in-flight RPCs | ||||||||||||||||||||||||
are completed within a reasonable time frame and new RPCs are no longer | ||||||||||||||||||||||||
accepted. The "Graceful shutdown function" facilitates this process, allowing | ||||||||||||||||||||||||
the server to transition smoothly without abruptly terminating active | ||||||||||||||||||||||||
connections. | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
When the "Graceful shutdown function" is called, the server immediately | ||||||||||||||||||||||||
notifies all clients that they should stop sending new RPCs. Then after the | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: "that they should" -> "to" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||||||||||||||||||||||||
clients have received that notification, the server will stop accepting new | ||||||||||||||||||||||||
RPCs. In-flight RPCs are allowed to continue until they complete or a specified | ||||||||||||||||||||||||
deadline is reached. Once all active RPCs finish or the deadline expires, the | ||||||||||||||||||||||||
server shuts down completely. | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
Because graceful shutdown helps prevent clients from encountering RPC failures, | ||||||||||||||||||||||||
it should be used if possible. However, gRPC also provides a forceful shutdown | ||||||||||||||||||||||||
mechanism which will immediately cause the server to stop serving and close all | ||||||||||||||||||||||||
connections, which results in the failure of any in-flight RPCs. | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
### How to do Graceful Server Shutdown | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
The exact implementation of the "Graceful shutdown function" varies depending on | ||||||||||||||||||||||||
the programming language you are using. However, the general pattern | ||||||||||||||||||||||||
involves: | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
- Initiating the graceful shutdown process by calling the "Graceful shutdown | ||||||||||||||||||||||||
function" on your gRPC server object. This function blocks until all | ||||||||||||||||||||||||
currently running RPCs complete. This ensures that in-flight requests are | ||||||||||||||||||||||||
allowed to finish processing. | ||||||||||||||||||||||||
- Specify a timeout period to limit the time allowed for in-progress RPCs to | ||||||||||||||||||||||||
finish. It's crucial to separately call the "Forceful shutdown function" on | ||||||||||||||||||||||||
the server object using a timer mechanism (depending on your language) to | ||||||||||||||||||||||||
trigger a forceful shutdown after a predefined duration. This | ||||||||||||||||||||||||
acts as a safety net, ensuring that the server eventually shuts down even if | ||||||||||||||||||||||||
some in-flight RPCs don't complete within a reasonable time frame. This | ||||||||||||||||||||||||
prevents indefinite blocking. | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
The following shows the sequence of events that occur, when a server's graceful | ||||||||||||||||||||||||
stop is invoked, in-flight RPCs continue to process, but new RPCs are | ||||||||||||||||||||||||
rejected. If some in-flight RPCs are not finished in time, server is forcefully | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a run-on sentence, and has an unnecessary comma. Also stop->shutdown for consistency. The following shows the sequence of events that occur during the graceful shutdown process. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated |
||||||||||||||||||||||||
shutdown. | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Verb form, so should be "shut down" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated |
||||||||||||||||||||||||
```mermaid | ||||||||||||||||||||||||
sequenceDiagram | ||||||||||||||||||||||||
Client->>Server: New RPC Request 1 | ||||||||||||||||||||||||
Client->>Server: New RPC Request 2 | ||||||||||||||||||||||||
Server-->>Server: Graceful Shutdown Invoked | ||||||||||||||||||||||||
Server->>Client: Continues Processing In-Flight RPCs | ||||||||||||||||||||||||
Client->>Client: Detects server shutdown and go finds other servers if available | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove "go" here please. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||||||||||||||||||||||||
alt RPCs complete within timeout | ||||||||||||||||||||||||
Server->>Client: Completes RPC 1 | ||||||||||||||||||||||||
Server->>Client: Completes RPC 2 | ||||||||||||||||||||||||
Server-->>Server: Graceful Shutdown Complete | ||||||||||||||||||||||||
else Timeout reached | ||||||||||||||||||||||||
Server->>Client: Forceful Shutdown Invoked, terminating pending RPCs | ||||||||||||||||||||||||
Server-->>Server: Forceful Shutdown Complete | ||||||||||||||||||||||||
end | ||||||||||||||||||||||||
``` | ||||||||||||||||||||||||
The following is a state based view | ||||||||||||||||||||||||
```mermaid | ||||||||||||||||||||||||
stateDiagram-v2 | ||||||||||||||||||||||||
[*] --> RUNNING : Server Started | ||||||||||||||||||||||||
RUNNING --> GRACEFUL_STOP_INITIATED : Graceful Stop Called (with Timeout) | ||||||||||||||||||||||||
GRACEFUL_STOP_INITIATED --> GRACEFUL_STOPPING | ||||||||||||||||||||||||
GRACEFUL_STOPPING --> TERMINATED : In-Flight RPCs Completed (Before Timeout) | ||||||||||||||||||||||||
GRACEFUL_STOPPING --> TIMER_EXPIRED : Timeout Reached | ||||||||||||||||||||||||
TIMER_EXPIRED --> TERMINATED : Force Stop Called | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed |
||||||||||||||||||||||||
``` | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
### Language Support | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
| Language | Example | | ||||||||||||||||||||||||
|----------|------------------| | ||||||||||||||||||||||||
| C++ | | | ||||||||||||||||||||||||
| Go | [Go Example] | | ||||||||||||||||||||||||
| Java | [Java Example] | | ||||||||||||||||||||||||
| Python | | | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
[Go example]: https://github.com/grpc/grpc-go/tree/master/examples/features/gracefulstop | ||||||||||||||||||||||||
[Java example]: https://github.com/grpc/grpc-java/tree/master/examples/example-hostname/src/main/java/io/grpc/examples/hostname |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The brackets were there for you to read and choose either "stop" or "shut down", not to copy verbatim. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yeah. Forgot to remove. Removed now.