Skip to main content

A client throttle tool based on redis.

Project description

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

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

client_throttler-2.0.0.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

client_throttler-2.0.0-py3-none-any.whl (15.5 kB view details)

Uploaded Python 3

File details

Details for the file client_throttler-2.0.0.tar.gz.

File metadata

  • Download URL: client_throttler-2.0.0.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.18

File hashes

Hashes for client_throttler-2.0.0.tar.gz
Algorithm Hash digest
SHA256 f4454fa72f789348b8d8eadc527a3bc5cb7800ca21a70349c713ff001847d0f1
MD5 ba567b525bba0f573a4ec6089e0db81d
BLAKE2b-256 841b659b2137534777f372ca466808e7422ab30d4b3282f3320faad5cc44890d

See more details on using hashes here.

File details

Details for the file client_throttler-2.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for client_throttler-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5dabbf27ad564881cb2ce03ab05e5b9e200bbaeaf80765a4ba28fccc5c2e4029
MD5 3bd0e388fb604b2d5bee0c95f5eaf3f3
BLAKE2b-256 9b586d5175b9febc92a9926188a855bf30b0fa1cfb758f4aaa05aeeb8f4fc332

See more details on using hashes here.

Supported by

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