alisaifee / limits

Rate limiting using various strategies and storage backends such as redis & memcached
https://limits.readthedocs.org
MIT License
410 stars 57 forks source link

Memory storage assertion error for async MovingWindowRateLimiter #232

Closed mcantrell closed 2 days ago

mcantrell commented 2 days ago

I'm getting the following error when trying to use the memory storage with the async version of MovingWindowRateLimiter.

../.venv/lib/python3.11/site-packages/limits/aio/strategies.py:16: in __init__
    assert isinstance(storage, Storage)
E   AssertionError

I see another related issue that was resolved by fixing the import for the strategy:

https://github.com/alisaifee/limits/issues/171

I don't believe this is related. The test detailed below shows the proper import.

Steps to Reproduce

import pytest
from limits import parse
from limits.storage import storage_from_string
from limits.aio.strategies import MovingWindowRateLimiter

memory_store = storage_from_string("memory://")
moving_window = MovingWindowRateLimiter(memory_store)
one_per_minute = parse("1/minute")

@pytest.mark.asyncio
async def test_hit():
    result = await moving_window.hit(one_per_minute, "test_namespace", "foo")
    assert result is True

Your Environment

limits version: 3.13.0 os: MacOS Sonoma 14.5 python: 3.11.9

mcantrell commented 2 days ago

argh, my mistake. I missed the async part of the storage uri:

memory:// vs asyc+memory://

Closing this but maybe this might help someone else that is copy/paste editing too quickly 😂