新增小红书、XMPP、元宝、Zalo 四个渠道扩展。 小红书渠道扩展主要模块:config, gateway, webhook, outbound, streaming, pairing, security, dedupe, media, status, window XMPP 渠道扩展主要模块:plugin, config, gateway, outbound, streaming, pairing, security, dedupe, accounts, commands, muc, rate_limiter, stanza_utils, status, monitor 元宝渠道扩展主要模块:plugin, client, config_schema, gateway, outbound(chunk/queue/transport), inbound(dispatcher), streaming, pairing, security, accounts, actions, commands, codec(biz/conn), session, shared, utils Zalo 渠道扩展主要模块:api, config, gateway, webhook, outbound, pairing, security, session, polling, monitor, status
86 lines
2.3 KiB
Python
86 lines
2.3 KiB
Python
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import logging
|
|
from collections.abc import Callable
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class BlockStreamingCoalescer:
|
|
def __init__(
|
|
self,
|
|
on_flush: Callable[[str], None],
|
|
min_chars: int = 2800,
|
|
max_chars: int = 3000,
|
|
idle_ms: int = 1000,
|
|
):
|
|
self._on_flush = on_flush
|
|
self._min_chars = min_chars
|
|
self._max_chars = max_chars
|
|
self._idle_ms = idle_ms
|
|
self._buffer: str = ""
|
|
self._idle_task: asyncio.Task | None = None
|
|
self._closed = False
|
|
self._last_append: float = 0
|
|
|
|
def push(self, text: str) -> None:
|
|
if self._closed:
|
|
return
|
|
|
|
self._buffer += text
|
|
try:
|
|
self._last_append = asyncio.get_running_loop().time()
|
|
except RuntimeError:
|
|
self._last_append = 0
|
|
|
|
if len(self._buffer) >= self._min_chars:
|
|
self._flush_buffer()
|
|
return
|
|
|
|
if self._idle_task and not self._idle_task.done():
|
|
self._idle_task.cancel()
|
|
try:
|
|
loop = asyncio.get_running_loop()
|
|
except RuntimeError:
|
|
loop = None
|
|
if loop is not None:
|
|
self._idle_task = loop.create_task(self._idle_watch())
|
|
|
|
async def _idle_watch(self) -> None:
|
|
try:
|
|
await asyncio.sleep(self._idle_ms / 1000)
|
|
loop = asyncio.get_event_loop()
|
|
if loop.time() - self._last_append >= self._idle_ms / 1000 and not self._closed:
|
|
self._flush_buffer()
|
|
except asyncio.CancelledError:
|
|
pass
|
|
|
|
def _flush_buffer(self) -> None:
|
|
if self._closed or not self._buffer.strip():
|
|
return
|
|
|
|
content = self._buffer
|
|
self._buffer = ""
|
|
|
|
if self._idle_task and not self._idle_task.done():
|
|
self._idle_task.cancel()
|
|
self._idle_task = None
|
|
|
|
self._on_flush(content)
|
|
|
|
def drain_now(self) -> None:
|
|
self._flush_buffer()
|
|
|
|
def flush(self) -> None:
|
|
self._flush_buffer()
|
|
self._closed = True
|
|
self._buffer = ""
|
|
|
|
def abort(self) -> None:
|
|
self._closed = True
|
|
self._buffer = ""
|
|
if self._idle_task and not self._idle_task.done():
|
|
self._idle_task.cancel()
|
|
self._idle_task = None
|