perf: keep the server responsive while a knowledge base is exported (#34600)

Exporting a knowledge base packed and compressed all of its files while holding up the worker process serving it, so every other request on that worker (chats, page loads, health checks) waited until the archive was done. With several exports running at once, other users saw pauses of more than a second. The download still waits for the archive, but the server now keeps answering everyone else while it is packed. In our test the worst pause dropped from about 1.3 seconds to about 0.4 seconds, and the downloaded file is unchanged.

| 200 files of ~60 KB, 10 exports at once, worst of 2 runs | before | after |
| --- | --- | --- |
| slowest health check while exporting | 1328 ms | 403 ms |

Fixes #34599
This commit is contained in:
Classic298 authored and GitHub committed 2026-11-17 22:19:48 +09:00
1 parent 0f54aee859
commit aca79d17b2
1 file changed
+13 -9
+13 -9
View File
@@ -2301,15 +2301,19 @@ async def export_knowledge_by_id(id: str, user=Depends(get_admin_user), db: Asyn
# Create zip file in memory
zip_buffer = io.BytesIO()
with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zf:
for file in files:
content = file.data.get('content', '') if file.data else ''
if content:
# Use original filename with .txt extension
filename = file.filename
if not filename.endswith('.txt'):
filename = f'{filename}.txt'
zf.writestr(filename, content)
def write_zip():
with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zf:
for file in files:
content = file.data.get('content', '') if file.data else ''
if content:
# Use original filename with .txt extension
filename = file.filename
if not filename.endswith('.txt'):
filename = f'{filename}.txt'
zf.writestr(filename, content)
await asyncio.to_thread(write_zip)
# Sanitize knowledge name for filename
safe_name = ''.join(c if c.isalnum() or c in ' -_' else '_' for c in knowledge.name)