-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use ServerStreaming scenario to demonstrate graceful shutdown
- Loading branch information
1 parent
5ee5e3b
commit 36bb93c
Showing
2 changed files
with
34 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,33 @@ | ||
# Graceful Stop | ||
|
||
This example demonstrates how to gracefully stop a gRPC server using | ||
Server.GracefulStop(). | ||
`Server.GracefulStop()`. | ||
|
||
## How to run | ||
|
||
Start the server which will listen to incoming gRPC requests as well as OS | ||
interrupt signals (SIGINT/SIGTERM). After receiving interrupt signal, it calls | ||
`s.GracefulStop()` to gracefully shut down the server. If graceful shutdown | ||
doesn't happen in time, server is stopped forcefully. | ||
Start the server which will serve `ServerStreaming` gRPC requests as well as an | ||
OS interrupt signal (SIGINT/SIGTERM). `ServerStreaming` handler returns an | ||
error after sending 5 messages to the client or if client's context is | ||
canceled. If an interrupt signal is received, it calls `s.GracefulStop()` to | ||
gracefully shut down the server. If graceful shutdown doesn't happen in time, | ||
the server is stopped forcefully. | ||
|
||
```sh | ||
$ go run server/main.go | ||
``` | ||
|
||
In a separate terminal, start the client which will send multiple requests to | ||
the server with some delay between each request. | ||
In a separate terminal, start the client which will start `ServerStreaming` | ||
request to the server and keep receiving messages until an error is received. | ||
Once an error is received, it closes the stream. | ||
|
||
```sh | ||
$ go run client/main.go | ||
``` | ||
|
||
Use Ctrl+C or SIGTERM to signal the server to shut down. | ||
Once the client starts receiving messages from server, use Ctrl+C or SIGTERM to | ||
signal the server to shut down. | ||
|
||
The server begins a graceful stop: | ||
- It finishes ongoing requests. | ||
- Rejects new incoming requests. | ||
|
||
The client will notice the server's shutdown when a request fails. | ||
The server begins a graceful stop. It finish the in-flight request. In this | ||
case, client will receive 5 messages followed by the stream failure from the | ||
server, allowing client to close the stream gracefully, before shutting down. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters