Skip to content

Server and configuration

serve

serve(config: ConfigOptions) -> None

Run the grpc router until it is stopped.

When called from the main thread, SIGTERM and SIGINT trigger a graceful shutdown: the health status turns NOT_SERVING, new RPCs are rejected and in-flight RPCs get config.shutdown_grace seconds to complete. A second signal stops immediately.

create_server

create_server(config: ConfigOptions) -> grpc.Server

Create a (not yet started) grpc router server bound to config.hostname:config.port.

With config.state_file the register is loaded from it now and saved in the background; use serve() to also get the final save on shutdown.

ConfigOptions dataclass

Configuration of the router.

Attributes:

Name Type Description
hostname str

Hostname or address to bind to, e.g. "[::]" for all interfaces.

port int

Port to bind to.

max_workers int

Maximum number of requests handled concurrently.

allow_global_region bool

Allow services to register without a region (the global region), and fall back to global services when a client's region has none.

allow_cross_region_connectivity bool

Fall back to services of other regions when neither the client's region nor the global region has any.

health_check_timeout int

Seconds without a heartbeat after which an ACTIVE_CLIENT service is taken out of service.

unhealthy_expiry_timeout int

Seconds without any health push after which an out of service instance is forgotten; it then has to register again.

shutdown_grace float

Seconds in-flight requests get to complete on SIGTERM/SIGINT.

state_file str | None

Path of the JSON file to save the register to and reload it from on start; None disables persistence.

state_save_interval float

The state file is saved at most once per this many seconds.

HealthCheckType

How the router checks that a registered service is alive.

Attributes:

Name Type Description
NONE

No health checks: the service stays registered until it deregisters.

ACTIVE_CLIENT

The service pushes heartbeats to the router (GRPCRouterClient does it).

PASSIVE_CLIENT

The router polls the service. Not implemented yet.

HealthStatus

Health status a service reports with its heartbeats.

ERROR takes the service out of service; any other status keeps it in, or puts it back in, service.