Skip to main content

HAProxy-like L7 load balancer and reverse proxy in pure Python (async).

Project description

pyhaproxy

PyPI Python Versions License Tests

pyhaproxy is a minimal HAProxy-like reverse proxy and load balancer written in Python.

This is not a wrapper around the HAProxy binary. It is a pure Python async implementation focused on extensibility.

MVP Features

  • Frontends — bind to multiple host:port pairs, each with its own routing rules
  • ACL-like routing — route requests by host or path_prefix
  • Backend pools — groups of servers behind a single backend name
  • Load balancing — round-robin and least-connections
  • Health checks — concurrent async HTTP checks per backend
  • Prometheus metrics — request count and latency histograms
  • Connection pooling — shared httpx.AsyncClient with keep-alive
  • X-Forwarded- headers* — automatically added to proxied requests

Quickstart

pip install pyhaproxy

Run

pyhaproxy run -c examples/config.yml

Test

curl -H "Host: example.com" http://localhost:9000/
curl -H "Host: example.com" http://localhost:9000/api/users

Example Config

frontends:
  - name: "http_front"
    bind: "0.0.0.0:9000"
    default_backend: "app_backend"
    rules:
      - if: "host == example.com"
        backend: "app_backend"
      - if: "path_prefix == /api"
        backend: "api_backend"

backends:
  app_backend:
    balance: "roundrobin"
    servers:
      - url: "http://localhost:5000"
      - url: "http://localhost:5001"
  api_backend:
    balance: "leastconn"
    servers:
      - url: "http://localhost:6000"

healthcheck:
  enabled: true
  interval_seconds: 5
  timeout_seconds: 2
  path: "/health"

metrics:
  enabled: true
  path: "/metrics"

Architecture

Client ──► Uvicorn ──► Starlette ──► Frontend.resolve_backend()
                                         │
                                    ┌─────▼──────┐
                                    │  ACL rules  │
                                    │ host/path   │
                                    └─────┬──────┘
                                          │ backend name
                                          ▼
                                    BackendRegistry
                                          │
                                    ┌─────▼──────┐
                                    │  Balancer   │
                                    │ RR / Least  │
                                    └─────┬──────┘
                                          │ server URL
                                          ▼
                                   proxy.forward()
                                          │
                                    ┌─────▼──────┐
                                    │  Backend    │
                                    │  server     │
                                    └────────────┘

Changelog

0.1.1

  • Multi-frontend support: CLI now runs all configured frontends concurrently.
  • Connection pooling: Shared httpx.AsyncClient with keep-alive limits (was creating a client per request).
  • Concurrent health checks: Servers are checked in parallel via asyncio.gather.
  • X-Forwarded- headers*: Proxied requests now include x-forwarded-for, x-forwarded-proto, x-forwarded-host.
  • Better error messages: Balancer errors say "at least one server".
  • Improved type annotations: Full type hints on all public classes.
  • Comprehensive tests: Balancers, config, registry, frontend, ACL, proxy.

License

MIT

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

pyhaproxy_lb-0.1.1.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

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

pyhaproxy_lb-0.1.1-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file pyhaproxy_lb-0.1.1.tar.gz.

File metadata

  • Download URL: pyhaproxy_lb-0.1.1.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for pyhaproxy_lb-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d46a357f60abf7670e52b8dcca8fa880116d2018cef43a455657936ce310bc2e
MD5 36007c2251152fd26a3d5cf249db5289
BLAKE2b-256 046219beac7021eb1ee743bb7674dd50ad706bc16190c34cc660b914dfe848ab

See more details on using hashes here.

File details

Details for the file pyhaproxy_lb-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pyhaproxy_lb-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for pyhaproxy_lb-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 30549153be80c8cb38d3e51d192440dabeaae25ba8411f1e5c2d04904434a89f
MD5 2739cb4591889b77a4615fe9fa54d8ad
BLAKE2b-256 bfae216085e33053493878e5a102680c5d187fedb68b66a0f1927e2b1f3a0da2

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