Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
107 commits
Select commit Hold shift + click to select a range
e579c90
Add Paozhu
hggq Nov 7, 2026
6dbdfc8
Add Poazhu
hggq Nov 7, 2026
15e347b
Add Paozhu http1 post chunked
hggq Nov 8, 2026
da7f6c3
Add paozhu
hggq Nov 8, 2026
e4a3cc0
Merge branch 'MDA2AV:main' into main
hggq Nov 8, 2026
5be39ce
Add Paozhu
hggq Nov 9, 2026
08b8f6f
Add Paozhu
hggq Nov 9, 2026
5cd63b2
Add paozhu
hggq Nov 9, 2026
1187d4f
Benchmark results: paozhu [skip ci]
github-actions[bot] Nov 9, 2026
a44b4ac
Merge branch 'MDA2AV:main' into main
hggq Nov 10, 2026
7053697
Edit mulit ioc
hggq Nov 10, 2026
6d4303c
Benchmark results: paozhu [skip ci]
github-actions[bot] Nov 10, 2026
efd9951
Add paozhu
hggq Nov 10, 2026
737060a
Add paozhu
hggq Nov 10, 2026
2ec885f
Add paozhu
hggq Nov 10, 2026
1e2e203
Add paozhu
hggq Nov 10, 2026
c7b539e
Benchmark results: paozhu [skip ci]
github-actions[bot] Nov 10, 2026
6602983
Add paozhu
hggq Nov 10, 2026
50f6d04
Add paozhu
hggq Nov 10, 2026
e7a8d4b
Benchmark results: paozhu [skip ci]
github-actions[bot] Nov 10, 2026
a1df593
Add paozhu
hggq Nov 11, 2026
81cefee
Add paozhu
hggq Nov 11, 2026
a4ddde4
Add paozhu
hggq Nov 11, 2026
fcb0655
Add paozhu
hggq Nov 11, 2026
fa07f5e
Benchmark results: paozhu [skip ci]
github-actions[bot] Nov 11, 2026
5075d16
Add paozhu
hggq Nov 11, 2026
61508df
Add paozhu
hggq Nov 11, 2026
1e9fb1d
Add paozhu
hggq Nov 11, 2026
1714fd6
Benchmark results: paozhu [skip ci]
github-actions[bot] Nov 11, 2026
d2357dc
Add paozhu
hggq Nov 11, 2026
287cdcc
Add paozhu
hggq Nov 11, 2026
27c58e9
Benchmark results: paozhu [skip ci]
github-actions[bot] Nov 11, 2026
9222b6c
Add paozhu
hggq Nov 11, 2026
eece81e
Benchmark results: paozhu [skip ci]
github-actions[bot] Nov 11, 2026
1821391
Add paozhu
hggq Nov 11, 2026
9a85c3a
Benchmark results: paozhu [skip ci]
github-actions[bot] Nov 11, 2026
2332d83
Fix h2 orm
hggq Nov 12, 2026
4b6ea10
Fix paozhu
hggq Nov 12, 2026
99c4b28
Fix orm init 55
hggq Nov 12, 2026
2d8b43b
Benchmark results: paozhu [skip ci]
github-actions[bot] Nov 12, 2026
70d115a
Benchmark results: paozhu [skip ci]
github-actions[bot] Nov 12, 2026
77c98ae
Benchmark results: paozhu [skip ci]
github-actions[bot] Nov 12, 2026
5dc1f75
Fix orm threads pool to 64
hggq Nov 12, 2026
3a62390
Benchmark results: paozhu [skip ci]
github-actions[bot] Nov 12, 2026
6f56f5c
Benchmark results: paozhu [skip ci]
github-actions[bot] Nov 12, 2026
3d87d9b
Add cache httppeer
hggq Nov 12, 2026
b267014
Benchmark results: paozhu [skip ci]
github-actions[bot] Nov 12, 2026
f8ab29e
Fix http2 send ring to 32 slot
hggq Nov 13, 2026
bd78573
Benchmark results: paozhu [skip ci]
github-actions[bot] Nov 13, 2026
4fe99b0
Fix minpool init
hggq Nov 13, 2026
5734d11
Fin minpool init
hggq Nov 13, 2026
bdbe109
Edit db.cpp
hggq Nov 13, 2026
68e71ac
Fix db.cpp
hggq Nov 13, 2026
4971231
Benchmark results: paozhu [skip ci]
github-actions[bot] Nov 13, 2026
81eada4
Benchmark results: paozhu [skip ci]
github-actions[bot] Nov 13, 2026
470e75a
Benchmark results: paozhu [skip ci]
github-actions[bot] Nov 13, 2026
aad6611
Add paozhu thread pool to 128
hggq Nov 14, 2026
8c0e89e
Add paozhu
hggq Nov 14, 2026
70c4ea0
Fix paozhu
hggq Nov 14, 2026
405eae5
Benchmark results: paozhu [skip ci]
github-actions[bot] Nov 14, 2026
9ed7810
Merge branch 'MDA2AV:main' into main
hggq Nov 14, 2026
22cfc37
Merge branch 'MDA2AV:main' into main
hggq Nov 14, 2026
0c734b8
Add websocket
hggq Nov 14, 2026
1e33515
Benchmark results: paozhu [skip ci]
github-actions[bot] Nov 14, 2026
d4c2742
Merge branch 'MDA2AV:main' into main
hggq Nov 15, 2026
81fe8fb
Fix br level
hggq Nov 15, 2026
2cca5ed
Fix orm conn
hggq Nov 15, 2026
91a71be
Benchmark results: paozhu [skip ci]
github-actions[bot] Nov 15, 2026
e3bd1e0
Merge branch 'MDA2AV:main' into main
hggq Nov 15, 2026
f39b2cc
Add 8gbit, static to cache
hggq Nov 15, 2026
4bf90af
Merge branch 'main' into main
hggq Nov 15, 2026
2345926
Merge branch 'MDA2AV:main' into main
hggq Nov 15, 2026
ce41ce5
Add sleep 3s
hggq Nov 15, 2026
ce87853
Add sleep 3s
hggq Nov 15, 2026
4a1bd27
Merge branch 'MDA2AV:main' into main
hggq Nov 15, 2026
98b3e06
Fix curl
hggq Nov 15, 2026
a73b1a0
Add static tls
hggq Nov 15, 2026
dd6f27f
Merge branch 'MDA2AV:main' into main
hggq Nov 16, 2026
a135781
Reset /static/:filename
hggq Nov 16, 2026
250fa32
Benchmark results: paozhu [skip ci]
github-actions[bot] Nov 16, 2026
4331a97
Fix compress
hggq Nov 16, 2026
890da32
Merge branch 'MDA2AV:main' into main
hggq Nov 16, 2026
f22b7e8
Merge branch 'MDA2AV:main' into main
hggq Nov 16, 2026
1a489d6
Merge branch 'main' into main
hggq Nov 16, 2026
e13a89f
Fix static file to dynamic
hggq Nov 16, 2026
8179862
Benchmark results: paozhu [skip ci]
github-actions[bot] Nov 16, 2026
d1cf7ec
Benchmark results: paozhu [skip ci]
github-actions[bot] Nov 16, 2026
9ef28cd
Merge branch 'MDA2AV:main' into main
hggq Nov 17, 2026
2cfa5a8
Fix latency-500k-8cpu
hggq Nov 17, 2026
fa2c6ad
Fix sqlite
hggq Nov 17, 2026
b833fa2
Fix sql
hggq Nov 17, 2026
49fe4f9
Use framework fun
hggq Nov 17, 2026
94664e5
Benchmark results: paozhu [skip ci]
github-actions[bot] Nov 17, 2026
44da3a0
Benchmark results: paozhu [skip ci]
github-actions[bot] Nov 17, 2026
3a42739
Fix threads
hggq Nov 17, 2026
c0362e6
Benchmark results: paozhu [skip ci]
github-actions[bot] Nov 17, 2026
3940d00
Fix orm pool get back
hggq Nov 17, 2026
d509c0c
Fix head body to asio::const_buffer
hggq Nov 17, 2026
b146c12
Fix ORM cache to 8k
hggq Nov 17, 2026
ea4eaae
Fix ORM cache_data to 8k
hggq Nov 17, 2026
3f5ce80
Fix msvc ci
hggq Nov 17, 2026
603f0de
Benchmark results: paozhu [skip ci]
github-actions[bot] Nov 17, 2026
5a0667b
Fix on stat to cache static files
hggq Nov 18, 2026
ac141bc
Benchmark results: paozhu [skip ci]
github-actions[bot] Nov 18, 2026
76a8878
Benchmark results: paozhu [skip ci]
github-actions[bot] Nov 18, 2026
6a55605
Fix static static-h2 64X4 threads
hggq Nov 18, 2026
2a59c20
Benchmark results: paozhu [skip ci]
github-actions[bot] Nov 18, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frameworks/paozhu/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \

