Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
_LAST_HEART_BEAT_SUCCESS_TIME = time.time()
# 心跳进程是否意外退出
_HEART_BEAT_EXIT_UNEXPECTEDLY = False
# 上次注册服务的时间,用于避免频繁注册覆盖热加载的服务
_LAST_REGISTRY_TIME = 0


@value('heart-beat.client.sceneType', "fit-registry")
Expand Down Expand Up @@ -83,7 +85,7 @@ def shutdown() -> None:


def _try_heart_beat_once():
global _FAIL_COUNT, _LAST_HEART_BEAT_SUCCESS_TIME
global _FAIL_COUNT, _LAST_HEART_BEAT_SUCCESS_TIME, _LAST_REGISTRY_TIME
try:
heartbeat([HeartBeatInfo(_scene_type(), _alive_time(), _init_delay())],
HeartBeatAddress(get_runtime_worker_id()))
Expand All @@ -93,11 +95,27 @@ def _try_heart_beat_once():
sys_plugin_logger.warning(f"heart beat unstable. "
f"heart_beat_gap={'{:.3f}'.format(heart_beat_gap)}s, "
f"heart_beat_interval={'{:.3f}'.format(_interval() / 1000)}s]")

# 心跳重连成功后,需要重新注册所有服务,确保服务不丢失
# 但为了避免覆盖热加载刚注册的服务,增加时间窗口保护(3倍心跳间隔)
current_time = time.time()
registry_protection_window = 3 * _interval() / 1000 # 保护窗口:3倍心跳间隔
should_registry = False

if _FAIL_COUNT != 0:
# 重连成功,必须注册(解决服务丢失问题)
should_registry = True
sys_plugin_logger.info(f"heart beat reconnect success. [fail_count={_FAIL_COUNT}]")
_FAIL_COUNT = 0
# 当前的优化仅为临时优化,待 Nacos 版注册中心上线后,更新并验证
_registry_fitable_addresses()
elif _LAST_REGISTRY_TIME > 0 and current_time - _LAST_REGISTRY_TIME > registry_protection_window:
# 距离上次注册时间超过保护窗口,可以注册(用于兜底,防止服务丢失)
should_registry = True

if should_registry:
# 当前的优化仅为临时优化,待 Nacos 版注册中心上线后,更新并验证
_registry_fitable_addresses()
_LAST_REGISTRY_TIME = current_time

sys_plugin_logger.debug(f'heart beating success.')
_LAST_HEART_BEAT_SUCCESS_TIME = heart_beat_finish_time
except:
Expand Down Expand Up @@ -168,8 +186,10 @@ def _registry_fitable_addresses():
"""
Register with the registration center after the heartbeat is reconnected.
"""
global _LAST_REGISTRY_TIME
try:
register_all_fit_services()
_LAST_REGISTRY_TIME = time.time()
sys_plugin_logger.debug("In heart beat agent registry all fitable address success.")
except:
sys_plugin_logger.warning(f"In heart beat agent registry all fitable address failed.")
Expand Down