# 契約與韌性

Source: https://gortexa.sheng.page/examples/resilience/

## 04 breaking-gate — 契約破壞閘

證明 `gortexa regen` 的 buf breaking gate 會擋下破壞相容性的 proto 改動、放行 additive 改動。這條情境同時走 `regen` 的 `isGitToplevel()` 路徑（macOS `/tmp` → `/private/tmp` symlink 曾讓閘靜默跳過，已修）。

```bash
gortexa create bg --module github.com/me/bg --repo "$REPO" --ref main
cd bg && make gen                 # 首次 gen：無 main ref，閘合理跳過
git add -A && git commit -m base && git branch -M main

# 刪掉 Resource 的 owner 欄位（tag 3）——破壞性改動
gortexa regen                     # 非零退出：gate BLOCKS breaking edit
```

additive 改動（新增欄位）則 `regen` 正常通過。gate 讀的是 git `main` ref 的 proto，不是 `gen/`。

## 05 resilience-shutdown — SIGTERM 優雅排空

帶認證的混合負載打到一半送 SIGTERM，斷言伺服器在 `shutdown_timeout` 內排空 in-flight 請求、以 exit 0 退出、無 panic，重啟後可恢復。

```bash
./server &                        # 真實二進位，讓 SIGTERM 直接落在 server（非 go run wrapper）
./loadgen ...                     # 認證負載
kill -TERM $SRV                   # 負載進行中送 SIGTERM
wait $SRV                         # exit 0，排空完成，drain 時間 <= shutdown_timeout
```

用真實二進位（非 `go run`）是為了讓退出碼明確——`go run` 會多包一層行程。
