|
| 1 | +import dataclasses |
| 2 | +from typing import Any, Dict |
| 3 | + |
| 4 | +from qstash.http import HttpClient |
| 5 | + |
| 6 | + |
| 7 | +@dataclasses.dataclass |
| 8 | +class FlowControlInfo: |
| 9 | + """Information about a flow control key.""" |
| 10 | + |
| 11 | + key: str |
| 12 | + """The flow control key.""" |
| 13 | + |
| 14 | + wait_list_size: int |
| 15 | + """The number of messages waiting in the wait list.""" |
| 16 | + |
| 17 | + parallelism_max: int |
| 18 | + """The maximum parallelism configured for this flow control key.""" |
| 19 | + |
| 20 | + parallelism_count: int |
| 21 | + """The current number of active requests for this flow control key.""" |
| 22 | + |
| 23 | + rate_max: int |
| 24 | + """The maximum rate configured for this flow control key.""" |
| 25 | + |
| 26 | + rate_count: int |
| 27 | + """The current number of requests consumed in the current period.""" |
| 28 | + |
| 29 | + rate_period: int |
| 30 | + """The rate period in seconds.""" |
| 31 | + |
| 32 | + rate_period_start: int |
| 33 | + """The start time of the current rate period as a unix timestamp.""" |
| 34 | + |
| 35 | + |
| 36 | +@dataclasses.dataclass |
| 37 | +class GlobalParallelismInfo: |
| 38 | + """Information about global parallelism.""" |
| 39 | + |
| 40 | + parallelism_max: int |
| 41 | + """The maximum global parallelism.""" |
| 42 | + |
| 43 | + parallelism_count: int |
| 44 | + """The current number of active requests globally.""" |
| 45 | + |
| 46 | + |
| 47 | +def parse_flow_control_info(response: Dict[str, Any]) -> FlowControlInfo: |
| 48 | + return FlowControlInfo( |
| 49 | + key=response["flowControlKey"], |
| 50 | + wait_list_size=response.get("waitListSize", 0), |
| 51 | + parallelism_max=response.get("parallelismMax", 0), |
| 52 | + parallelism_count=response.get("parallelismCount", 0), |
| 53 | + rate_max=response.get("rateMax", 0), |
| 54 | + rate_count=response.get("rateCount", 0), |
| 55 | + rate_period=response.get("ratePeriod", 0), |
| 56 | + rate_period_start=response.get("ratePeriodStart", 0), |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +class FlowControlApi: |
| 61 | + def __init__(self, http: HttpClient) -> None: |
| 62 | + self._http = http |
| 63 | + |
| 64 | + def get(self, flow_control_key: str) -> FlowControlInfo: |
| 65 | + """ |
| 66 | + Gets a single flow control by key. |
| 67 | +
|
| 68 | + :param flow_control_key: The flow control key to get. |
| 69 | + """ |
| 70 | + response = self._http.request( |
| 71 | + path=f"/v2/flowControl/{flow_control_key}", |
| 72 | + method="GET", |
| 73 | + ) |
| 74 | + |
| 75 | + return parse_flow_control_info(response) |
| 76 | + |
| 77 | + def get_global_parallelism(self) -> GlobalParallelismInfo: |
| 78 | + """ |
| 79 | + Gets the global parallelism info. |
| 80 | + """ |
| 81 | + response = self._http.request( |
| 82 | + path="/v2/globalParallelism", |
| 83 | + method="GET", |
| 84 | + ) |
| 85 | + |
| 86 | + return GlobalParallelismInfo( |
| 87 | + parallelism_max=response.get("parallelismMax", 0), |
| 88 | + parallelism_count=response.get("parallelismCount", 0), |
| 89 | + ) |
0 commit comments