|
| 1 | +## Graceful shutdown |
| 2 | + |
| 3 | +By default, when a user-based service receives a SIGTERM or SIGINT signal, it swiftly closes all active connections and halts all components. |
| 4 | + |
| 5 | +However, in certain scenarios, a service might need to shut down gracefully. This means it should inform its clients of the impending shutdown, provide them with extra time to submit any pending requests, and attempt to complete all ongoing operations. This process is known as a graceful shutdown. |
| 6 | + |
| 7 | +Graceful shutdown is deactivated by default. It can be activated by setting the `graceful_shutdown_interval` parameter in the configuration of the components::ManagerControllerComponent. |
| 8 | + |
| 9 | +\b static_config.yaml example: |
| 10 | +@snippet core/functional_tests/graceful_shutdown/static_config.yaml graceful_shutdown_interval |
| 11 | + |
| 12 | +With such configurations a service will switch to *graceful shutdown* mode after SIGTERM or SIGINT. |
| 13 | +It corresponds to @ref components::ServiceLifetimeStage::kGracefulShutdown service lifetime stage. |
| 14 | + |
| 15 | +At this stage the service will: |
| 16 | + |
| 17 | +* Start failing HTTP and gRPC health checks: |
| 18 | + * HTTP @ref scripts/docs/en/userver/tutorial/production_service.md "/ping" handler will return 500 errors. |
| 19 | + * gRPC `grpc.health.v1.Health` service will return `NOT_SERVING` status. |
| 20 | +* Append @ref graceful_shutdown_headers "special headers" to HTTP responses and gRPC response metadata, |
| 21 | + if enabled. |
| 22 | +* Accept new HTTP and gRPC requests and continue processing of ongoing requests for the configured time |
| 23 | + interval (10 seconds in the example above). |
| 24 | + |
| 25 | +@anchor graceful_shutdown_headers |
| 26 | +### Graceful shutdown headers |
| 27 | + |
| 28 | +During a graceful shutdown, a service appends special HTTP headers and gRPC metadata to outgoing responses, provided the feature is enabled. Typically, the initial metadata is employed for gRPC. However, if a graceful shutdown is triggered after the initial metadata has already been sent, trailing metadata will be utilized instead. |
| 29 | + |
| 30 | +Graceful shutdown headers are enabled by default. They could be configured or disabled via @ref GRACEFUL_SHUTDOWN_HEADERS dynamic configuration. |
| 31 | + |
| 32 | +@example grpc/functional_tests/middleware_server/tests/test_graceful_shutdown_headers.py |
| 33 | + |
| 34 | +---------- |
| 35 | + |
| 36 | +@htmlonly <div class="bottom-nav"> @endhtmlonly |
| 37 | +⇦ @ref scripts/docs/en/userver/long_transactions.md | @ref scripts/docs/en/userver/caches.md ⇨ |
| 38 | +@htmlonly </div> @endhtmlonly |
0 commit comments