添加HTTP请求超时

This commit is contained in:
John Smith
2021-03-28 18:30:31 +08:00
parent 4601ee328c
commit 77d0277d4e
8 changed files with 25 additions and 34 deletions

View File

@@ -32,7 +32,7 @@ class Command(enum.IntEnum):
UPDATE_TRANSLATION = 7 UPDATE_TRANSLATION = 7
_http_session = aiohttp.ClientSession() _http_session = aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=10))
room_manager: Optional['RoomManager'] = None room_manager: Optional['RoomManager'] = None
@@ -576,7 +576,7 @@ class RoomInfoHandler(api.base.ApiHandler):
res.status, res.reason) res.status, res.reason)
return room_id, 0 return room_id, 0
data = await res.json() data = await res.json()
except aiohttp.ClientConnectionError: except (aiohttp.ClientConnectionError, asyncio.TimeoutError):
logger.exception('room %d _get_room_info failed', room_id) logger.exception('room %d _get_room_info failed', room_id)
return room_id, 0 return room_id, 0
@@ -600,7 +600,7 @@ class RoomInfoHandler(api.base.ApiHandler):
# res.status, res.reason) # res.status, res.reason)
# return cls._host_server_list_cache # return cls._host_server_list_cache
# data = await res.json() # data = await res.json()
# except aiohttp.ClientConnectionError: # except (aiohttp.ClientConnectionError, asyncio.TimeoutError):
# logger.exception('room %d _get_server_host_list failed', room_id) # logger.exception('room %d _get_server_host_list failed', room_id)
# return cls._host_server_list_cache # return cls._host_server_list_cache
# #

Submodule blivedm updated: 13712f89eb...4669b2c1c9

View File

@@ -146,7 +146,11 @@ export default class ChatClientDirect {
onReceiveTimeout() { onReceiveTimeout() {
window.console.warn('接收消息超时') window.console.warn('接收消息超时')
this.receiveTimeoutTimerId = null this.receiveTimeoutTimerId = null
// 直接丢弃阻塞的websocket不等onclose回调了
this.websocket.onopen = this.websocket.onclose = this.websocket.onmessage = null
this.websocket.close() this.websocket.close()
this.onWsClose()
} }
onWsClose () { onWsClose () {

View File

@@ -24,6 +24,7 @@ if (process.env.NODE_ENV === 'development') {
// 开发时使用localhost:12450 // 开发时使用localhost:12450
axios.defaults.baseURL = 'http://localhost:12450' axios.defaults.baseURL = 'http://localhost:12450'
} }
axios.defaults.timeout = 10 * 1000
Vue.use(VueRouter) Vue.use(VueRouter)
Vue.use(VueI18n) Vue.use(VueI18n)

View File

@@ -18,7 +18,7 @@ logger = logging.getLogger(__name__)
DEFAULT_AVATAR_URL = '//static.hdslb.com/images/member/noface.gif' DEFAULT_AVATAR_URL = '//static.hdslb.com/images/member/noface.gif'
_main_event_loop = asyncio.get_event_loop() _main_event_loop = asyncio.get_event_loop()
_http_session = aiohttp.ClientSession() _http_session = aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=10))
# user_id -> avatar_url # user_id -> avatar_url
_avatar_url_cache: Dict[int, str] = {} _avatar_url_cache: Dict[int, str] = {}
# 正在获取头像的Futureuser_id -> Future # 正在获取头像的Futureuser_id -> Future

View File

@@ -21,7 +21,7 @@ NO_TRANSLATE_TEXTS = {
} }
_main_event_loop = asyncio.get_event_loop() _main_event_loop = asyncio.get_event_loop()
_http_session = aiohttp.ClientSession() _http_session = aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=10))
_translate_providers: List['TranslateProvider'] = [] _translate_providers: List['TranslateProvider'] = []
# text -> res # text -> res
_translate_cache: Dict[str, str] = {} _translate_cache: Dict[str, str] = {}
@@ -127,7 +127,6 @@ class TencentTranslate(TranslateProvider):
self._reinit_future = None self._reinit_future = None
# 连续失败的次数 # 连续失败的次数
self._fail_count = 0 self._fail_count = 0
self._cool_down_future = None
async def init(self): async def init(self):
self._reinit_future = asyncio.ensure_future(self._reinit_coroutine()) self._reinit_future = asyncio.ensure_future(self._reinit_coroutine())
@@ -174,14 +173,13 @@ class TencentTranslate(TranslateProvider):
try: try:
while True: while True:
await asyncio.sleep(30) await asyncio.sleep(30)
while True: logger.debug('TencentTranslate reinit')
logger.debug('TencentTranslate reinit') try:
try: await self._do_init()
if await self._do_init(): except asyncio.CancelledError:
break raise
except Exception: except Exception:
logger.exception('TencentTranslate init error:') logger.exception('TencentTranslate init error:')
await asyncio.sleep(3 * 60)
except asyncio.CancelledError: except asyncio.CancelledError:
pass pass
@@ -238,26 +236,14 @@ class TencentTranslate(TranslateProvider):
def _on_fail(self): def _on_fail(self):
self._fail_count += 1 self._fail_count += 1
# 目前没有测试出被ban的情况为了可靠性连续失败20次时冷却重新init # 目前没有测试出被ban的情况为了可靠性连续失败20次时冷却直到下次重新init
if self._fail_count >= 20 and self._cool_down_future is None: if self._fail_count >= 20:
self._cool_down_future = asyncio.ensure_future(self._cool_down()) self._cool_down()
async def _cool_down(self): def _cool_down(self):
logger.info('TencentTranslate is cooling down') logger.info('TencentTranslate is cooling down')
self._qtv = self._qtk = '' self._qtv = self._qtk = ''
try: self._fail_count = 0
while True:
await asyncio.sleep(3 * 60)
logger.info('TencentTranslate reinit')
try:
if await self._do_init():
self._fail_count = 0
break
except Exception:
logger.exception('TencentTranslate init error:')
finally:
logger.info('TencentTranslate finished cooling down')
self._cool_down_future = None
class YoudaoTranslate(TranslateProvider): class YoudaoTranslate(TranslateProvider):

View File

@@ -1,3 +1,3 @@
aiohttp==3.5.4 aiohttp==3.7.4
sqlalchemy==1.3.13 sqlalchemy==1.3.13
tornado==6.0.2 tornado==6.0.2

View File

@@ -13,7 +13,7 @@ def check_update():
async def _do_check_update(): async def _do_check_update():
try: try:
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=10)) as session:
async with session.get('https://api.github.com/repos/xfgryujk/blivechat/releases/latest') as r: async with session.get('https://api.github.com/repos/xfgryujk/blivechat/releases/latest') as r:
data = await r.json() data = await r.json()
if data['name'] != VERSION: if data['name'] != VERSION: