|
| 1 | +"""Simple quota limiter where quota can be revoked.""" |
| 2 | + |
| 3 | +from datetime import datetime |
| 4 | + |
| 5 | +from models.config import QuotaHandlersConfiguration |
| 6 | +from log import get_logger |
| 7 | +from utils.connection_decorator import connection |
| 8 | +from quota.quota_exceed_error import QuotaExceedError |
| 9 | +from quota.quota_limiter import QuotaLimiter |
| 10 | +from quota.sql import ( |
| 11 | + CREATE_QUOTA_TABLE, |
| 12 | + UPDATE_AVAILABLE_QUOTA_PG, |
| 13 | + SELECT_QUOTA_PG, |
| 14 | + SET_AVAILABLE_QUOTA_PG, |
| 15 | + INIT_QUOTA_PG, |
| 16 | +) |
| 17 | + |
| 18 | +logger = get_logger(__name__) |
| 19 | + |
| 20 | + |
| 21 | +class RevokableQuotaLimiter(QuotaLimiter): |
| 22 | + """Simple quota limiter where quota can be revoked.""" |
| 23 | + |
| 24 | + def __init__( |
| 25 | + self, |
| 26 | + configuration: QuotaHandlersConfiguration, |
| 27 | + initial_quota: int, |
| 28 | + increase_by: int, |
| 29 | + subject_type: str, |
| 30 | + ) -> None: |
| 31 | + """Initialize quota limiter.""" |
| 32 | + self.subject_type = subject_type |
| 33 | + self.initial_quota = initial_quota |
| 34 | + self.increase_by = increase_by |
| 35 | + self.sqlite_connection_config = configuration.sqlite |
| 36 | + self.postgres_connection_config = configuration.postgres |
| 37 | + |
| 38 | + @connection |
| 39 | + def available_quota(self, subject_id: str = "") -> int: |
| 40 | + """Retrieve available quota for given subject.""" |
| 41 | + if self.subject_type == "c": |
| 42 | + subject_id = "" |
| 43 | + with self.connection.cursor() as cursor: |
| 44 | + cursor.execute( |
| 45 | + SELECT_QUOTA_PG, |
| 46 | + (subject_id, self.subject_type), |
| 47 | + ) |
| 48 | + value = cursor.fetchone() |
| 49 | + if value is None: |
| 50 | + self._init_quota(subject_id) |
| 51 | + return self.initial_quota |
| 52 | + return value[0] |
| 53 | + |
| 54 | + @connection |
| 55 | + def revoke_quota(self, subject_id: str = "") -> None: |
| 56 | + """Revoke quota for given subject.""" |
| 57 | + if self.subject_type == "c": |
| 58 | + subject_id = "" |
| 59 | + # timestamp to be used |
| 60 | + revoked_at = datetime.now() |
| 61 | + |
| 62 | + with self.connection.cursor() as cursor: |
| 63 | + cursor.execute( |
| 64 | + SET_AVAILABLE_QUOTA_PG, |
| 65 | + (self.initial_quota, revoked_at, subject_id, self.subject_type), |
| 66 | + ) |
| 67 | + self.connection.commit() |
| 68 | + |
| 69 | + @connection |
| 70 | + def increase_quota(self, subject_id: str = "") -> None: |
| 71 | + """Increase quota for given subject.""" |
| 72 | + if self.subject_type == "c": |
| 73 | + subject_id = "" |
| 74 | + # timestamp to be used |
| 75 | + updated_at = datetime.now() |
| 76 | + |
| 77 | + with self.connection.cursor() as cursor: |
| 78 | + cursor.execute( |
| 79 | + UPDATE_AVAILABLE_QUOTA_PG, |
| 80 | + (self.increase_by, updated_at, subject_id, self.subject_type), |
| 81 | + ) |
| 82 | + self.connection.commit() |
| 83 | + |
| 84 | + def ensure_available_quota(self, subject_id: str = "") -> None: |
| 85 | + """Ensure that there's avaiable quota left.""" |
| 86 | + if self.subject_type == "c": |
| 87 | + subject_id = "" |
| 88 | + available = self.available_quota(subject_id) |
| 89 | + logger.info("Available quota for subject %s is %d", subject_id, available) |
| 90 | + # check if ID still have available tokens to be consumed |
| 91 | + if available <= 0: |
| 92 | + e = QuotaExceedError(subject_id, self.subject_type, available) |
| 93 | + logger.exception("Quota exceed: %s", e) |
| 94 | + raise e |
| 95 | + |
| 96 | + @connection |
| 97 | + def consume_tokens( |
| 98 | + self, |
| 99 | + input_tokens: int = 0, |
| 100 | + output_tokens: int = 0, |
| 101 | + subject_id: str = "", |
| 102 | + ) -> None: |
| 103 | + """Consume tokens by given subject.""" |
| 104 | + if self.subject_type == "c": |
| 105 | + subject_id = "" |
| 106 | + logger.info( |
| 107 | + "Consuming %d input and %d output tokens for subject %s", |
| 108 | + input_tokens, |
| 109 | + output_tokens, |
| 110 | + subject_id, |
| 111 | + ) |
| 112 | + to_be_consumed = input_tokens + output_tokens |
| 113 | + |
| 114 | + with self.connection.cursor() as cursor: |
| 115 | + # timestamp to be used |
| 116 | + updated_at = datetime.now() |
| 117 | + |
| 118 | + cursor.execute( |
| 119 | + UPDATE_AVAILABLE_QUOTA_PG, |
| 120 | + (-to_be_consumed, updated_at, subject_id, self.subject_type), |
| 121 | + ) |
| 122 | + self.connection.commit() |
| 123 | + |
| 124 | + def _initialize_tables(self) -> None: |
| 125 | + """Initialize tables used by quota limiter.""" |
| 126 | + logger.info("Initializing tables for quota limiter") |
| 127 | + cursor = self.connection.cursor() |
| 128 | + cursor.execute(CREATE_QUOTA_TABLE) |
| 129 | + cursor.close() |
| 130 | + self.connection.commit() |
| 131 | + |
| 132 | + def _init_quota(self, subject_id: str = "") -> None: |
| 133 | + """Initialize quota for given ID.""" |
| 134 | + # timestamp to be used |
| 135 | + revoked_at = datetime.now() |
| 136 | + |
| 137 | + with self.connection.cursor() as cursor: |
| 138 | + cursor.execute( |
| 139 | + INIT_QUOTA_PG, |
| 140 | + ( |
| 141 | + subject_id, |
| 142 | + self.subject_type, |
| 143 | + self.initial_quota, |
| 144 | + self.initial_quota, |
| 145 | + revoked_at, |
| 146 | + ), |
| 147 | + ) |
| 148 | + self.connection.commit() |
0 commit comments