Skip to content

v4 Custom clock #261

@launay12u

Description

@launay12u

Hello,
I currently migrate my imlpementation of pyrateLimiter from v3 to v4. I use pyrate limiter on celery worker that are distributed amongst various hosts and this make time.monotonic() (the default) unfit. In the v3 I was using a custom Redis clock (the redis DB is not distributed) :

class RedisClock(AbstractClock):
    def now(self) -> int:
        unix_time, microseconds = injector.get(redis.Redis).time()  # injector.get(redis.Redis) return a redis client
        milliseconds: int = (unix_time * 1000) + (microseconds // 1000)
        return milliseconds

And then to create the Limiter :

    bucket = RedisBucket.init(
        rates=[...],
        redis=injector.get(redis.Redis),
        bucket_key=bucket_name,
    )
    return Limiter(bucket, clock=RedisClock())

How can I do in the v4 to define my clock ? Should I create a new implementation of the redis bucket ? Is it possible to get an exemple ? Also I saw in the code for the nox function of the redis bucket a TODO to use a Redis time source via a Lua script. Do you know when this will be available ? I think this will fix my issue.

Thanks for your reply

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions