From c3c4da007df9beafcdadf03e97c79ed197c1719e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=93=82=E5=B1=91?= <80164656+boxie123@users.noreply.github.com> Date: Sun, 3 Mar 2024 19:28:18 +0800 Subject: [PATCH] =?UTF-8?q?Fix:=20=E8=B6=85=E5=87=BA=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E6=B1=A0=E4=B8=8A=E9=99=90=E6=97=A0=E6=B3=95=E9=87=8A=E6=94=BE?= =?UTF-8?q?=E3=80=81webui=E4=B8=ADcookie=E6=96=87=E4=BB=B6=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E9=94=99=E8=AF=AF=20(#834)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix: 超出连接池上限无法释放 * 确保Session正常关闭 * 优化pre获取cookie方式 * 修复cookie文件失效表现形式 --- app/lib/use-streamers.ts | 29 ++++++++++++++++++++--------- biliup/__main__.py | 1 + biliup/database/db.py | 10 ++++++++++ biliup/engine/download.py | 3 +++ biliup/handler.py | 1 + biliup/web/__init__.py | 35 ++++++++++++++++++----------------- 6 files changed, 53 insertions(+), 26 deletions(-) diff --git a/app/lib/use-streamers.ts b/app/lib/use-streamers.ts index 7cb2e61..1071930 100644 --- a/app/lib/use-streamers.ts +++ b/app/lib/use-streamers.ts @@ -32,15 +32,26 @@ export function useBiliUsers() { } const updateList = async (item: User) => { - const res = await fetcher(`/bili/space/myinfo?user=${item.value}`, undefined); - const pRes = await proxy(`/bili/proxy?url=${res.data.face}`); - const myBlob = await pRes.blob(); - - return { - ...item, - name: res.data.name, - face: URL.createObjectURL(myBlob), - }; + try { + const res = await fetcher(`/bili/space/myinfo?user=${item.value}`, undefined); + const pRes = await proxy(`/bili/proxy?url=${res.data.face}`); + const myBlob = await pRes.blob(); + + return { + ...item, + name: res.data.name, + face: URL.createObjectURL(myBlob), + }; + } catch (error) { + console.error(error); + const pRes = await proxy("/bili/proxy?url=https://i0.hdslb.com/bfs/face/member/noface.jpg"); + const myBlob = await pRes.blob(); + return { + ...item, + name: "Cookie已失效", + face: URL.createObjectURL(myBlob), + }; + } }; const updateData = async (data: User[]) => { diff --git a/biliup/__main__.py b/biliup/__main__.py index 715e194..3cf8a1a 100644 --- a/biliup/__main__.py +++ b/biliup/__main__.py @@ -50,6 +50,7 @@ def arg_parser(): except FileNotFoundError: print(f'新版本不依赖配置文件,请访问 WebUI 修改配置') config.load_from_db() + db.remove() LOG_CONF.update(config.get('LOGGING', {})) if args.verbose: LOG_CONF['loggers']['biliup']['level'] = args.verbose diff --git a/biliup/database/db.py b/biliup/database/db.py index 88005bb..ab3d565 100644 --- a/biliup/database/db.py +++ b/biliup/database/db.py @@ -47,6 +47,16 @@ class DB: cls.migrate_via_alembic() return run or no_http + @classmethod + def close(cls): + """ 关闭 Session, 释放连接到连接池 """ + Session.close() + + @classmethod + def remove(cls): + """ 释放连接, 并删除注册表中的 Session """ + Session.remove() + @classmethod def get_stream_info(cls, name: str) -> dict: """根据 streamer 获取下载信息, 若不存在则返回空字典""" diff --git a/biliup/engine/download.py b/biliup/engine/download.py index 225c6b5..0bf6405 100644 --- a/biliup/engine/download.py +++ b/biliup/engine/download.py @@ -186,6 +186,7 @@ class DownloadBase: # 将文件名和直播标题存储到数据库 db.update_file_list(self.database_row_id, file_name) db.update_room_title(self.database_row_id, self.room_title) + db.close() retval = self.download(file_name) self.rename(f'{file_name}.{self.suffix}') return retval @@ -208,6 +209,7 @@ class DownloadBase: 'date': date, } self.database_row_id = db.add_stream_info(**stream_info) # 返回数据库中此行记录的 id + db.close() while True: ret = False @@ -264,6 +266,7 @@ class DownloadBase: self.download_cover(time.strftime(self.get_filename().encode("unicode-escape").decode(), date).encode().decode("unicode-escape")) # 更新数据库中封面存储路径 db.update_cover_path(self.database_row_id, self.live_cover_path) + db.remove() logger.info(f'退出下载:{self.__class__.__name__} - {self.fname}') stream_info = { 'name': self.fname, diff --git a/biliup/handler.py b/biliup/handler.py index 6cd544f..9feeeb0 100644 --- a/biliup/handler.py +++ b/biliup/handler.py @@ -132,6 +132,7 @@ def process_upload(stream_info): data, _ = fmt_title_and_desc({ **DB.get_stream_info_by_filename(os.path.splitext(file_list[0].video)[0]), "name": name}) # 如果 restart, data 中会缺失 name 项 + DB.remove() stream_info.update(data) filelist = upload(stream_info) if filelist: diff --git a/biliup/web/__init__.py b/biliup/web/__init__.py index dd9bff9..1f92f81 100644 --- a/biliup/web/__init__.py +++ b/biliup/web/__init__.py @@ -422,15 +422,21 @@ async def dump_config(request): @routes.get('/bili/archive/pre') async def pre_archive(request): - path = 'cookies.json' + # path = 'cookies.json' # conf = Configuration.get_or_none(Configuration.key == 'bilibili-cookies') - conf = Session.scalars( - select(Configuration).where(Configuration.key == 'bilibili-cookies')).first() - if conf is not None: + confs = Session.scalars( + select(Configuration).where(Configuration.key == 'bilibili-cookies')) + # if conf is not None: + # path = conf.value + for conf in confs: path = conf.value - config.load_cookies(path) - cookies = config.data['user']['cookies'] - return web.json_response(BiliBili.tid_archive(cookies)) + try: + config.load_cookies(path) + cookies = config.data['user']['cookies'] + return web.json_response(BiliBili.tid_archive(cookies)) + except: + continue + return web.json_response({"status": 500, 'error': "无可用 cookie 文件"}, status=500) @routes.get('/bili/space/myinfo') @@ -457,14 +463,6 @@ def find_all_folders(directory): return result -@web.middleware -async def get_session(request, handler): - """ 中间件,用来在请求结束时关闭对应线程会话 """ - resp = await handler(request) - Session.remove() - return resp - - async def service(args): try: from importlib.resources import files @@ -472,7 +470,7 @@ async def service(args): # Try backported to PY<37 `importlib_resources`. from importlib_resources import files - app = web.Application(middlewares=[get_session]) + app = web.Application() app.add_routes([ web.get('/api/check_tag', tag_check), web.get('/url-status', url_status), @@ -559,6 +557,8 @@ def create_error_middleware(overrides): except Exception: request.protocol.logger.exception("Error handling request") return await overrides[500](request) + finally: + Session.remove() return error_middleware @@ -596,6 +596,7 @@ def log_startup(host, port) -> None: print("\n".join(messages)) + def get_interface_ip(family: socket.AddressFamily) -> str: """Get the IP address of an external interface. Used when binding to 0.0.0.0 or ::1 to show a more useful URL. @@ -611,4 +612,4 @@ def get_interface_ip(family: socket.AddressFamily) -> str: except OSError: return "::1" if family == socket.AF_INET6 else "127.0.0.1" - return s.getsockname()[0] # type: ignore \ No newline at end of file + return s.getsockname()[0] # type: ignore