# Pinned so mini_project.sh, the controller generator and the CMake entry point
# stay in sync with the scaffold this entry was written against.
ARG PAOZHU_REF=6b0945334a9b5c34f3cf1f9718c6797c07564380
ARG PAOZHU_REF=d93c8b5534bd55674691d83e2436336f8637f160
WORKDIR /paozhu
# Prefer a source archive shipped in the build context (offline / slow-GitHub
# environments); otherwise fetch the pinned revision from codeload. The direct
Expand Down
2 changes: 1 addition & 1 deletion frameworks/paozhu/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Registered through paozhu annotations; path parameters must be the final segment
- `usehtmlcache` is intentionally absent from both conf files; a present key enables the disk pre-check for dotted URLs. Because the key is absent the pre-check never runs, so it never competes with `/static/:filename` either way, and `BENCHMARK` compiling that pre-check out is a no-op for this entry.
- Static compression is served by `arena_static` itself, not the framework: it honours `Accept-Encoding` (`peer->state.br` / `state.gzip`, q=0 rejected upstream), prefers brotli then gzip, and reads the matching `.br` / `.gz` twin from `/data/static` per request — no resident body cache, so the validation suite's mid-run file replacement stays visible. The precompressed body is sent with `peer->compress = 2` (br) / `1` (gzip), which tells the send layer to skip auto compression and only emit `Content-Encoding` + `Vary`; without an accepted coding or a twin it serves the identity file (`compress = 0`). Binary assets (woff2/webp) ship no twin and stay identity. The framework's own static compress cache is therefore unused: `static_file_compress_cache = 0` in both conf files, and the entrypoint no longer pre-warms it.
- The two processes use separate `temppath` / `logpath` directories so they never race on scratch files.
- `cothreadnum` in both conf files is a floor, not the worker count: upstream computes `max(cothreadnum, hardware_concurrency() + 4)`. Under the profile cpuset (`0-31,64-95`, 64 CPUs) each process runs about 68 io threads, not 8, and the second process contributes a second set that stays idle while the other port is being profiled.
- `cothreadnum` is an optional CAP on the io-thread count, not a floor: upstream sets `total_count = std::thread::hardware_concurrency()` and lowers it to `cothreadnum` only when that is smaller (floor 4), then runs one `io_context` per count with `per_ioc_thread_num` run threads each. `hardware_concurrency()` reports the host's online CPUs and ignores the container cpuset, so the shipped `cothreadnum = 128` would leave ~host-core-count io threads per instance — on `latency-500k-8cpu` (cpuset `0-3,64-67`, 8 logical CPUs) that oversubscription cost both throughput and tail latency. `entrypoint.sh` therefore renders `cothreadnum = $(nproc)` into both confs before startup (`nproc` counts via `sched_getaffinity`, i.e. the cpuset: 8 on that profile, 32 on the big ones). The second instance gets the same count and stays mostly idle while the other port is being profiled.
- `PAOZHU_REF` selects the upstream revision. The Dockerfile uses a direct `codeload.github.com` tarball URL: `github.com/.../archive/...` 302-redirects to codeload and that hop is blocked on some networks.
- On slow or offline networks, ship a source archive in the build context instead of downloading it: `git -C /Users/hzq/paozhu archive --format=tar.gz --prefix=paozhu-<sha>/ -o frameworks/paozhu/vendor/paozhu-src.tar.gz HEAD`. The Dockerfile prefers `vendor/paozhu-src.tar.gz` when present. The archive is ~36 MB and is deliberately not checked in.
- `APT_MIRROR` is an optional build arg for networks where `deb.debian.org` is slow, e.g. `--build-arg APT_MIRROR=mirrors.aliyun.com`; empty keeps the distro default.
Expand Down
60 changes: 14 additions & 46 deletions frameworks/paozhu/cost_define.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,14 @@ constexpr bool http2_wu_stream_id_ok(unsigned long long sid, unsigned long long

#define CONST_HTTP2_SEND_FRAME_BUF 15360

// 一条流连续多久没有任何字节真正推进,就放弃它(RST_STREAM,不断连接)。
// 量的是「多久没推进」而不是「等了多久」:慢客户端只要一直在收,
// 等得再久也不该撤流;只有彻底没进展才该撤。
// Stream abandoned (RST_STREAM, no GOAWAY) when no bytes actually advance for N seconds. Measures "time without progress", not "time waited" — slow clients that keep receiving are fine.
#define CONST_HTTP2_SEND_NO_PROGRESS_TIMEOUT 120
// 文件直发单批 gather 的字节上限。一次预留、一次写锁、一次 async_write 最多送这么多
// DATA 字节(仍按对端 MAX_FRAME_SIZE 切成帧)。批尺寸受写锁等待预算约束:慢客户端下
// 整批写出期间锁被占住,同连接其他流等锁超过 CONST_HTTP2_WRITE_LOCK_TIMEOUT 会被撤流。
// 32KB 在 6KB/s 客户端下约 5.3s,对 10s 预算留近一倍余量(旧 16KB 单帧约 2.7s)。
// 对端 MAX_FRAME_SIZE 下限 16384 由解析侧 40216 门禁保证;本值 ≤ 65536 ⇒ 单批至多 4 帧、
// 9 块 buffer(含首批 HEADERS),发送侧 fhs/bufs 的栈上界依此成立。
#define CONST_HTTP2_FILE_BATCH_BYTES 32768

// 每连接 HTTP/2 发送环的槽数。三条硬约束:
// 1. 必须是 2 的幂 —— 下标推进用 & (capacity_ - 1),非 2 的幂会算出错误的下一格;
// 2. 不能超过 255 —— head_/tail_ 是 unsigned char;
Expand All @@ -277,47 +280,12 @@ constexpr bool http2_wu_stream_id_ok(unsigned long long sid, unsigned long long
// MUST fit unsigned char (head_/tail_), and one slot stays empty for the full/empty test,
// so capacity-1 frames are queueable.
#define CONST_HTTP2_RING_QUEUE_CAPACITY 32
// 发送环积压到几个槽就让路。32 槽环有 31 个可排槽,目标是正好留 6 个给
// HEADERS/SETTINGS/RST/GOAWAY/WINDOW_UPDATE 这类控制帧收尾,所以阈值取 24:
// 判据是严格大于(has_size() > 24),积压 24 时仍放行一帧到 25,看到 25 就挂起,
// 稳态最大积压 25,31 − 25 = 6。取 25 会变成最大积压 26、只剩 5 格,反而不达标。
// 8 发送线程下的已知代价:闸门是"先读 has_size() 再 push",读和 push 之间有窗口,
// 8 个推者可能同时通过同一个 24 的检查;6 格空位对 8 个推者不够,最坏一轮会有
// 2 次 push 失败(计入 http2_ring_overflow_count,帧留在对象上由 standby_next
// 下一轮重投,不丢数据)。2 线程时 6 格足够(并发推者数 2 < 6)。
// 发送侧的挂起条件和回灌条件必须用同一个数,否则会出现「挂起来、回灌去」的空转循环。
// Send-ring backpressure slot threshold. 31 usable slots, exactly 6 reserved for control-frame
// tails, hence 24: the test is strictly greater, so a backlog of 24 still admits one more frame
// (to 25) and parking starts at 25; steady-state max is 25 and 31 - 25 = 6. Using 25 would make
// the max 26 and leave only 5. Known cost with 8 send threads: the gate reads has_size() before
// pushing, so up to 8 pushers may pass the same check; 6 free slots cannot absorb 8, so worst case
// 2 pushes fail per round (counted in http2_ring_overflow_count, retried via standby_next — no data
// loss). 2 threads: 6 slots are enough.
// Suspend and refill conditions MUST use the same value to avoid "hang → refill → hang" thrashing.
#define CONST_HTTP2_RING_BACKPRESSURE_SLOTS 24
// 一条流一轮之内除了首帧还能补喂几帧:每轮最多推 SEND_FEED_MAX + 1 = 6 帧(首帧 + 5 次补喂)。
// 首帧由闸门放行,每次补喂之前重取发送环积压,达到 CONST_HTTP2_RING_FEED_SLOTS 或
// 额度用完就停 —— 这个上限是「一条流不许在本轮独占发送线程」的界,不是吞吐旋钮:
// 环空着的时候多喂的帧,最终仍受窗口与传输约束。
// 要让本值真正生效,CONST_HTTP2_RING_FEED_SLOTS 必须 >= 本值 + 1,否则水位先到、一轮凑不满。
// Max extra frames per stream per round: SEND_FEED_MAX + 1 = 6 frames per round (first + 5 extras).
// For this cap to bind, CONST_HTTP2_RING_FEED_SLOTS must be >= SEND_FEED_MAX + 1, else the
// feed-watermark cuts the round short.
#define CONST_HTTP2_SEND_FEED_MAX 5
// 补喂允许的发送环积压水位:环里已排队帧数低于它才继续喂同一条流。取成与让路阈值同一个数
// (24),单条流可以一路喂到积压 24 才停;31 个可排槽里剩下的 7 个留给控制帧收尾。
// 必须 >= SEND_FEED_MAX + 1(6),否则水位先拦、一轮凑不满 6 帧。
// Feed-watermark: keep feeding the same stream while ring queued < this. Set equal to the
// backpressure threshold (24) so one stream may fill up to 24; the remaining 7 usable slots keep
// headroom for control-frame tails. Must be >= SEND_FEED_MAX + 1 (6), else rounds get cut short.
#define CONST_HTTP2_RING_FEED_SLOTS 24
// 发送线程最长能睡多久:等待新发送对象的条件变量带这个期限,醒来跑一次挂起表兜底扫描。
// 取 6 而不是 5,是为了与 httpwatch 主循环那根 5 秒错开 —— 两条周期任务若同刻醒来会
// 去抢同一批锁(idle 清理要持 socket_session_lists_mutex,兜底要持 send_data_mutex)。
// 没有这个期限,「所有发送对象都挂在表上」这个状态本身就让发送线程睡在无超时的 wait 上,
// 而挂在表上的对象恰恰只有这一趟扫描会去续期与撤流。
// Max send-thread sleep (seconds) — wake at least this often to run the parked-belt sweep (renews time_limit + issues RST for stuck streams). Chosen 6 (not 5) to de-phase from httpwatch's 5s cycle (avoids simultaneous lock contention). Without deadline: all sends parked ⇒ thread sleeps forever; belt never runs.
#define CONST_HTTP2_BELT_SWEEP_SECONDS 6

// 一次窗口停等的总预算:到期 RST(CANCEL) 撤流,连接保留。按次重算,不是整条响应的
// 预算;被 WU 叫醒后用剩余量重挂,小增量不续命。
#define CONST_HTTP2_WINDOW_STALL_TIMEOUT 10
// 写锁等待预算,和窗口停等预算分开:慢写占锁不该被当成窗口停等而误撤流。
#define CONST_HTTP2_WRITE_LOCK_TIMEOUT 10

// RST_STREAM 流错误码(RFC 9113 §7)。CANCEL 表示「流被撤销」,不表示对端违反协议;
// 本地背压/过载放弃发送时应使用它,而不是 PROTOCOL_ERROR(0x01)。
Expand Down
6 changes: 3 additions & 3 deletions frameworks/paozhu/src/conf/server-h1.conf
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
[default]
threadmax = 1024
threadmin = 5
cothreadnum = 128 ; Use this value < number of CPU cores
per_ioc_thread_num = 1 ;
cothreadnum = 64 ; 0 = auto (host cores)
per_ioc_thread_num = 4 ;
orm_ioc_num = 4 ;ORM io_context num;
orm_thread_pool_num = 128 ;0 use hardware_concurrency()
orm_thread_pool_num = 0 ;0 use hardware_concurrency()
http2_send_loop_num = 8 ;

httpport = 8082
Expand Down
6 changes: 3 additions & 3 deletions frameworks/paozhu/src/conf/server.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
[default]
threadmax = 1024
threadmin = 5
cothreadnum = 128 ; Use this value < number of CPU cores
per_ioc_thread_num = 1 ;
cothreadnum = 64 ; 0 = auto (host cores)
per_ioc_thread_num = 4 ;
orm_ioc_num = 4 ;ORM io_context num;
orm_thread_pool_num = 128 ;0 use hardware_concurrency()
orm_thread_pool_num = 0 ;0 use hardware_concurrency()
http2_send_loop_num = 8 ;

httpport = 8080
Expand Down
173 changes: 123 additions & 50 deletions frameworks/paozhu/src/controller/src/arena.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@

#include <chrono>
#include <cstdio>
#include <mutex>
#include <cstdlib>
#include <fstream>
#include <iterator>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -495,6 +497,56 @@ bool safe_filename(const std::string &name)
return true;
}

// ── 静态文件懒加载缓存 ──────────────────────────────────────────────────
// HttpArena static-h2 固定 20 个文件。键是 (文件名, 编码):miss 时把存在的
// 源文件 / .gz / .br 变体全部读入 unordered_map(每文件至多 3 条,共 ≤60 条),
// 之后任何 Accept-Encoding 都命中内存。entrypoint.sh 用 curl 预热完再放
// 压测进来,因此 benchmark 运行期间零 fopen / 零 fread;每次命中 stat 一次
// 比对 mtime/size——validate 的 staleness 探针会原地替换文件,必须跟随磁盘。
// 20 文件 br ≈ 418KB + gz + identity ≈ 170KB → 合计 < 1MB。
struct cached_static
{
std::string name; // 纯文件名(与 enc 字节拼接成缓存键)
std::string body; // 预加载的文件内容(可能是压缩后的)
std::string path; // 实际读取的磁盘路径(含 .br/.gz 后缀),命中重校验用
unsigned char enc; // 0=identity, 1=gzip, 2=brotli
const char *mime; // mime type
time_t mtime = 0; // 读取时源文件的 mtime/size,任一变化即失效重读
off_t size = 0;
};

static std::mutex g_static_cache_mutex;
static std::unordered_map<std::string, cached_static> g_static_cache;

// 键 = 文件名 + 编码字节。safe_filename 只放行字母数字 .-_,
// 追加 0/1/2 字节不会与任何合法文件名歧义。
static std::string cache_key(const std::string &name, unsigned char enc)
{
std::string k = name;
k.push_back(static_cast<char>(enc));
return k;
}

// 锁内拷贝出完整 cached_static 返回,调用方拿到独立副本,
// 后续访问不受并发插入影响
static bool lookup_cached(const std::string &filename, unsigned char enc, cached_static &out)
{
std::lock_guard<std::mutex> lock(g_static_cache_mutex);
auto it = g_static_cache.find(cache_key(filename, enc));
if (it == g_static_cache.end())
return false;
out = it->second;
return true;
}

// 写入缓存(加锁:同名同编码已存在则覆盖——磁盘文件被替换后重读的新内容要顶掉旧条目)
static void insert_cached(cached_static cs)
{
std::lock_guard<std::mutex> lock(g_static_cache_mutex);
const std::string key = cache_key(cs.name, cs.enc);
g_static_cache[key] = std::move(cs);
}

} // namespace

