sycommon-python-lib 0.1.42__py3-none-any.whl → 0.1.44__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -79,7 +79,7 @@ def setup_trace_id_handler(app):
79
79
 
80
80
  content_type = response.headers.get("Content-Type", "")
81
81
 
82
- # 处理 SSE 响应 - 关键修复点
82
+ # 处理 SSE 响应
83
83
  if "text/event-stream" in content_type:
84
84
  # 流式响应不能有Content-Length,移除它
85
85
  if "Content-Length" in response.headers:
@@ -85,6 +85,22 @@ class RabbitMQClient:
85
85
  # 线程安全锁
86
86
  self._consume_lock = asyncio.Lock()
87
87
  self._connect_lock = asyncio.Lock()
88
+ # 跟踪连接关闭回调(用于后续移除)
89
+ self._conn_close_callback: Optional[Callable] = None
90
+ # 控制重连频率的信号量(避免短时间内大量重连任务)
91
+ self._reconnect_semaphore = asyncio.Semaphore(1)
92
+ # 重连冷却时间(秒)
93
+ self._reconnect_cooldown = 3
94
+ # 固定重连间隔15秒(全局统一)
95
+ self._RECONNECT_INTERVAL = 15
96
+ # 重连任务锁(确保同一时间只有一个重连任务)
97
+ self._reconnect_task_lock = asyncio.Lock()
98
+ # 跟踪当前重连任务(避免重复创建)
99
+ self._current_reconnect_task: Optional[asyncio.Task] = None
100
+ # 连接失败计数器(用于告警)
101
+ self._reconnect_fail_count = 0
102
+ # 连接失败告警阈值
103
+ self._reconnect_alert_threshold = 5
88
104
 
89
105
  @property
90
106
  async def is_connected(self) -> bool:
@@ -104,14 +120,16 @@ class RabbitMQClient:
104
120
  return False
105
121
 
106
122
  async def connect(self) -> None:
107
- """建立连接并初始化交换机/队列(支持重连)"""
108
123
  if self._closed:
109
124
  raise RuntimeError("客户端已关闭,无法重新连接")
110
125
 
111
126
  async with self._connect_lock:
112
- # 释放旧的无效资源
127
+ # 释放旧资源(保留原有回调清理逻辑)
113
128
  if self._channel and self._channel_conn:
114
129
  try:
130
+ if self._conn_close_callback and self._channel_conn:
131
+ self._channel_conn.close_callbacks.discard(
132
+ self._conn_close_callback)
115
133
  await self.connection_pool.release_channel(self._channel, self._channel_conn)
116
134
  except Exception as e:
117
135
  SYLogger.warning(f"释放旧通道失败: {str(e)}")
@@ -119,21 +137,28 @@ class RabbitMQClient:
119
137
  self._channel_conn = None
120
138
  self._exchange = None
121
139
  self._queue = None
140
+ self._conn_close_callback = None
122
141
 
123
142
  try:
124
- # 1. 从连接池获取通道+连接
143
+ # 从连接池获取通道+连接(连接池已控制连接数)
125
144
  self._channel, self._channel_conn = await self.connection_pool.acquire_channel()
126
145
 
127
146
  def on_conn_closed(conn: AbstractRobustConnection, exc: Optional[BaseException]):
128
- """连接关闭时触发的回调"""
129
- SYLogger.error(
147
+ """连接关闭回调:触发固定间隔重连"""
148
+ SYLogger.warning(
130
149
  f"客户端连接关闭: {conn!r},原因: {exc}", exc_info=exc)
150
+ self._reconnect_fail_count += 1
151
+ # 超过阈值告警
152
+ if self._reconnect_fail_count >= self._reconnect_alert_threshold:
153
+ SYLogger.error(
154
+ f"连接失败次数已达阈值({self._reconnect_alert_threshold}),请检查MQ服务状态")
131
155
  if not self._closed:
132
- asyncio.create_task(self.connect())
156
+ asyncio.create_task(self._safe_reconnect())
133
157
 
134
- # 给连接添加关闭回调
158
+ self._conn_close_callback = on_conn_closed
135
159
  if self._channel_conn:
136
- self._channel_conn.close_callbacks.add(on_conn_closed)
160
+ self._channel_conn.close_callbacks.add(
161
+ self._conn_close_callback)
137
162
 
138
163
  # 2. 设置预取计数(限流)
139
164
  await self._channel.set_qos(prefetch_count=self.prefetch_count)
@@ -168,10 +193,15 @@ class RabbitMQClient:
168
193
  f"(绑定交换机: {self.exchange_name}, routing_key: {self.routing_key})"
169
194
  )
