gRPC Load Balancer
grpc_load_balancer is a Python package that allows you to easily configure and manage separate gRPC server and client instances using a metrics-based load balancing approach. This is useful when you have multiple services running and want to distribute the load efficiently among them. The package includes a connection forwarder, a metrics-based server finder, and a configuration loader.
It implemetns the following design:
Features
- Forward connections from clients to gRPC servers.
- Use Prometheus metrics to select the best server to handle a request.
- Configure and manage gRPC server finder using environment variables.
Installation
You can install grpc_load_balancer using pip:
pip install grpc_load_balancer
Install promethus-client:
pip install prometheus-client
Usage
Configuring and running a gRPC server
Here's a simple example of how to use grpc_load_balancer with a gRPC server and prometeus-client:
Import the required modules:
import grpc
from grpc_load_balancer import GrpcConnnectionForwarder
from prometheus_client import start_http_server, Gauge
Initialize your gRPC server(s)
grpc_server = create_example_grpc_server() # implement this function yourself
connection_counter = Gauge('connections_num', 'Number of connections forwarded')
forwarder = GrpcConnnectionForwarder(
grpc_server, callback=lambda value: connection_counter.set(value)
)
start_http_server(prometheus_port)
# forwarder_port - port to listen connections from gRPC clients
# Note this call is blocking
forwarder.serve(host="0.0.0.0", port=forwarder_port)
# If you want to run the server in a separate thread, and want to stop it later, use the forwarder.stop() method
Configuring and running a gRPC client
Import the required modules:
import grpc
from grpc_load_balancer import EnvConfigLoader, MetricsBasedServerFinder
Initialize the configuration loader
# This is example how your environment variables can look like
# os.environ['LB_METRICS_NAME'] = 'connections_num'
# os.environ['LB_HOST1_HOST'] = 'localhost'
# os.environ['LB_HOST1_PORT'] = '20000'
# os.environ['LB_HOST1_METRICS_PORT'] = '20001'
# os.environ['LB_HOST2_HOST'] = 'localhost'
# os.environ['LB_HOST2_PORT'] = '30000'
# os.environ['LB_HOST2_METRICS_PORT'] = '30001'
env_config_loader = EnvConfigLoader(prefix="LB")
Initialize the server finder, find the best server and connect to it
metrics_based_server_finder = env_config_loader.init_metrics_based_server_finder()
best_host = metrics_based_server_finder.fetch_metrics()
channel = grpc.insecure_channel(f"{best_host[0]}:{best_host[1]}")
client = create_example_grpc_client(channel) # implement this function yourself
# Use the client to send requests to the server
Troubleshooting
If you have any problems with grpc_load_balancer, please open an issue on GitHub.
Also, you can check tests/test_grpc_load_balancer.py for more information.
Contributing
We welcome contributions to grpc_load_balancer. If you find a bug or want to propose a new feature, please open a GitHub issue or submit a pull request.
License
This project is licensed under the MIT License. See LICENSE for details.
Metadata
Release files for grpc-load-balancer 0.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| grpc_load_balancer-0.0.3.tar.gz | 7.3 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| grpc_load_balancer-0.0.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 14.8 kB
Release files / grpc_load_balancer-0.0.3.tar.gz
| Download URL | grpc_load_balancer-0.0.3.tar.gz |
|---|---|
| Size | 7.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9e29c29c17aaa08cba7cc297c18119525eaa1cfa802c1d03a1e2f31c2b3e90be
|
|
BLAKE2b-256 checksum How to use checksums |
c9404e7f5cfc8c5e03b63dd1e95cf42e95719ad0064a9b7578dce20886a8037f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.3
|
Release files / grpc_load_balancer-0.0.3-py3-none-any.whl
| Download URL | grpc_load_balancer-0.0.3-py3-none-any.whl |
|---|---|
| Size | 7.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f590c013cff44ddd2cd57d731f66706f7b4df27f82a6e0cdf2ccf3d0ab1d686e
|
|
BLAKE2b-256 checksum How to use checksums |
73ca3774e30dd6c932f52463bae648443315e13201a2462404e3d4d6ee17f903
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.3
|
Security for grpc-load-balancer 0.0.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.