| 162 | |
| 163 | |
| 164 | class Pool: |
| 165 | def __init__(self, client_args: ClientArgs) -> None: |
| 166 | assert "api_keys" in client_args or "api_key" in client_args |
| 167 | assert not ("api_keys" in client_args and "api_key" in client_args) |
| 168 | |
| 169 | if "api_key" in client_args: |
| 170 | self.queue: List[Resource] = [Resource(**client_args)] |
| 171 | elif "api_keys" in client_args: |
| 172 | self.queue: List[Resource] = [ |
| 173 | Resource( |
| 174 | api_key=api_key, |
| 175 | base_url=client_args["base_url"], |
| 176 | organization=client_args.get("organization", None), |
| 177 | project=client_args.get("project", None), |
| 178 | max_retries=client_args.get("max_retries", 2), |
| 179 | timeout=client_args.get("timeout", None), |
| 180 | ) |
| 181 | for api_key in client_args["api_keys"] |
| 182 | ] |
| 183 | |
| 184 | self._index: int = 0 |
| 185 | |
| 186 | def __len__(self) -> int: |
| 187 | return len(self.queue) |
| 188 | |
| 189 | @abstractmethod |
| 190 | def __enter__(self, **kwargs) -> ClientArgs: |
| 191 | """Get a client from the pool and return it as a context manager.""" |
| 192 | raise NotImplementedError |
| 193 | |
| 194 | @abstractmethod |
| 195 | def __exit__(self, exc_type, exc_value, exc_traceback) -> bool: |
| 196 | """Put the resource back in the pool.""" |
| 197 | raise NotImplementedError |
| 198 | |
| 199 | def _remove_cur_arg(self) -> None: |
| 200 | logger.info(f"removing api key {self.queue[self._index]}") |
| 201 | _ = self.client_args.pop(self._index) |
| 202 | if len(self) == 0: |
| 203 | raise APIKeyPoolEmptyError("api key pool is empty") |
| 204 | |
| 205 | |
| 206 | class PQPool(Pool): |
nothing calls this directly
no outgoing calls
no test coverage detected