send_queue.py 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. """Per-chat FIFO send queues (OpenClaw chat-queue.ts) + token bucket for WeCom's 30 msgs/min/chat limit (846607).
  2. Two lanes per chat: normal, and a control lane (approval prompts, finalize frames) backed by a reserved token pool."""
  3. from __future__ import annotations
  4. import asyncio
  5. import logging
  6. import time
  7. from typing import Dict
  8. logger = logging.getLogger("plugins.platforms.wecom.adapter")
  9. class ChatSendQueueMixin:
  10. """Expects ``_chat_queues/_chat_workers/_control_queues/_control_workers/_chat_token_usage`` dicts."""
  11. # Token bucket: 30 tokens/min per chat, split between normal and reserved (control) quota.
  12. _BUCKET_MAX_TOKENS = 30
  13. _BUCKET_NORMAL_TOKENS = 24
  14. _BUCKET_RESERVED_TOKENS = 6
  15. def _get_token_usage(self, chat_id: str) -> dict[str, float]:
  16. return self._chat_token_usage.setdefault(str(chat_id or "").strip(), {"normal": 0.0, "reserved": 0.0, "last_reset": time.monotonic()})
  17. def _bucket_try_consume(self, chat_id: str, is_control: bool = False) -> float:
  18. """Consume one token: 0 if available, else seconds until the next minute window. Control messages use the normal quota first, then the reserved pool."""
  19. usage = self._get_token_usage(chat_id)
  20. now = time.monotonic()
  21. if now - usage["last_reset"] > 60.0: # reset counters every minute
  22. usage.update(normal=0.0, reserved=0.0, last_reset=now)
  23. for lane, cap, allowed in (("normal", self._BUCKET_NORMAL_TOKENS, True), ("reserved", self._BUCKET_RESERVED_TOKENS, is_control)):
  24. if allowed and usage[lane] < cap:
  25. usage[lane] += 1.0
  26. return 0.0
  27. return 60.0 - (now - usage["last_reset"])
  28. def _lane(self, is_control: bool):
  29. return (self._control_queues, self._control_workers) if is_control else (self._chat_queues, self._chat_workers)
  30. async def _enqueue_chat_send(self, chat_id: str, coro_factory, is_control: bool = False):
  31. """Enqueue a send for a chat and await its result (FIFO per chat); the control lane bypasses the normal queue so approval prompts are never blocked."""
  32. key = str(chat_id or "").strip()
  33. lane = "control" if is_control else "normal"
  34. queues, workers = self._lane(is_control)
  35. if key not in queues:
  36. logger.debug("[%s] Creating %s queue + worker for chat %s", self.name, lane, key)
  37. queues[key] = asyncio.Queue()
  38. workers[key] = asyncio.create_task(self._send_worker(key, is_control))
  39. queue = queues[key]
  40. logger.debug("[%s] Enqueuing send for chat %s (lane=%s, qsize=%d)", self.name, key, lane, queue.qsize())
  41. future = asyncio.get_running_loop().create_future()
  42. await queue.put((coro_factory, future))
  43. return await future
  44. async def _send_worker(self, chat_key: str, is_control: bool) -> None:
  45. """Per-chat worker: drain one lane's queue under the token bucket."""
  46. queue = self._lane(is_control)[0][chat_key]
  47. if not is_control:
  48. logger.debug("[%s] Normal send worker started for chat %s", self.name, chat_key)
  49. try:
  50. while True:
  51. coro_factory, future = await queue.get()
  52. try:
  53. wait = self._bucket_try_consume(chat_key, is_control)
  54. if wait > 0:
  55. if not is_control:
  56. logger.debug("[%s] Normal worker rate-limited for chat %s, waiting %.1fs", self.name, chat_key, wait)
  57. await asyncio.sleep(wait)
  58. self._bucket_try_consume(chat_key, is_control) # re-consume after wait
  59. result = await coro_factory()
  60. if not future.done():
  61. future.set_result(result)
  62. except Exception as exc:
  63. if not future.done():
  64. future.set_exception(exc)
  65. finally:
  66. queue.task_done()
  67. except asyncio.CancelledError:
  68. while not queue.empty():
  69. _, future = queue.get_nowait()
  70. if not future.done():
  71. future.set_exception(RuntimeError("WeCom adapter shutting down"))