Skip to main content

An active current limiting algorithm based on redis atomic implementation.

Project description

Client Throttler

license Release Version PRs Welcome

Overview

A distributed proactive rate limiting algorithm based on Redis.

Users can proactively limit the invocation frequency of methods or functions.

Calls that exceed the limit will not be discarded, but will instead be queued and retried in the next cycle based on the calculated delay time.

Getting started

Installation

$ pip install client_throttler

Usage

# -*- coding: utf-8 -*-

import asyncio

from redis.client import Redis

from client_throttler.throttler import throttler

redis_client = Redis(host="localhost", port=1234, db=1)


@throttler(rate="1/2s", redis_client=redis_client)
async def funcA(*args, **kwargs):
    return args, kwargs


class TestAPI:
    api = "test_api"

    @throttler(rate="1/2s", redis_client=redis_client)
    async def funcB(self):
        return self.api

    def funcD(self):
        return self.api


def funcC(*args, **kwargs):
    return args, kwargs


async def main():
    result_a = await funcA(1, 2, 3, name=1, age=2)
    print("A:", result_a)
    result_b = await TestAPI().funcB()
    print("B:", result_b)
    result_c = funcC(1, 2, 3, name=1, age=2)
    print("C:", result_c)
    result_d = TestAPI().funcD()
    print("D:", result_d)


if __name__ == "__main__":
    asyncio.run(main())
"""
A: ((1, 2, 3), {'name': 1, 'age': 2})
B: test_api
C: ((1, 2, 3), {'name': 1, 'age': 2})
D: test_api
"""

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-1.0.0.tar.gz (5.8 kB view hashes)

Uploaded Source

Built Distribution

client_throttler-1.0.0-py3-none-any.whl (9.0 kB view hashes)

Uploaded Python 3

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