添加重连次数太多的保险措施

This commit is contained in:
John Smith 2023-09-09 23:45:06 +08:00
parent 356837c136
commit b1d066b59f
3 changed files with 22 additions and 2 deletions

View File

@ -260,6 +260,16 @@ class OpenLiveClient(ws_base.WebSocketClientBase):
return False
return True
async def _on_before_ws_connect(self, retry_count):
"""
在每次建立连接之前调用可以用来初始化房间
"""
# 重连次数太多则重新init_room保险
reinit_period = max(3, len(self._host_server_url_list or ()))
if retry_count > 0 and retry_count % reinit_period == 0:
self._need_init_room = True
await super()._on_before_ws_connect(retry_count)
def _get_ws_url(self, retry_count) -> str:
"""
返回WebSocket连接的URL可以在这里做故障转移和负载均衡

View File

@ -232,6 +232,16 @@ class BLiveClient(ws_base.WebSocketClientBase):
return False
return True
async def _on_before_ws_connect(self, retry_count):
"""
在每次建立连接之前调用可以用来初始化房间
"""
# 重连次数太多则重新init_room保险
reinit_period = max(3, len(self._host_server_list or ()))
if retry_count > 0 and retry_count % reinit_period == 0:
self._need_init_room = True
await super()._on_before_ws_connect(retry_count)
def _get_ws_url(self, retry_count) -> str:
"""
返回WebSocket连接的URL可以在这里做故障转移和负载均衡

View File

@ -248,7 +248,7 @@ class WebSocketClientBase:
retry_count = 0
while True:
try:
await self._on_before_ws_connect()
await self._on_before_ws_connect(retry_count)
# 连接
async with self._session.ws_connect(
@ -282,7 +282,7 @@ class WebSocketClientBase:
logger.warning('room=%d is reconnecting, retry_count=%d', self.room_id, retry_count)
await asyncio.sleep(1)
async def _on_before_ws_connect(self):
async def _on_before_ws_connect(self, retry_count):
"""
在每次建立连接之前调用可以用来初始化房间
"""