This commit is contained in:
Your Name
2026-07-30 10:06:53 +08:00
parent 8f68af1c2c
commit 3fc94c4a89
9 changed files with 275 additions and 10 deletions
+31 -6
View File
@@ -22,11 +22,30 @@ class _StartPreparationTimeout(Exception):
"""Internal marker for the queue's own per-account deadline."""
DEFAULT_CONCURRENCY = 6
MAX_CONCURRENCY = 32
def _configured_concurrency() -> int:
"""Admission width for account preparation.
Every account spends most of its startup waiting: for the shared network
lane, for a WebSocket handshake, for signing work in a thread. Admitting
only two at a time therefore left the network lane idle and made a fleet of
several hundred accounts take tens of minutes. Actual outbound traffic is
still capped by the traffic controller, so a wider admission window fills
the existing lane instead of adding load.
"""
try:
return max(1, min(8, int(os.getenv("KEFU_BATCH_START_CONCURRENCY", "2"))))
return max(
1,
min(
MAX_CONCURRENCY,
int(os.getenv("KEFU_BATCH_START_CONCURRENCY", str(DEFAULT_CONCURRENCY))),
),
)
except (TypeError, ValueError):
return 2
return DEFAULT_CONCURRENCY
def _configured_timeout_seconds() -> float:
@@ -84,19 +103,25 @@ class BatchStartQueue:
self._active_tasks: dict[int, tuple[JobToken, asyncio.Task]] = {}
self._batches: dict[str, _BatchRecord] = {}
self._workers: list[asyncio.Task] = []
self._worker_sequence = 0
self._lock = asyncio.Lock()
self._stopping = False
async def _ensure_workers(self) -> None:
async with self._lock:
self._workers = [task for task in self._workers if not task.done()]
if self._workers or self._stopping:
if self._stopping:
return
for index in range(self.concurrency):
# Top up to the configured width instead of only starting from
# zero. A worker that died on an unexpected error used to shrink
# the queue permanently, so later batches crawled through a single
# remaining worker with no way to recover short of a restart.
while len(self._workers) < self.concurrency:
self._worker_sequence += 1
self._workers.append(
asyncio.create_task(
self._worker(index + 1),
name=f"account-batch-start-{index + 1}",
self._worker(self._worker_sequence),
name=f"account-batch-start-{self._worker_sequence}",
)
)