Skip to main content

API Gateway: rate limiting (Redis/in-memory), reverse proxy, and circuit breaker

Project description

ulfblk-gateway

API Gateway con rate limiting (Redis/in-memory), reverse proxy via httpx, y circuit breaker para proteger upstreams.

Instalacion

uv add ulfblk-gateway

Rate Limiting

from ulfblk_gateway.rate_limiter import (
    InMemoryBackend,
    RateLimiterMiddleware,
    RateLimiterSettings,
)

settings = RateLimiterSettings(
    requests=100,          # max por ventana
    window_seconds=60,
    exclude_paths=["/health"],
)
backend = InMemoryBackend()

app.add_middleware(RateLimiterMiddleware, settings=settings, backend=backend)

Con Redis (soft dependency via ulfblk-redis):

from ulfblk_gateway.rate_limiter import RedisBackend, RateLimiterMiddleware, RateLimiterSettings
from ulfblk_redis import RedisManager

redis = RedisManager()
await redis.connect()

backend = RedisBackend(redis)
app.add_middleware(
    RateLimiterMiddleware,
    settings=RateLimiterSettings(requests=100, window_seconds=60),
    backend=backend,
)

Reverse Proxy

from ulfblk_gateway.proxy import ProxyHandler, ProxyMiddleware, ProxyRoute, ProxySettings

settings = ProxySettings(
    routes=[
        ProxyRoute(path_prefix="/api/users", upstream_url="http://user-service:8001"),
        ProxyRoute(path_prefix="/api/orders", upstream_url="http://order-service:8002"),
    ]
)

handler = ProxyHandler(settings)
await handler.start()

app.add_middleware(ProxyMiddleware, handler=handler)

Circuit Breaker

from ulfblk_gateway.circuit_breaker import CircuitBreaker, CircuitBreakerSettings

cb = CircuitBreaker("user-service", CircuitBreakerSettings(
    failure_threshold=5,
    recovery_timeout=30.0,
    success_threshold=2,
))

# Integrado con proxy middleware
app.add_middleware(
    ProxyMiddleware,
    handler=handler,
    circuit_breakers={"http://user-service:8001": cb},
)

Health Check

from ulfblk_gateway.health import gateway_health_check

status = await gateway_health_check(handler)
# {"gateway": True, "upstream:http://user-service:8001": True}

Dependencias

  • ulfblk-core (obligatorio)
  • httpx>=0.27 (obligatorio)
  • ulfblk-redis (opcional, para RedisBackend)

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

ulfblk_gateway-0.1.0.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.

ulfblk_gateway-0.1.0-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file ulfblk_gateway-0.1.0.tar.gz.

File metadata

  • Download URL: ulfblk_gateway-0.1.0.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.11 {"installer":{"name":"uv","version":"0.10.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for ulfblk_gateway-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a492c909d2548251cff743994ebd136f474a31f8b971606771248c5d7155ae7f
MD5 301e031d8882664aacfccdacf8bbd291
BLAKE2b-256 dc64a294a81bcf3231b28e51c9c149ce82921ad789711239f5f77f893399997c

See more details on using hashes here.

File details

Details for the file ulfblk_gateway-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ulfblk_gateway-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.11 {"installer":{"name":"uv","version":"0.10.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for ulfblk_gateway-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a29fd105a104641baa788147eadbd4f0d0400e17d8c3dfe3cc7bf704a059628c
MD5 cd9eeefb8fac4437f9413dc37821a13d
BLAKE2b-256 9b134c5396de78f70270e6714533a29917b64ef184705ba956b8a436b8cdcc94

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