sycommon-python-lib 0.1.22__py3-none-any.whl → 0.1.24__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.
Potentially problematic release.
This version of sycommon-python-lib might be problematic. Click here for more details.
- sycommon/logging/kafka_log.py +1 -1
- sycommon/synacos/nacos_service.py +63 -48
- {sycommon_python_lib-0.1.22.dist-info → sycommon_python_lib-0.1.24.dist-info}/METADATA +1 -1
- {sycommon_python_lib-0.1.22.dist-info → sycommon_python_lib-0.1.24.dist-info}/RECORD +7 -7
- {sycommon_python_lib-0.1.22.dist-info → sycommon_python_lib-0.1.24.dist-info}/WHEEL +0 -0
- {sycommon_python_lib-0.1.22.dist-info → sycommon_python_lib-0.1.24.dist-info}/entry_points.txt +0 -0
- {sycommon_python_lib-0.1.22.dist-info → sycommon_python_lib-0.1.24.dist-info}/top_level.txt +0 -0
sycommon/logging/kafka_log.py
CHANGED
|
@@ -47,7 +47,7 @@ class NacosService(metaclass=SingletonMeta):
|
|
|
47
47
|
self.retry_backoff = self.nacos_config.get('retryBackoff', 1.5)
|
|
48
48
|
self.max_retry_delay = self.nacos_config.get('maxRetryDelay', 30)
|
|
49
49
|
self.heartbeat_interval = self.nacos_config.get(
|
|
50
|
-
'heartbeatInterval',
|
|
50
|
+
'heartbeatInterval', 10)
|
|
51
51
|
self.register_retry_interval = self.nacos_config.get(
|
|
52
52
|
'registerRetryInterval', 5) # 注册重试间隔
|
|
53
53
|
|
|
@@ -87,7 +87,9 @@ class NacosService(metaclass=SingletonMeta):
|
|
|
87
87
|
# 心跳相关
|
|
88
88
|
self._last_heartbeat_time = 0
|
|
89
89
|
self._heartbeat_fail_count = 0
|
|
90
|
+
self._heartbeat_lock = threading.Lock() # 控制心跳线程创建的锁
|
|
90
91
|
self._heartbeat_thread = None
|
|
92
|
+
|
|
91
93
|
self.max_heartbeat_timeout = self.nacos_config.get(
|
|
92
94
|
'maxHeartbeatTimeout', 30) # 最大无心跳时间(秒)
|
|
93
95
|
self._last_successful_heartbeat = time.time() # 上次成功心跳时间戳
|
|
@@ -367,9 +369,9 @@ class NacosService(metaclass=SingletonMeta):
|
|
|
367
369
|
|
|
368
370
|
metadata = {
|
|
369
371
|
"ignore-metrics": "true",
|
|
370
|
-
"preserved.heart.beat.interval": "3000", # 心跳间隔 3 秒
|
|
371
|
-
"preserved.heart.beat.timeout": "15000", # 心跳超时 15 秒
|
|
372
|
-
"preserved.ip.delete.timeout": "30000" # 实例删除超时 30 秒
|
|
372
|
+
# "preserved.heart.beat.interval": "3000", # 心跳间隔 3 秒
|
|
373
|
+
# "preserved.heart.beat.timeout": "15000", # 心跳超时 15 秒
|
|
374
|
+
# "preserved.ip.delete.timeout": "30000" # 实例删除超时 30 秒
|
|
373
375
|
}
|
|
374
376
|
if self.version:
|
|
375
377
|
metadata["version"] = self.version
|
|
@@ -460,68 +462,81 @@ class NacosService(metaclass=SingletonMeta):
|
|
|
460
462
|
return instance
|
|
461
463
|
|
|
462
464
|
def start_heartbeat(self):
|
|
463
|
-
"""
|
|
464
|
-
|
|
465
|
-
|
|
465
|
+
"""启动心跳线程(确保单例)"""
|
|
466
|
+
with self._heartbeat_lock: # 加锁确保线程安全
|
|
467
|
+
# 双重检查:先判断线程是否已存在且存活
|
|
468
|
+
if self._heartbeat_thread is not None and self._heartbeat_thread.is_alive():
|
|
469
|
+
return
|
|
466
470
|
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
name="NacosHeartbeatThread",
|
|
470
|
-
daemon=True
|
|
471
|
-
)
|
|
472
|
-
# 设置线程为守护线程并尝试提高优先级
|
|
473
|
-
self._heartbeat_thread.daemon = True
|
|
474
|
-
self._heartbeat_thread.start()
|
|
471
|
+
# 彻底清理可能的残留线程引用
|
|
472
|
+
self._heartbeat_thread = None
|
|
475
473
|
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
SYLogger.warning(f"nacos:无法提高心跳线程优先级: {e}")
|
|
474
|
+
# 创建新的心跳线程
|
|
475
|
+
self._heartbeat_thread = threading.Thread(
|
|
476
|
+
target=self._send_heartbeat_loop,
|
|
477
|
+
name="NacosHeartbeatThread",
|
|
478
|
+
daemon=True
|
|
479
|
+
)
|
|
480
|
+
self._heartbeat_thread.daemon = True
|
|
481
|
+
self._heartbeat_thread.start()
|
|
482
|
+
SYLogger.info(f"nacos:心跳线程启动,线程ID: {self._heartbeat_thread.ident}")
|
|
486
483
|
|
|
487
484
|
def _send_heartbeat_loop(self):
|
|
488
485
|
"""优化后的心跳发送循环,确保严格按间隔执行"""
|
|
489
|
-
|
|
490
|
-
|
|
486
|
+
current_thread = threading.current_thread()
|
|
487
|
+
thread_ident = current_thread.ident
|
|
488
|
+
SYLogger.info(
|
|
489
|
+
f"nacos:心跳循环启动 - 线程ID: {thread_ident}, "
|
|
490
|
+
f"配置间隔: {self.heartbeat_interval}秒"
|
|
491
|
+
)
|
|
492
|
+
|
|
493
|
+
consecutive_fail = 0 # 连续失败计数器
|
|
491
494
|
|
|
492
495
|
while not self._shutdown_event.is_set():
|
|
496
|
+
# 记录当前时间,作为本次心跳的基准
|
|
493
497
|
current_time = time.time()
|
|
494
|
-
# 计算距离下次心跳的时间(确保严格按间隔执行)
|
|
495
|
-
sleep_time = max(0, self.heartbeat_interval -
|
|
496
|
-
(current_time - last_heartbeat_time))
|
|
497
|
-
|
|
498
|
-
if sleep_time > 0:
|
|
499
|
-
self._shutdown_event.wait(sleep_time) # 精准等待
|
|
500
498
|
|
|
501
|
-
# 执行心跳逻辑
|
|
502
499
|
try:
|
|
503
|
-
#
|
|
500
|
+
# 检查注册状态(带锁读取)
|
|
504
501
|
with self._state_lock:
|
|
505
502
|
registered_status = self.registered
|
|
506
503
|
|
|
507
|
-
if registered_status
|
|
504
|
+
if not registered_status:
|
|
505
|
+
SYLogger.warning(
|
|
506
|
+
f"nacos:服务未注册,跳过心跳 - 线程ID: {thread_ident}")
|
|
507
|
+
consecutive_fail = 0
|
|
508
|
+
else:
|
|
509
|
+
# 发送心跳
|
|
508
510
|
success = self.send_heartbeat()
|
|
509
511
|
if success:
|
|
510
|
-
|
|
512
|
+
consecutive_fail = 0
|
|
513
|
+
self._last_successful_heartbeat = current_time
|
|
511
514
|
SYLogger.info(
|
|
512
|
-
f"nacos
|
|
515
|
+
f"nacos:心跳发送成功 - 时间: {current_time:.3f}, "
|
|
516
|
+
f"间隔: {self.heartbeat_interval}秒"
|
|
517
|
+
)
|
|
513
518
|
else:
|
|
519
|
+
consecutive_fail += 1
|
|
514
520
|
SYLogger.warning(
|
|
515
|
-
f"nacos
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
521
|
+
f"nacos:心跳发送失败 - 连续失败: {consecutive_fail}次"
|
|
522
|
+
)
|
|
523
|
+
if consecutive_fail >= 5:
|
|
524
|
+
SYLogger.error("nacos:心跳连续失败5次,尝试重连")
|
|
525
|
+
self.reconnect_nacos_client()
|
|
526
|
+
consecutive_fail = 0
|
|
527
|
+
|
|
528
|
+
except Exception as e:
|
|
529
|
+
consecutive_fail += 1
|
|
530
|
+
SYLogger.error(
|
|
531
|
+
f"nacos:心跳异常: {str(e)}, 连续失败: {consecutive_fail}次")
|
|
532
|
+
|
|
533
|
+
# 计算下次执行时间(当前时间 + 配置间隔),确保间隔稳定
|
|
534
|
+
next_run_time = current_time + self.heartbeat_interval
|
|
535
|
+
sleep_time = max(0, next_run_time - time.time()
|
|
536
|
+
) # 避免负数(处理耗时超过间隔的情况)
|
|
537
|
+
self._shutdown_event.wait(sleep_time) # 精准休眠至下次执行时间
|
|
538
|
+
|
|
539
|
+
SYLogger.info(f"nacos:心跳循环已停止 - 线程ID: {thread_ident}")
|
|
525
540
|
|
|
526
541
|
def send_heartbeat(self):
|
|
527
542
|
"""发送心跳并添加超时控制"""
|
|
@@ -15,7 +15,7 @@ sycommon/health/health_check.py,sha256=EhfbhspRpQiKJaxdtE-PzpKQO_ucaFKtQxIm16F5M
|
|
|
15
15
|
sycommon/health/metrics.py,sha256=fHqO73JuhoZkNPR-xIlxieXiTCvttq-kG-tvxag1s1s,268
|
|
16
16
|
sycommon/health/ping.py,sha256=FTlnIKk5y1mPfS1ZGOeT5IM_2udF5aqVLubEtuBp18M,250
|
|
17
17
|
sycommon/logging/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
18
|
-
sycommon/logging/kafka_log.py,sha256=
|
|
18
|
+
sycommon/logging/kafka_log.py,sha256=x1ZUCpSwSONugzOkSppZ6hOPFqekmjPDHdlJ8v75D-s,20764
|
|
19
19
|
sycommon/logging/logger_wrapper.py,sha256=TiHsrIIHiQMzXgXK12-0KIpU9GhwQJOoHslakzmq2zc,357
|
|
20
20
|
sycommon/middleware/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
21
21
|
sycommon/middleware/context.py,sha256=_5ghpv4u_yAvjkgM-XDx8OnO-YI1XtntHrXsHJHZkwo,88
|
|
@@ -42,13 +42,13 @@ sycommon/sse/event.py,sha256=k_rBJy23R7crtzQeetT0Q73D8o5-5p-eESGSs_BPOj0,2797
|
|
|
42
42
|
sycommon/sse/sse.py,sha256=__CfWEcYxOxQ-HpLor4LTZ5hLWqw9-2X7CngqbVHsfw,10128
|
|
43
43
|
sycommon/synacos/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
44
44
|
sycommon/synacos/feign.py,sha256=qALBl3YwVGvAzgx6tvwW84GptfS1u8WpapTRTygZROM,21282
|
|
45
|
-
sycommon/synacos/nacos_service.py,sha256=
|
|
45
|
+
sycommon/synacos/nacos_service.py,sha256=kFGtOTKL6ZuFLebDK6gK6YNqdthhFW0aaXfG2eWzrzY,34493
|
|
46
46
|
sycommon/tools/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
47
47
|
sycommon/tools/docs.py,sha256=OPj2ETheuWjXLyaXtaZPbwmJKfJaYXV5s4XMVAUNrms,1607
|
|
48
48
|
sycommon/tools/snowflake.py,sha256=DdEj3T5r5OEvikp3puxqmmmz6BrggxomoSlnsRFb5dM,1174
|
|
49
49
|
sycommon/tools/timing.py,sha256=OiiE7P07lRoMzX9kzb8sZU9cDb0zNnqIlY5pWqHcnkY,2064
|
|
50
|
-
sycommon_python_lib-0.1.
|
|
51
|
-
sycommon_python_lib-0.1.
|
|
52
|
-
sycommon_python_lib-0.1.
|
|
53
|
-
sycommon_python_lib-0.1.
|
|
54
|
-
sycommon_python_lib-0.1.
|
|
50
|
+
sycommon_python_lib-0.1.24.dist-info/METADATA,sha256=lWldyI4VazRFZVRGcN7u1otEYwuWHCA0FiUxgSp2YPY,7038
|
|
51
|
+
sycommon_python_lib-0.1.24.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
52
|
+
sycommon_python_lib-0.1.24.dist-info/entry_points.txt,sha256=q_h2nbvhhmdnsOUZEIwpuoDjaNfBF9XqppDEmQn9d_A,46
|
|
53
|
+
sycommon_python_lib-0.1.24.dist-info/top_level.txt,sha256=98CJ-cyM2WIKxLz-Pf0AitWLhJyrfXvyY8slwjTXNuc,17
|
|
54
|
+
sycommon_python_lib-0.1.24.dist-info/RECORD,,
|
|
File without changes
|
{sycommon_python_lib-0.1.22.dist-info → sycommon_python_lib-0.1.24.dist-info}/entry_points.txt
RENAMED
|
File without changes
|
|
File without changes
|