Spaces:
Sleeping
Sleeping
Download core/rate_gate.py from Qurrah/replyflow-rag: direct link, hf CLI and curl.
- Browser
- Download file 636 Bytes
-
https://huggingface.co/spaces/Qurrah/replyflow-rag/resolve/main/core/rate_gate.py
- Command line
-
hf download hf://spaces/Qurrah/replyflow-rag/core/rate_gate.py
-
curl -L -o rate_gate.py https://huggingface.co/spaces/Qurrah/replyflow-rag/resolve/main/core/rate_gate.py
636 Bytes
| import threading | |
| import time | |
| class RateGate: | |
| def __init__(self, max_concurrent: int, min_interval: float): | |
| self._sem = threading.Semaphore(max_concurrent) | |
| self._min_interval = min_interval | |
| self._lock = threading.Lock() | |
| self._last_call = 0.0 | |
| def __enter__(self): | |
| self._sem.acquire() | |
| with self._lock: | |
| wait = self._last_call + self._min_interval - time.monotonic() | |
| if wait > 0: | |
| time.sleep(wait) | |
| self._last_call = time.monotonic() | |
| return self | |
| def __exit__(self, *exc): | |
| self._sem.release() | |
| return False |