From bf89cf2b4bf338adef31d21eb33b41b050794309 Mon Sep 17 00:00:00 2001 From: Mateusz Mandera Date: Mon, 30 Dec 2019 02:26:08 +0100 Subject: [PATCH] rate_limiter: Use ABC for defining the abstract class RateLimitedObject. --- zerver/lib/rate_limiter.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/zerver/lib/rate_limiter.py b/zerver/lib/rate_limiter.py index 52c64e0609..4d406ad922 100644 --- a/zerver/lib/rate_limiter.py +++ b/zerver/lib/rate_limiter.py @@ -1,5 +1,6 @@ import os +from abc import ABC, abstractmethod from typing import Dict, List, Optional, Tuple from django.conf import settings @@ -27,20 +28,23 @@ logger = logging.getLogger(__name__) class RateLimiterLockingException(Exception): pass -class RateLimitedObject: +class RateLimitedObject(ABC): def get_keys(self) -> List[str]: key_fragment = self.key_fragment() return ["{}ratelimit:{}:{}".format(KEY_PREFIX, key_fragment, keytype) for keytype in ['list', 'zset', 'block']] + @abstractmethod def key_fragment(self) -> str: - raise NotImplementedError() + pass + @abstractmethod def rules(self) -> List[Tuple[int, int]]: - raise NotImplementedError() + pass + @abstractmethod def __str__(self) -> str: - raise NotImplementedError() + pass class RateLimitedUser(RateLimitedObject): def __init__(self, user: UserProfile, domain: str='api_by_user') -> None: