-
Notifications
You must be signed in to change notification settings - Fork 8
Dev/clientlist #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Dev/clientlist #45
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d32e241
feat: 配置client 池
164108e
feat: format code
f7ca375
feat: close #44
232cb2b
feat: update metric setting
6b12913
feat: update metric setting
64c59e2
feat: update metric setting
62f99d0
feat: add lock
d52bba7
feat: fix bugs
f152b59
feat: update round
441fbd5
feat: update round
698a246
feat: update round
8896845
feat: update round
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
102 changes: 102 additions & 0 deletions
102
packages/sunagent-ext/src/sunagent_ext/tweet/twitter_client_pool.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import asyncio | ||
| import logging | ||
| import time | ||
| from dataclasses import dataclass | ||
| from typing import Any, Coroutine, List, Optional | ||
|
|
||
| import tweepy | ||
| from tweepy import Client | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
| RETRY_AFTER_SEC = 15 * 60 # 15 分钟 | ||
|
|
||
|
|
||
| @dataclass | ||
| class _PoolItem: # type: ignore[no-any-unimported] | ||
| client: tweepy.Client # type: ignore[no-any-unimported] | ||
| client_key: str # 用 consumer_key 当唯一标识 | ||
| dead_at: Optional[float] = None # None 表示 alive | ||
|
|
||
|
|
||
| class TwitterClientPool: | ||
| """ | ||
| Twitter 客户端专用池:轮询获取、异常熔断、15 min 复活、支持永久摘除。 | ||
| 所有操作在锁内完成,保证并发安全。 | ||
| """ | ||
|
|
||
| def __init__(self, clients: list[tweepy.Client], retry_after: float = RETRY_AFTER_SEC) -> None: # type: ignore[no-any-unimported] | ||
| self._retry_after = retry_after | ||
| self._pool: list[_PoolItem] = [_PoolItem(c, c.consumer_key) for c in clients] | ||
| self._lock = asyncio.Lock() | ||
| self._not_empty = asyncio.Event() | ||
| # 轮询指针:指向下一次应该开始检查的索引 | ||
| self._rr_idx = 0 | ||
| if any(item.dead_at is None for item in self._pool): | ||
| self._not_empty.set() | ||
|
|
||
| async def acquire(self) -> tuple[Client, str]: # type: ignore[no-any-unimported] | ||
| while True: | ||
| async with self._lock: | ||
| # 0. 处理池子为空的边缘情况 | ||
| if not self._pool: | ||
| self._not_empty.clear() | ||
| # 在锁外等待,但先释放锁 | ||
| raise RuntimeError("TwitterClientPool: 所有客户端已被永久摘除,请重建池子") | ||
| now = time.time() | ||
| # 1. 复活 | ||
| revived = False | ||
| for it in self._pool: | ||
| if it.dead_at and now - it.dead_at >= self._retry_after: | ||
| it.dead_at = None | ||
| revived = True | ||
| logger.info("client %s revived", it.client_key) | ||
| if revived: | ||
| self._not_empty.set() | ||
| # 2. 简化的、正确的轮询 | ||
| # 从上一次的位置开始,最多检查 N 次 (N=池子大小) | ||
| for i in range(len(self._pool)): | ||
| # 使用 % 来确保索引总在有效范围内 | ||
| idx = (self._rr_idx + i) % len(self._pool) | ||
| chosen = self._pool[idx] | ||
| if chosen.dead_at is None: | ||
| # 找到了一个可用的,更新下一次开始搜索的指针 | ||
| self._rr_idx = idx + 1 | ||
|
Hades-Ye marked this conversation as resolved.
Outdated
|
||
| return chosen.client, chosen.client_key | ||
| # 3. 如果循环走完都没有找到 alive 的 | ||
| self._not_empty.clear() | ||
| # 4. 释放锁后等待,避免死锁和忙等 | ||
| await self._not_empty.wait() | ||
|
Hades-Ye marked this conversation as resolved.
Outdated
|
||
|
|
||
| # -------------------- 加锁摘除 -------------------- | ||
| async def remove(self, client: tweepy.Client) -> None: # type: ignore[no-any-unimported] | ||
| """永久摘除某个 client(不再放回池子)。""" | ||
| async with self._lock: | ||
| # 使用列表推导式过滤掉要移除的客户端,比 pop 更安全 | ||
| original_len = len(self._pool) | ||
| client_key_to_remove = client.consumer_key | ||
| self._pool = [it for it in self._pool if it.client is not client] | ||
| if len(self._pool) < original_len: | ||
| logger.info("client %s removed permanently", client_key_to_remove) | ||
| # 检查移除后是否还有存活的客户端 | ||
| if not any(item.dead_at is None for item in self._pool): | ||
| self._not_empty.clear() | ||
|
boboliu-1010 marked this conversation as resolved.
|
||
|
|
||
| # -------------------- 归还 -------------------- | ||
| async def report_failure(self, client: tweepy.Client) -> None: # type: ignore[no-any-unimported] | ||
| """ | ||
| 报告一个客户端操作失败,将其置于熔断状态。 | ||
| 这不会将客户端从池中移除,它将在指定时间后自动复活。 | ||
| """ | ||
| async with self._lock: | ||
| for it in self._pool: | ||
| if it.client is client: | ||
| # 只有当它还活着时才标记为死亡,避免重复记录 | ||
| if it.dead_at is None: | ||
| it.dead_at = time.time() | ||
| logger.warning( | ||
| "client %s dead, will retry after %s min", it.client_key, self._retry_after // 60 | ||
| ) | ||
| # 检查此操作是否导致所有客户端都死亡 | ||
| if not any(item.dead_at is None for item in self._pool): | ||
| self._not_empty.clear() | ||
| return # 找到后即可退出 | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.