fix(upload): 大文件改为流式落盘 + gunicorn 生产启动 + 请求体上限快速拒绝

400MB 级上传时 file.read() 会让单请求常驻数百 MB 内存,并把「传完->响应」的
间隔拉长到客户端读超时,表现为 499 Client Closed Request。改为按 1MB 分块
写入 .part 后 fsync + 原子改名;同时用 gunicorn(gthread, 单 worker 多线程,
避免 APScheduler 重复执行) 承担生产流量,超时放宽到 1 小时。
This commit is contained in:
zzy
2026-11-13 03:07:04 +08:00
parent 0a23742170
commit 92dedc38e7
6 changed files with 139 additions and 7 deletions
+34
View File
@@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
"""gunicorn 生产配置。
为什么不是多 worker:app.py 在模块级启动 APScheduler(清理/指标任务),多进程会
让同一批定时任务在每个 worker 里各跑一遍。这里用「单 worker + 多线程」,既保证
定时任务只跑一次,又让并发上传互不阻塞(上传是 I/O 密集,GIL 在 read/write 时释放)。
超时给到 1 小时:400MB 在移动网络下光传输就可能十几分钟,之后的落盘也不能被打断。
"""
import os
bind = "0.0.0.0:8080"
workers = 1
worker_class = "gthread"
threads = int(os.environ.get("UPLOAD_THREADS", "16"))
timeout = int(os.environ.get("UPLOAD_TIMEOUT", "3600"))
graceful_timeout = 120
keepalive = 75
# worker 心跳放 tmpfs,避免和附件写盘抢 IO
worker_tmp_dir = "/dev/shm"
limit_request_line = 0
limit_request_field_size = 0
limit_request_fields = 200
accesslog = "-"
errorlog = "-"
loglevel = os.environ.get("UPLOAD_LOG_LEVEL", "info")
access_log_format = '%(h)s "%(r)s" %(s)s %(b)s %(L)s "%(a)s"'
forwarded_allow_ips = "*"