baseplate.lib.service_discovery
¶
Integration with Synapse’s file_output
service discovery method.
Note
Production Baseplate services have Synapse hooked up to a local HAProxy instance which will automatically route connections to services for you if you connect to the correct address/port on localhost. That is the preferred method of connecting to services.
The contents of this module are useful for inspecting the service inventory directly for cases where a blind TCP connection is insufficient (e.g. to give service addresses to a client, or for topology-aware clients like Cassandra).
A basic example of usage:
inventory = ServiceInventory("/var/lib/synapse/example.json")
backend = inventory.get_backend()
print(backend.endpoint.address)
-
class
baseplate.lib.service_discovery.
ServiceInventory
(filename)[source]¶ The inventory enumerates available backends for a single service.
Parameters: filename ( str
) – The absolute path to the Synapse-generated inventory file in JSON format.-
get_backends
()[source]¶ Return a list of all available backends in the inventory.
If the inventory file becomes unavailable, the previously seen inventory is returned.
Return type: Sequence
[Backend
]
-
get_backend
()[source]¶ Return a randomly chosen backend from the available backends.
If weights are specified in the inventory, they will be respected when making the random selection.
Raises: NoBackendsAvailableError
if the inventory has no available endpoints.Return type: Backend
-
-
class
baseplate.lib.service_discovery.
Backend
[source]¶ A description of a service backend.
This is a tuple of several values:
id
- A unique integer ID identifying the backend.
name
- The name of the backend.
endpoint
- An
EndpointConfiguration
object describing the network address of the backend. weight
- An integer weight indicating how much to prefer this backend when choosing whom to connect to.