Skip to main content

Client Throttler

license Release Version PRs Welcome Unittest codecov

Overview

A proactive rate-limiting tool utilizing Redis.

Users can actively limit the call frequency of functions or methods, and calls that would cause exceeding the limit will be delayed until they can be executed.

Getting started

Installation

$ pip install client_throttler

Usage

  1. [Optional] Configure a default config for all Throttler rate limiters.

    from redis.client import Redis
    from client_throttler import setup, ThrottlerConfig
    
    setup(ThrottlerConfig(redis_client=Redis(host="localhost", port=1234, db=1), rate="100/s"))
    
  2. Simply add a decorator to the function or method that needs to have its calls limited.

    from redis.client import Redis
    from client_throttler import throttler, ThrottlerConfig, Throttler
    
    redis_client = Redis(host="localhost", port=1234, db=2)
    
    # use default config
    @throttler()
    def func_a(*args, **kwargs):
        return args, kwargs
    
    # use default config, and change custom config
    @throttler(ThrottlerConfig(rate="1/2s"))
    def func_b(*args, **kwargs):
        return args, kwargs
    
    # use default config, and change redis client and some custom config
    @throttler(ThrottlerConfig(rate="1/2s", redis_client=redis_client))
    def func_c(*args, **kwargs):
        return args, kwargs
    
    # change a callable into throttled callable
    def func_d(*args, **kwargs):
        return args, kwargs
    func = Throttler(ThrottlerConfig(func=func_d))
    func(*args, **kwargs)
    
  3. [Optinal] get metric data

    from redis.client import Redis
    from client_throttler import MetricManager, ThrottlerConfig, Throttler
    
    redis_client = Redis(host="localhost", port=1234, db=2)
    
    def func_a(*args, **kwargs):
        return args, kwargs
    
    # enable_metric_record=True should be set when collect metric
    config = ThrottlerConfig(func=func_a, redis_client=redis_client, rate="100/s", enable_metric_record=True)
    func = Throttler(config)
    
    """
    call your func
    """
    
    metrics = MetricManager(config).load_metrics()
    

License

Based on the MIT protocol. Please refer to LICENSE

Metadata

Release files for client-throttler 2.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for client-throttler 2.1.0
File Size Uploaded
client_throttler-2.1.0.tar.gz 12.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for client-throttler 2.1.0
File Interpreter ABI Platform
client_throttler-2.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 27.8 kB

Release files / client_throttler-2.1.0.tar.gz

Download URL client_throttler-2.1.0.tar.gz
Size 12.1 kB
Tags Source
SHA-256 checksum
How to use checksums
6506c0497d1745483949e7a73870151b814086e9399e8da10df3372ec71a8721
BLAKE2b-256 checksum
How to use checksums
c4340dd507bf531ebab66137bfe74a4625ffc12c3a0f7178deb1989ba2429b10
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.23

Release files / client_throttler-2.1.0-py3-none-any.whl

Download URL client_throttler-2.1.0-py3-none-any.whl
Size 15.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
643f5bfce4656fa4be63d804e5334eaff56bc1bbee519d8e5b97d212f81faa87
BLAKE2b-256 checksum
How to use checksums
33b6d0988476547a45ec25715087a612787fd93f65b08133958d6b2a6da7c46a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.23

Release history Release notifications | RSS feed

This release

2.1.0 This release

2 release files

2.0.0

2 release files

1.1.0

2 release files

1.0.2

2 release files

1.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page