File size: 13,908 Bytes
2415446
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
"""Runtime-owned queued delivery for one messaging platform."""

import asyncio
from collections import deque
from collections.abc import Awaitable, Callable
from typing import Any

from loguru import logger

from free_claude_code.core.rate_limit import (
    StrictSlidingWindowLimiter as SlidingWindowLimiter,
)

from .safe_diagnostics import format_exception_for_log


class MessagingRateLimiter:
    """
    Rate limiter and compacting work queue for one messaging runtime.

    Uses a custom queue with task compaction (deduplication) to ensure
    only the latest version of a message update is processed.
    """

    def __init__(
        self,
        *,
        rate_limit: int,
        rate_window: float,
        log_error_details: bool = False,
    ) -> None:
        self.limiter = SlidingWindowLimiter(rate_limit, rate_window)
        self._log_error_details = log_error_details
        # Custom queue state - using deque for O(1) popleft
        self._queue_list: deque[str] = deque()  # Deque of dedup_keys in order
        self._queue_map: dict[
            str, tuple[Callable[[], Awaitable[Any]], list[asyncio.Future]]
        ] = {}
        self._condition = asyncio.Condition()
        self._shutdown = asyncio.Event()
        self._worker_task: asyncio.Task[None] | None = None
        self._background_tasks: set[asyncio.Task[None]] = set()
        self._active_futures: list[asyncio.Future[Any]] = []
        self._closed = False
        self._paused_until = 0

        logger.info(
            f"MessagingRateLimiter initialized ({rate_limit} req / {rate_window}s with Task Compaction)"
        )

    def start(self) -> None:
        """Start the owned worker on the current event loop."""
        if self._closed:
            raise RuntimeError("Messaging rate limiter is closed.")
        if self._worker_task and not self._worker_task.done():
            return
        self._worker_task = asyncio.create_task(
            self._worker(), name="msg-limiter-worker"
        )

    async def _worker(self) -> None:
        """Background worker that processes queued messaging tasks."""
        logger.info("MessagingRateLimiter worker started")
        while not self._shutdown.is_set():
            try:
                # Get a task from the queue
                async with self._condition:
                    while not self._queue_list and not self._shutdown.is_set():
                        await self._condition.wait()

                    if self._shutdown.is_set():
                        break

                    dedup_key = self._queue_list.popleft()
                    func, futures = self._queue_map.pop(dedup_key)
                    self._active_futures = futures

                # Check for manual pause (FloodWait)
                now = asyncio.get_event_loop().time()
                if self._paused_until > now:
                    wait_time = self._paused_until - now
                    logger.warning(
                        f"Limiter worker paused, waiting {wait_time:.1f}s more..."
                    )
                    await asyncio.sleep(wait_time)

                # Wait for rate limit capacity
                async with self.limiter:
                    try:
                        result = await func()
                        for f in futures:
                            if not f.done():
                                f.set_result(result)
                    except asyncio.CancelledError:
                        for f in futures:
                            if not f.done():
                                f.cancel()
                        worker = asyncio.current_task()
                        if self._shutdown.is_set() or (
                            worker is not None and worker.cancelling()
                        ):
                            raise
                        logger.debug(
                            "Messaging operation cancelled for key {}; worker remains active",
                            dedup_key,
                        )
                    except Exception as e:
                        # Report error to all futures and log it
                        for f in futures:
                            if not f.done():
                                f.set_exception(e)

                        error_msg = str(e).lower()
                        if "flood" in error_msg or "wait" in error_msg:
                            seconds = 30
                            try:
                                if hasattr(e, "seconds"):
                                    seconds = e.seconds
                                elif "after " in error_msg:
                                    # Try to parse "retry after X"
                                    parts = error_msg.split("after ")
                                    if len(parts) > 1:
                                        seconds = int(parts[1].split()[0])
                            except Exception:
                                pass

                            logger.error(
                                f"FloodWait detected! Pausing worker for {seconds}s"
                            )
                            wait_secs = (
                                float(seconds)
                                if isinstance(seconds, (int, float, str))
                                else 30.0
                            )
                            self._paused_until = (
                                asyncio.get_event_loop().time() + wait_secs
                            )
                        else:
                            logger.error(
                                "Error in limiter worker for key {}: {}",
                                dedup_key,
                                format_exception_for_log(
                                    e,
                                    log_full_message=self._log_error_details,
                                ),
                            )
                    finally:
                        self._active_futures = []
            except asyncio.CancelledError:
                for future in self._active_futures:
                    if not future.done():
                        future.cancel()
                self._active_futures = []
                if self._shutdown.is_set():
                    break
                raise
            except Exception as e:
                if self._log_error_details:
                    logger.error(
                        "MessagingRateLimiter worker critical error: {}",
                        e,
                        exc_info=True,
                    )
                else:
                    logger.error(
                        "MessagingRateLimiter worker critical error: exc_type={}",
                        type(e).__name__,
                    )
                await asyncio.sleep(1)

    async def shutdown(self, timeout: float | None = None) -> None:
        """Cancel queued work and stop every task owned by this limiter."""
        self._closed = True
        self._shutdown.set()
        async with self._condition:
            queued_futures = [
                future
                for _func, futures in self._queue_map.values()
                for future in futures
            ]
            self._queue_list.clear()
            self._queue_map.clear()
            for future in queued_futures:
                if not future.done():
                    future.cancel()
            for future in self._active_futures:
                if not future.done():
                    future.cancel()
            self._condition.notify_all()

        cancellation: asyncio.CancelledError | None = None
        timeout_error: TimeoutError | None = None
        task = self._worker_task
        if task and not task.done():
            task.cancel()
            try:
                drain = asyncio.gather(task, return_exceptions=True)
                if timeout is None:
                    await drain
                else:
                    await asyncio.wait_for(drain, timeout=timeout)
            except TimeoutError as exc:
                timeout_error = exc
            except asyncio.CancelledError as exc:
                cancellation = exc
        if task is None or task.done():
            self._worker_task = None

        background_tasks = tuple(self._background_tasks)
        for background_task in background_tasks:
            background_task.cancel()
        if background_tasks:
            try:
                await asyncio.gather(*background_tasks, return_exceptions=True)
            except asyncio.CancelledError as exc:
                cancellation = exc
        self._background_tasks.difference_update(
            task for task in background_tasks if task.done()
        )

        if cancellation is not None:
            raise cancellation
        if timeout_error is not None:
            raise TimeoutError(
                "MessagingRateLimiter worker did not stop before timeout"
            ) from timeout_error

    async def _enqueue_internal(
        self,
        func: Callable[[], Awaitable[Any]],
        future: asyncio.Future[Any],
        dedup_key: str,
        front: bool = False,
    ) -> None:
        await self._enqueue_internal_multi(func, [future], dedup_key, front)

    async def _enqueue_internal_multi(
        self,
        func: Callable[[], Awaitable[Any]],
        futures: list[asyncio.Future[Any]],
        dedup_key: str,
        front: bool = False,
    ) -> None:
        async with self._condition:
            if self._closed:
                raise RuntimeError("Messaging rate limiter is closed.")
            if self._worker_task is None or self._worker_task.done():
                raise RuntimeError("Messaging rate limiter has not been started.")
            if dedup_key in self._queue_map:
                # Compaction: Update existing task with new func, append new futures
                _old_func, old_futures = self._queue_map[dedup_key]
                old_futures.extend(futures)
                self._queue_map[dedup_key] = (func, old_futures)
                logger.debug(
                    f"Compacted task for key: {dedup_key} (now {len(old_futures)} futures)"
                )
            else:
                self._queue_map[dedup_key] = (func, futures)
                if front:
                    self._queue_list.appendleft(dedup_key)
                else:
                    self._queue_list.append(dedup_key)
                self._condition.notify_all()

    async def enqueue(
        self, func: Callable[[], Awaitable[Any]], dedup_key: str | None = None
    ) -> Any:
        """
        Enqueue a messaging task and return its future result.
        If dedup_key is provided, subsequent tasks with the same key will replace this one.
        """
        self._require_running()
        if dedup_key is None:
            # Unique key to avoid deduplication
            dedup_key = f"task_{id(func)}_{asyncio.get_running_loop().time()}"

        future = asyncio.get_running_loop().create_future()
        try:
            await self._enqueue_internal(func, future, dedup_key)
        except BaseException:
            future.cancel()
            raise
        return await future

    def fire_and_forget(
        self, func: Callable[[], Awaitable[Any]], dedup_key: str | None = None
    ) -> None:
        """Enqueue a task without waiting for the result."""
        self._require_running()
        if dedup_key is None:
            dedup_key = f"task_{id(func)}_{asyncio.get_running_loop().time()}"

        async def _wrapped() -> None:
            max_retries = 2
            for attempt in range(max_retries + 1):
                try:
                    await self.enqueue(func, dedup_key)
                    return
                except Exception as e:
                    error_msg = str(e).lower()
                    # Only retry transient connectivity issues that might have slipped through
                    # or occurred between platform checks.
                    if attempt < max_retries and any(
                        x in error_msg for x in ["connect", "timeout", "broken"]
                    ):
                        wait = 2**attempt
                        if self._log_error_details:
                            logger.warning(
                                "Limiter fire_and_forget transient error (attempt {}): {}. Retrying in {}s...",
                                attempt + 1,
                                e,
                                wait,
                            )
                        else:
                            logger.warning(
                                "Limiter fire_and_forget transient error (attempt {}): exc_type={}. Retrying in {}s...",
                                attempt + 1,
                                type(e).__name__,
                                wait,
                            )
                        await asyncio.sleep(wait)
                        continue

                    logger.error(
                        "Final error in fire_and_forget for key {}: {}",
                        dedup_key,
                        format_exception_for_log(
                            e,
                            log_full_message=self._log_error_details,
                        ),
                    )
                    break

        task = asyncio.create_task(_wrapped(), name=f"msg-limiter:{dedup_key}")
        self._background_tasks.add(task)
        task.add_done_callback(self._background_tasks.discard)

    def _require_running(self) -> None:
        if self._closed:
            raise RuntimeError("Messaging rate limiter is closed.")
        if self._worker_task is None or self._worker_task.done():
            raise RuntimeError("Messaging rate limiter has not been started.")