namespace http
Expand Down Expand Up @@ -588,72 +640,93 @@ asio::awaitable<std::string> arena_static(std::shared_ptr<httppeer> peer)
co_return "";
}

// The Accept-Encoding header decides whether compression is wanted at all:
// state.br / state.gzip are set only for codings the client accepts (q=0 is
// rejected upstream). Prefer brotli, then gzip; each twin is used only when
// the client accepts that coding AND the twin exists. Otherwise serve the
// identity file. Reading per request (no resident cache) keeps the validation
// suite's mid-run replacement visible and costs no q11 on the request path;
// binary assets (woff2/webp) ship no twin and stay identity.
std::string path = "/data/static/" + filename;
unsigned char enc = 0;
struct stat st {};
bool stat_ok = false;
// ── Fast path: 按客户端偏好 br → gz → identity 查缓存 ──
cached_static entry;
bool hit = false;
if (peer->state.br)
hit = lookup_cached(filename, 2, entry);
if (!hit && peer->state.gzip)
hit = lookup_cached(filename, 1, entry);
if (!hit)
hit = lookup_cached(filename, 0, entry);
if (hit)
{
const std::string twin = path + ".br";
if (::stat(twin.c_str(), &st) == 0 && (st.st_mode & S_IFREG) != 0)
// staleness 探针会原地替换文件(等长不同字节):stat 比对 mtime/size,
// 变了就落慢路径重读。稳态下只有这一次 stat,没有 fopen/fread。
struct stat cst {};
if (::stat(entry.path.c_str(), &cst) == 0 &&
cst.st_mtime == entry.mtime && cst.st_size == entry.size)
{
path = twin;
enc = 2;
stat_ok = true;
peer->type(entry.mime);
peer->compress = entry.enc;
peer->length(entry.body.size());
peer->output = std::move(entry.body);
co_return "";
}
}
if (!stat_ok && peer->state.gzip)

