| 1 |
GRPC Health Checking Protocol |
| 2 |
================================ |
| 3 |
|
| 4 |
Health checks are used to probe whether the server is able to handle rpcs. The |
| 5 |
client-to-server health checking can happen from point to point or via some |
| 6 |
control system. A server may choose to reply “unhealthy” because it |
| 7 |
is not ready to take requests, it is shutting down or some other reason. |
| 8 |
The client can act accordingly if the response is not received within some time |
| 9 |
window or the response says unhealthy in it. |
| 10 |
|
| 11 |
|
| 12 |
A GRPC service is used as the health checking mechanism for both simple |
| 13 |
client-to-server scenario and other control systems such as load-balancing. |
| 14 |
Being a high |
| 15 |
level service provides some benefits. Firstly, since it is a GRPC service |
| 16 |
itself, doing a health check is in the same format as a normal rpc. Secondly, |
| 17 |
it has rich semantics such as per-service health status. Thirdly, as a GRPC |
| 18 |
service, it is able reuse all the existing billing, quota infrastructure, etc, |
| 19 |
and thus the server has full control over the access of the health checking |
| 20 |
service. |
| 21 |
|
| 22 |
## Service Definition |
| 23 |
|
| 24 |
The server should export a service defined in the following proto: |
| 25 |
|
| 26 |
``` |
| 27 |
syntax = "proto3"; |
| 28 |
|
| 29 |
package grpc.health.v1; |
| 30 |
|
| 31 |
message HealthCheckRequest { |
| 32 |
string service = 1; |
| 33 |
} |
| 34 |
|
| 35 |
message HealthCheckResponse { |
| 36 |
enum ServingStatus { |
| 37 |
UNKNOWN = 0; |
| 38 |
SERVING = 1; |
| 39 |
NOT_SERVING = 2; |
| 40 |
SERVICE_UNKNOWN = 3; // Used only by the Watch method. |
| 41 |
} |
| 42 |
ServingStatus status = 1; |
| 43 |
} |
| 44 |
|
| 45 |
service Health { |
| 46 |
rpc Check(HealthCheckRequest) returns (HealthCheckResponse); |
| 47 |
|
| 48 |
rpc Watch(HealthCheckRequest) returns (stream HealthCheckResponse); |
| 49 |
} |
| 50 |
``` |
| 51 |
|
| 52 |
A client can query the server’s health status by calling the `Check` method, and |
| 53 |
a deadline should be set on the rpc. The client can optionally set the service |
| 54 |
name it wants to query for health status. The suggested format of service name |
| 55 |
is `package_names.ServiceName`, such as `grpc.health.v1.Health`. |
| 56 |
|
| 57 |
The server should register all the services manually and set |
| 58 |
the individual status, including an empty service name and its status. For each |
| 59 |
request received, if the service name can be found in the registry, |
| 60 |
a response must be sent back with an `OK` status and the status field should be |
| 61 |
set to `SERVING` or `NOT_SERVING` accordingly. If the service name is not |
| 62 |
registered, the server returns a `NOT_FOUND` GRPC status. |
| 63 |
|
| 64 |
The server should use an empty string as the key for server's |
| 65 |
overall health status, so that a client not interested in a specific service can |
| 66 |
query the server's status with an empty request. The server can just do exact |
| 67 |
matching of the service name without support of any kind of wildcard matching. |
| 68 |
However, the service owner has the freedom to implement more complicated |
| 69 |
matching semantics that both the client and server agree upon. |
| 70 |
|
| 71 |
A client can declare the server as unhealthy if the rpc is not finished after |
| 72 |
some amount of time. The client should be able to handle the case where server |
| 73 |
does not have the Health service. |
| 74 |
|
| 75 |
A client can call the `Watch` method to perform a streaming health-check. |
| 76 |
The server will immediately send back a message indicating the current |
| 77 |
serving status. It will then subsequently send a new message whenever |
| 78 |
the service's serving status changes. |