Skip to main content

A Python library for load balancing HTTP requests across multiple hosts with health checking

Project description

requests-load-balancer

A Python library for load balancing HTTP requests across multiple hosts with automatic health checking and failover capabilities.

Features

  • Round-robin load balancing: Distributes requests evenly across multiple hosts
  • Automatic health checking: Monitors host health based on HTTP status codes
  • Automatic failover: Switches to healthy hosts when current host becomes unhealthy
  • Auto-recovery: Unhealthy hosts are automatically retried after a configurable timeout
  • Flexible error codes: Customize which status codes indicate an unhealthy host
  • Simple API: Drop-in replacement for common requests methods

Installation

pip install requests-load-balancer

Quick Start

from requests_load_balancer import LoadBalancer

# Create a load balancer with multiple hosts
lb = LoadBalancer([
    'http://host1.example.com',
    'http://host2.example.com',
    'http://host3.example.com'
])

# Make requests - they will be automatically balanced
response = lb.get('/api/users')
print(response.json())

# POST request
response = lb.post('/api/users', json={'name': 'John Doe'})

Usage

Basic Load Balancing

from requests_load_balancer import LoadBalancer

# Initialize with multiple hosts
lb = LoadBalancer([
    'http://api1.example.com',
    'http://api2.example.com',
])

# All standard HTTP methods are supported
response = lb.get('/endpoint')
response = lb.post('/endpoint', json={'key': 'value'})
response = lb.put('/endpoint', json={'key': 'value'})
response = lb.delete('/endpoint')
response = lb.patch('/endpoint', json={'key': 'value'})

Custom Error Codes

By default, the following status codes mark a host as unhealthy: 500, 502, 503, 504

You can customize this behavior:

from requests_load_balancer import LoadBalancer

# Consider 404 and 429 as unhealthy as well
lb = LoadBalancer(
    hosts=['http://host1.com', 'http://host2.com'],
    error_codes={404, 429, 500, 502, 503, 504}
)

Unhealthy Host Recovery

By default, unhealthy hosts are automatically retried after 60 seconds:

from requests_load_balancer import LoadBalancer

# Default: retry unhealthy hosts after 60 seconds
lb = LoadBalancer(['http://host1.com', 'http://host2.com'])

# Custom timeout: retry after 30 seconds
lb = LoadBalancer(
    hosts=['http://host1.com', 'http://host2.com'],
    unhealthy_timeout=30
)

# Disable auto-recovery: hosts stay unhealthy until manually reset
lb = LoadBalancer(
    hosts=['http://host1.com', 'http://host2.com'],
    unhealthy_timeout=None
)

Health Management

# Get lists of healthy and unhealthy hosts
healthy = lb.get_healthy_hosts()
unhealthy = lb.get_unhealthy_hosts()

print(f"Healthy hosts: {healthy}")
print(f"Unhealthy hosts: {unhealthy}")

# Reset all hosts to healthy
lb.reset_health()

Error Handling

When all hosts become unhealthy, a RuntimeError is raised:

from requests_load_balancer import LoadBalancer

lb = LoadBalancer(['http://host1.com', 'http://host2.com'])

try:
    response = lb.get('/endpoint')
except RuntimeError as e:
    print(f"All hosts are unhealthy: {e}")
    # Optionally reset health and retry
    lb.reset_health()

How It Works

  1. Round-Robin Distribution: The load balancer cycles through hosts in order, distributing requests evenly.

  2. Health Checking: When a request returns a status code in the error_codes set or raises a connection exception, that host is marked as unhealthy with a timestamp.

  3. Automatic Failover: Unhealthy hosts are automatically skipped. The load balancer will try the next healthy host.

  4. Auto-Recovery: After the configured unhealthy_timeout (default 60 seconds), unhealthy hosts are automatically retried. The timeout can be customized or disabled.

  5. Manual Recovery: Use reset_health() to immediately mark all hosts as healthy again.

Requirements

  • Python >= 3.8
  • requests >= 2.25.0

License

MIT License

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Repository

https://github.com/GoCredit/requests-load-balancer

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

requests_load_balancer-0.0.2.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

requests_load_balancer-0.0.2-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file requests_load_balancer-0.0.2.tar.gz.

File metadata

  • Download URL: requests_load_balancer-0.0.2.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for requests_load_balancer-0.0.2.tar.gz
Algorithm Hash digest
SHA256 1174512fdafcdae70958c0de372e5d2880827d5dab68a0d43c1c52d2233570b1
MD5 667e45d34d0a6d0f1f4a4ffba8531f0e
BLAKE2b-256 6a4bea8713a92f4ccb3ccc5bf547e9257e66f56ffd1ef184b318dd0920f98bde

See more details on using hashes here.

File details

Details for the file requests_load_balancer-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for requests_load_balancer-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6c0f74dbf4ce48785952bfdff90cbee4b08c5f4b2a64537629d1382e4a91f1c2
MD5 f219d9224cda43c9e169987f21752751
BLAKE2b-256 f71570bf0a3ae8019d34d8d62960fb4c4a05447149eb7dfceec287be43d4efae

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page