mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 02:36:59 +01:00
fix(deploy): every deploy was a 13-second outage (#283)
Caddy's access log carried 784 502s over 7 days on fleetcrown, in 77 distinct minutes. Every one of those minutes is a deploy: the app is healthy before and healthy after, so nothing else ever reported it. Traced end to end on the 2026-08-14 16:30 restart. SIGTERM at :49, the server stopped answering at :51, and it then sat bound-but-refusing until systemd gave up and SIGKILLed it at 16:31:04 — after which the replacement logged "Ready in 0ms". Next's standalone server closes its listener on SIGTERM and then waits on keep-alive connections that Caddy never closes, so the drain can never finish. The whole 15s TimeoutStopSec was being served to users as errors, and the startup it was protecting takes one second. Two changes, because the window and its visibility are separate bugs: - TimeoutStopSec 15 -> 3. Waiting on a drain that cannot complete is pure downtime. - lb_try_duration on the proxied blocks. Caddy was giving up the instant the restarting upstream refused a connection; now it re-dials across the gap. Only the dial is retried, so a request that already reached the app is never replayed and a POST cannot apply twice. Measured after: a full restart under a continuous public poll served 36/36 200s, against ~13-15 502s for the same operation before. sync-infra.sh generates the units and vhosts for the other 15 apps on the box and had the identical defect, so the fix goes in the templates too; they pick it up on their next sync. verify.sh gains a guard, since this class is invisible to every check that looks at a steady state. Co-authored-by: t <t@t.t> Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
4 files changed
+50
-2
No files matched your search
@@ -8,6 +8,15 @@
|
||||
# reaches the origin and presence goes stale. It must have `flush_interval -1`
|
||||
# so each event frame is flushed immediately. verify.sh asserts the bridge
|
||||
# block stays encode-free.
|
||||
#
|
||||
# RESTART RULE (load-bearing): every block needs `lb_try_duration`, or a deploy
|
||||
# is a user-visible outage. Without it Caddy 502s the instant the upstream
|
||||
# refuses a connection, and a `systemctl restart` refuses for several seconds —
|
||||
# 784 502s in 7 days, every one inside a restart window. With it, Caddy re-dials
|
||||
# for the duration and the restart becomes a brief delay instead of an error.
|
||||
# Retries here are safe: Caddy only retries when the *dial* failed, so the
|
||||
# request never reached the app and a POST cannot be applied twice.
|
||||
# verify.sh asserts every proxied block keeps it.
|
||||
|
||||
# Bridge (4001) — SSE only (presence, rawkey, peek). encode deliberately absent.
|
||||
bridge.orangecat.ch {
|
||||
@@ -15,6 +24,10 @@ bridge.orangecat.ch {
|
||||
reverse_proxy 127.0.0.1:4001 {
|
||||
# SSE needs each frame flushed immediately.
|
||||
flush_interval -1
|
||||
# Ride out a restart instead of 502ing. Governs the initial dial only,
|
||||
# so a live SSE stream is unaffected.
|
||||
lb_try_duration 20s
|
||||
lb_try_interval 250ms
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,5 +39,8 @@ fleetcrown.orangecat.ch {
|
||||
encode zstd gzip
|
||||
reverse_proxy 127.0.0.1:4002 {
|
||||
flush_interval -1
|
||||
# Ride out the ~3s restart window (TimeoutStopSec=3) instead of 502ing.
|
||||
lb_try_duration 20s
|
||||
lb_try_interval 250ms
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,15 @@ RestartSec=3
|
||||
# default 90s stop made a deploy's restart block long enough that verification hit
|
||||
# a still-restarting server and rolled back. KillMode=mixed SIGTERMs the main
|
||||
# process then SIGKILLs the cgroup, freeing :4002 fast for the new process.
|
||||
TimeoutStopSec=15
|
||||
#
|
||||
# Why 3 and not 15: the drain never completes, so every second here is pure
|
||||
# downtime. Measured on a real deploy (2026-08-14 16:30): SIGTERM at :49, the
|
||||
# server stopped answering at :51, and it then sat bound-but-refusing until
|
||||
# SIGKILL at 16:31:04 — ~13s of 502s, after which the replacement logged
|
||||
# "Ready in 0ms". Next closes its listener on SIGTERM but waits on keep-alive
|
||||
# connections Caddy never closes, so waiting longer buys nothing and costs
|
||||
# availability. 784 502s in 7 days traced to exactly this window.
|
||||
TimeoutStopSec=3
|
||||
KillMode=mixed
|
||||
StandardOutput=journal
|
||||
StandardError=journal
|
||||
|
||||
@@ -50,7 +50,13 @@ RestartSec=3
|
||||
# Bound the stop phase so a `systemctl restart` can't block on a slow-draining
|
||||
# Next process (default 90s). KillMode=mixed SIGTERMs the main proc then SIGKILLs
|
||||
# the cgroup, freeing the port fast for the new process.
|
||||
TimeoutStopSec=15
|
||||
#
|
||||
# 3s, not 15: a Next standalone server closes its listener on SIGTERM and then
|
||||
# waits on keep-alive connections the proxy never closes, so the drain never
|
||||
# finishes and the whole timeout is served as 502s. Measured on fleetcrown
|
||||
# (2026-08-14): ~13s of refused connections per deploy. Cut the wait; the
|
||||
# matching lb_try_duration below hides what is left.
|
||||
TimeoutStopSec=3
|
||||
KillMode=mixed
|
||||
# Memory ceiling: a leaking/runaway Next process is OOM-killed (and restarted)
|
||||
# alone instead of taking the whole small box down. MemoryHigh throttles first.
|
||||
@@ -89,6 +95,12 @@ $caddy_domains {
|
||||
}
|
||||
reverse_proxy 127.0.0.1:$PORT {
|
||||
flush_interval -1
|
||||
# Re-dial across a restart instead of returning 502 the moment the upstream
|
||||
# refuses. A deploy takes the port down for a few seconds; without this that
|
||||
# window is served to users as errors. Only the dial is retried, so a request
|
||||
# that already reached the app is never replayed.
|
||||
lb_try_duration 20s
|
||||
lb_try_interval 250ms
|
||||
}
|
||||
}
|
||||
EOF
|
||||
|
||||
@@ -53,4 +53,16 @@ printf "%-4s %-22s public=%s (401/200 expected)\n" "$([ "$sb" = 401 ] || [ "$sb"
|
||||
enc=$(box "awk '/bridge\\.orangecat\\.ch/,/^}/' /etc/caddy/Caddyfile 2>/dev/null | grep -c '^[[:space:]]*encode'" 2>/dev/null | tr -d '[:space:]')
|
||||
printf "%-4s %-22s %s\n" "$([ "${enc:-1}" = 0 ] && echo 'OK ' || { echo FAIL; fail=1; })" "bridge-sse-no-encode" "encode lines in bridge block: ${enc:-?}"
|
||||
|
||||
# Restart-window guard: a proxied block without `lb_try_duration` turns every
|
||||
# deploy into user-visible 502s, because Caddy gives up the instant the
|
||||
# restarting upstream refuses a connection. This was measured at 784 502s in 7
|
||||
# days on fleetcrown alone, all inside restart windows, and it is invisible
|
||||
# without this check — the app is healthy before and after, so nothing else
|
||||
# reports it. Covers the two blocks this repo owns in the handcrafted
|
||||
# Caddyfile; the rest are generated by sync-infra.sh, which emits the directive.
|
||||
for blk in bridge fleetcrown; do
|
||||
n=$(box "awk '/^${blk}\\.orangecat\\.ch/,/^}/' /etc/caddy/Caddyfile 2>/dev/null | grep -c 'lb_try_duration'" 2>/dev/null | tr -d '[:space:]')
|
||||
printf "%-4s %-22s %s\n" "$([ "${n:-0}" -ge 1 ] 2>/dev/null && echo 'OK ' || { echo FAIL; fail=1; })" "${blk}-restart-retry" "lb_try_duration in ${blk} block: ${n:-?}"
|
||||
done
|
||||
|
||||
exit $fail
|
||||
Reference in new issue
Block a user