alisaifee / limits

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

AttributeError in storage/redis_cluster.py due to undefined self.dependency #238

Closed YiJun10565 closed 1 day ago

YiJun10565 commented 1 month ago

I encountered an issue where the redis_cluster.py module doesn't define self.dependency. This leads to an AttributeError when the parent property self.base_exceptions attempts to call self.dependency.

Expected Behaviour

The code should execute without errors, assuming self.dependency is properly defined.

Current Behaviour

The following error occurs:

AttributeError: 'RedisClusterStorage' object has no attribute 'dependency'

Steps to Reproduce

Use the redis_cluster.py module with a Redis cluster.

Call functions that invoke the self.base_exceptions property, such as through the following chain of functions: The error will occur when the try block in base.py raises an exception.

# file: limits/redis.py
@property
    def base_exceptions(
        self,
    ) -> Union[Type[Exception], Tuple[Type[Exception], ...]]:  # pragma: no cover
        return self.dependency.RedisError  # type: ignore[no-any-return]

# file: limits/storage/base.py
def _wrap_errors(storage: Storage, fn: Callable[P, R]) -> Callable[P, R]:
    @functools.wraps(fn)
    def inner(*args: P.args, **kwargs: P.kwargs) -> R:
        try:
            return fn(*args, **kwargs)
        except storage.base_exceptions as exc:
            if storage.wrap_exceptions:
                raise errors.StorageError(exc) from exc
            raise
    return inner

# file: limits/strategies.py
def test(self, item: RateLimitItem, *identifiers: str, cost: int = 1) -> bool:

    return (
        cast(MovingWindowSupport, self.storage).get_moving_window(
            item.key_for(*identifiers),
            item.amount,
            item.get_expiry(),
        )[1]
        <= item.amount - cost
    )

Your Environment

alisaifee commented 3 days ago

Apologies for the delay in getting back on this. This will be addressed by 12ef45342cf48d83d3bc89b525a8965306fdfbb6 and available in the next release.