// ── Slow path: 首次 miss 或磁盘文件已变(重校验失败)──
// 源文件 / .gz / .br 存在的变体全部读入缓存,之后任何编码都命中;
// 响应挑客户端能接受的最优变体(br > gz > identity)。
const std::string base = "/data/static/" + filename;
struct variant_t
{
const char *suffix;
unsigned char enc;
};
const variant_t candidates[3] = {{".br", 2}, {".gz", 1}, {"", 0}};

std::string served;
unsigned char served_enc = 0;
bool found = false;
for (const variant_t &v : candidates)
{
const std::string twin = path + ".gz";
if (::stat(twin.c_str(), &st) == 0 && (st.st_mode & S_IFREG) != 0)
const std::string path = base + v.suffix;
struct stat st {};
if (::stat(path.c_str(), &st) != 0 || (st.st_mode & S_IFREG) == 0)
{
path = twin;
enc = 1;
stat_ok = true;
continue;
}
std::ifstream in(path, std::ios::binary);
if (!in.is_open())
{
continue;
}
std::string body;
body.resize(static_cast<std::size_t>(st.st_size));
in.read(&body[0], static_cast<std::streamsize>(st.st_size));
body.resize(static_cast<std::size_t>(in.gcount()));

cached_static cs;
cs.name = filename;
cs.body = body;
cs.enc = v.enc;
cs.mime = mime_of(filename);
cs.path = path;
cs.mtime = st.st_mtime;
cs.size = st.st_size;
insert_cached(std::move(cs));

const bool acceptable = (v.enc == 0) || (v.enc == 2 && peer->state.br) ||
(v.enc == 1 && peer->state.gzip);
if (!found && acceptable)
{
served = std::move(body);
served_enc = v.enc;
found = true;
}
}
if (!stat_ok)
{
enc = 0;
stat_ok = ::stat(path.c_str(), &st) == 0 && (st.st_mode & S_IFREG) != 0;
}
if (!stat_ok)
{
peer->status(404);
peer->type("text/plain");
peer->output = "Not Found";
co_return "";
}

// Size the body once, then read straight into it. Short read (file shrank
// between stat and read): keep what arrived. A zero-size file is valid.
std::ifstream in(path, std::ios::binary);
if (!in.is_open())
if (!found)
{
peer->status(404);
peer->type("text/plain");
peer->output = "Not Found";
co_return "";
}
std::string body;
body.resize(static_cast<std::size_t>(st.st_size));
in.read(&body[0], static_cast<std::streamsize>(st.st_size));
body.resize(static_cast<std::size_t>(in.gcount()));

peer->output = std::move(body);
// Content-Type stays the original asset type; Content-Encoding (from compress)
// carries the coding. compress=1/2 tells the send layer the body is already
// compressed so it does not compress again; 0 leaves it identity.
peer->type(mime_of(filename));
peer->compress = enc;
peer->length(peer->output.size());
peer->compress = served_enc;
peer->length(served.size());
peer->output = std::move(served);
co_return "";
}

Expand Down
Loading