170
195
 
196
+ # 重连成功,重置失败计数器
197
+ self._reconnect_fail_count = 0
171
198
  SYLogger.info("客户端连接初始化完成")
172
199
  except Exception as e:
173
200
  SYLogger.error(f"客户端连接失败: {str(e)}", exc_info=True)
174
201
  # 清理异常状态
202
+ if self._conn_close_callback and self._channel_conn:
203
+ self._channel_conn.close_callbacks.discard(
204
+ self._conn_close_callback)
175
205
  if self._channel and self._channel_conn:
176
206
  try:
177
207
  await self.connection_pool.release_channel(self._channel, self._channel_conn)
@@ -179,8 +209,40 @@ class RabbitMQClient:
179
209
  pass
180
210
  self._channel = None
181
211
  self._channel_conn = None
212
+ # 触发重连(固定间隔)
213
+ if not self._closed:
214
+ asyncio.create_task(self._safe_reconnect())
182
215
  raise
183
216
 
217
+ async def _safe_reconnect(self):
218
+ """安全重连:固定15秒间隔,避免重复任务"""
219
+ # 检查是否已有重连任务在运行
220
+ if self._current_reconnect_task and not self._current_reconnect_task.done():
221
+ SYLogger.debug("已有重连任务在运行,跳过重复触发")
222
+ return
223
+
224
+ async with self._reconnect_task_lock:
225
+ if self._closed or await self.is_connected:
226
+ return
227
+
228
+ # 固定15秒重连间隔
229
+ SYLogger.info(f"将在15秒后尝试重连...")
230
+ await asyncio.sleep(self._RECONNECT_INTERVAL)
231
+
232
+ if self._closed or await self.is_connected:
233
+ return
234
+
235
+ try:
236
+ self._current_reconnect_task = asyncio.create_task(
237
+ self.connect())
238
+ await self._current_reconnect_task
239
+ except Exception as e:
240
+ SYLogger.warning(f"重连失败: {str(e)}")
241
+ # 重连失败后,继续触发下一次重连(仍保持15秒间隔)
242
+ asyncio.create_task(self._safe_reconnect())
243
+ finally:
244
+ self._current_reconnect_task = None
245
+
184
246
  async def set_message_handler(
185
247
  self,
186
248
  handler: Callable[[MQMsgModel, AbstractIncomingMessage], Coroutine[Any, Any, None]],
@@ -351,14 +413,18 @@ class RabbitMQClient:
351
413
  )
352
414
 
353
415
  async def close(self) -> None:
354
- """关闭客户端(释放资源)"""
355
- if self._closed:
356
- SYLogger.warning("客户端已关闭,无需重复操作")
357
- return
358
-
416
+ """关闭客户端(移除回调)"""
359
417
  self._closed = True
360
418
  SYLogger.info("开始关闭RabbitMQ客户端...")
361
419
 
420
+ # 停止重连任务
421
+ if self._current_reconnect_task and not self._current_reconnect_task.done():
422
+ self._current_reconnect_task.cancel()
423
+ try:
424
+ await self._current_reconnect_task
425
+ except asyncio.CancelledError:
426
+ SYLogger.debug("重连任务已取消")
427
+
362
428
  # 1. 停止消费
363
429
  await self.stop_consuming()
364
430
 
@@ -366,6 +432,10 @@ class RabbitMQClient:
366
432
  async with self._connect_lock:
367
433
  if self._channel and self._channel_conn:
368
434
  try:
435
+ # 移除连接关闭回调
436
+ if self._conn_close_callback:
437
+ self._channel_conn.close_callbacks.discard(
438
+ self._conn_close_callback)
369
439
  await self.connection_pool.release_channel(self._channel, self._channel_conn)
370
440
  SYLogger.info("通道释放成功")
371
441
  except Exception as e: