Skip to content

Client

GRPCRouterClient

Client of the grpc router, for services (to register) and for their clients (to look them up).

Registered ACTIVE_CLIENT services get heartbeats sent in a background thread every HEALTH_PUSH_FREQUENCY_SECONDS, and straight away when set_health_status is called. If the router no longer knows a service (it restarted without a state file, or the service was out of service and silent for too long), the client registers it again on its next heartbeat.

Use it as a context manager, or call close when done, to stop the heartbeats and close the channel.

Parameters:

Name Type Description Default
host str

Host of the router.

required
port int

Port of the router.

required
grpc_service_config dict[str, Any] | None

gRPC service config for the channel, e.g. its retry policy. Defaults to DEFAULT_GRPC_SERVICE_CONFIG, which retries failed calls up to 5 times with backoff.

None

channel property

channel: Channel

The gRPC channel to the router, created on first use.

stub property

stub: GRPCRouterServiceStub

The generated gRPC stub of the router service, for direct calls.

register_service

register_service(service_id: str, host: str, port: int, region: str = '', slots: int = 10, health_check_type: int = 0) -> str

Register a service instance with the router.

Registering the same service_id again with the same client is a no-op that returns the existing token.

Parameters:

Name Type Description Default
service_id str

Name of the service, ideally a dot separated namespace, e.g. "myorg.billing.invoices".

required
host str

Host clients connect to.

required
port int

Port clients connect to.

required
region str

Region of the instance; "" is the global region.

''
slots int

Capacity hint of the instance (not used by the router yet).

10
health_check_type int

A HealthCheckType; ACTIVE_CLIENT starts sending heartbeats.

0

Returns:

Type Description
str

The service token. It can change if the client has to register the service again.

deregister_service

deregister_service(service_id: str) -> None

Deregister a service registered by this client (no-op if it is not registered).

get_service

get_service(service_id: str, region: str = '') -> tuple[str, int]

Look up an instance of a service, round robin.

The router prefers instances of the given region, then of the global region, then of any region, depending on its configuration.

Returns:

Type Description
tuple[str, int]

The (host, port) to connect to.

Raises:

Type Description
RpcError

With code NOT_FOUND if no instance is in service.

set_health_status

set_health_status(service_id: str, status: int, description: str) -> None

Set the health status of a service registered by this client, and push it straight away.

HealthStatus.ERROR takes the service out of service until a healthy status is set again.

Parameters:

Name Type Description Default
service_id str

The registered service.

required
status int

A HealthStatus (or the equivalent HEALTH_STATUS_* value of the generated stubs).

required
description str

Free text, e.g. why the service is unhealthy.

required

close

close() -> None

Stop pushing health statuses and close the channel. Services are not deregistered.