concurrency.test.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /**
  2. * ConcurrencyController 的纯状态机用例(docs/dynamic-workflow/concurrency.md「The controller」
  3. * 的两行控制器用例)。无时钟:`now` 由用例传入。度量是模型请求(决策 34),阻尼靠 epoch(决策 35),
  4. * 减系数 0.75、K = 4、lastGood 只由完成的 streak 设定(决策 40)。
  5. */
  6. import { describe, expect, it } from "vitest";
  7. import {
  8. CONCURRENCY_DECREASE_FACTOR,
  9. CONCURRENCY_FLOOR,
  10. CONCURRENCY_IDLE_RESET_MS,
  11. CONCURRENCY_INCREASE_AFTER_SUCCESSES,
  12. ConcurrencyController,
  13. } from "../../src/engine/index.js";
  14. const KEY = "anthropic/claude";
  15. const K = CONCURRENCY_INCREASE_AFTER_SUCCESSES;
  16. const T0 = 1_000_000;
  17. /** 准入 n 个请求,返回它们的 epoch。 */
  18. function admit(c: ConcurrencyController, n: number, now = T0): number[] {
  19. const epochs: number[] = [];
  20. for (let i = 0; i < n; i++) epochs.push(c.admitted(now));
  21. return epochs;
  22. }
  23. /** 准入 n 个请求并让它们全部在当前 epoch 成功;返回期间产生的全部 change。 */
  24. function roundTrip(c: ConcurrencyController, n: number, now = T0) {
  25. const changes = [];
  26. for (let i = 0; i < n; i++) {
  27. const epoch = c.admitted(now);
  28. changes.push(...c.succeeded(now, epoch));
  29. }
  30. return changes;
  31. }
  32. /** 一个当前 epoch 的 429(先准入再限流)。 */
  33. function throttleNow(c: ConcurrencyController, reason: "rate_limited" | "provider_overloaded" | "offpeak_queued" = "rate_limited", retryAfterMs?: number, now = T0) {
  34. const epoch = c.admitted(now);
  35. return c.throttled(now, epoch, reason, retryAfterMs);
  36. }
  37. describe("ConcurrencyController — 常量(决策 40)", () => {
  38. it("减系数 0.75、每级 4 次成功", () => {
  39. expect(CONCURRENCY_DECREASE_FACTOR).toBe(0.75);
  40. expect(K).toBe(4);
  41. });
  42. });
  43. describe("ConcurrencyController — 减(决策 4/6/11/35/40)", () => {
  44. it("当前 epoch 的限流 → cap × 0.75 向下取整并翻 epoch;反复限流一路减到地板 1", () => {
  45. const c = new ConcurrencyController(KEY, 16);
  46. expect(c.snapshot().epoch).toBe(0);
  47. // 减 cap 不设 lastGood(未被证明),只记 lastBad = 撞墙时的 cap。
  48. expect(throttleNow(c)).toEqual([{ key: KEY, previous: 16, next: 12, reason: "rate_limited", lastBad: 16 }]);
  49. expect(c.snapshot()).toMatchObject({ epoch: 1, lastBad: 16 });
  50. expect(c.snapshot().lastGood).toBeUndefined();
  51. for (const expected of [9, 6, 4, 3, 2, 1]) {
  52. const [change] = throttleNow(c, "provider_overloaded");
  53. expect(change?.next).toBe(expected);
  54. }
  55. expect(c.snapshot()).toMatchObject({ cap: CONCURRENCY_FLOOR, epoch: 7, inFlight: 0, lastBad: 2 });
  56. // 地板上再限流:cap 不变、无 Retry-After ⇒ 不发事件,但仍翻一页(同一批请求只裁决一次)。
  57. expect(throttleNow(c)).toEqual([]);
  58. expect(c.snapshot().epoch).toBe(8);
  59. });
  60. it("同一批 13 个同 epoch 的 429 只减一次、只发一条事件;其余只清 streak、减 inFlight", () => {
  61. const c = new ConcurrencyController(KEY, 13);
  62. const epochs = admit(c, 13);
  63. expect(new Set(epochs)).toEqual(new Set([0]));
  64. expect(c.snapshot().inFlight).toBe(13);
  65. const changes = [];
  66. for (const epoch of epochs) changes.push(...c.throttled(T0, epoch, "rate_limited"));
  67. expect(changes).toEqual([{ key: KEY, previous: 13, next: 9, reason: "rate_limited", lastBad: 13 }]);
  68. expect(c.snapshot()).toMatchObject({ cap: 9, epoch: 1, inFlight: 0, successStreak: 0 });
  69. });
  70. it("旧 epoch 的 429 不动 cap、不发事件,但清 streak、减 inFlight、刷新 cooldown", () => {
  71. const c = new ConcurrencyController(KEY, 8);
  72. const stale = admit(c, 3); // epoch 0
  73. throttleNow(c); // 8→6, epoch 1
  74. c.waiters(T0, 1);
  75. roundTrip(c, K - 1); // 当前 epoch 的成功:streak K−1(还差一次兑现)
  76. expect(c.snapshot()).toMatchObject({ cap: 6, successStreak: K - 1, inFlight: 3 });
  77. expect(c.throttled(T0, stale[0]!, "rate_limited", 9_000)).toEqual([]);
  78. expect(c.snapshot()).toMatchObject({ cap: 6, epoch: 1, successStreak: 0, inFlight: 2, cooldownUntil: T0 + 9_000 });
  79. // 更早的 Retry-After 不会把 cooldown 往前拉。
  80. expect(c.throttled(T0, stale[1]!, "rate_limited", 1_000)).toEqual([]);
  81. expect(c.snapshot().cooldownUntil).toBe(T0 + 9_000);
  82. });
  83. it("下一次裁决必须来自新 cap 下发出的请求:旧 epoch 全部撞墙后,新 epoch 的 429 才再减", () => {
  84. const c = new ConcurrencyController(KEY, 13);
  85. const stale = admit(c, 13);
  86. for (const epoch of stale) c.throttled(T0, epoch, "rate_limited"); // 13→9 一次
  87. expect(c.snapshot().cap).toBe(9);
  88. expect(throttleNow(c)).toEqual([{ key: KEY, previous: 9, next: 6, reason: "rate_limited", lastBad: 9 }]);
  89. });
  90. it("Retry-After 冻结新准入直到 deadline;地板上带 Retry-After 的限流仍发事件", () => {
  91. const c = new ConcurrencyController(KEY, 4);
  92. const [change] = throttleNow(c, "rate_limited", 20_000);
  93. expect(change).toMatchObject({ previous: 4, next: 3, cooldownMs: 20_000 });
  94. expect(c.canAdmit(T0 + 19_999)).toBe(false);
  95. expect(c.canAdmit(T0 + 20_000)).toBe(true);
  96. throttleNow(c, "rate_limited", undefined, T0 + 20_000); // 3→2
  97. throttleNow(c, "rate_limited", undefined, T0 + 20_000); // 2→1
  98. expect(c.snapshot().cap).toBe(1);
  99. expect(throttleNow(c, "offpeak_queued", 5_000, T0 + 20_001)).toEqual([
  100. { key: KEY, previous: 1, next: 1, reason: "offpeak_queued", lastBad: 1, cooldownMs: 5_000 },
  101. ]);
  102. });
  103. it("canAdmit 在 inFlight ≥ cap 时为假,release 后为真", () => {
  104. const c = new ConcurrencyController(KEY, 2);
  105. const [a, b] = admit(c, 2);
  106. expect(c.canAdmit(T0)).toBe(false);
  107. c.succeeded(T0, a!);
  108. expect(c.canAdmit(T0)).toBe(true);
  109. c.ended(T0);
  110. expect(c.snapshot().inFlight).toBe(0);
  111. void b;
  112. });
  113. it("瞬态非限流失败只清 streak、减 inFlight,cap 不动", () => {
  114. const c = new ConcurrencyController(KEY, 8);
  115. c.waiters(T0, 1);
  116. roundTrip(c, K - 1);
  117. admit(c, 1);
  118. expect(c.snapshot()).toMatchObject({ successStreak: K - 1, inFlight: 1 });
  119. expect(c.failedTransient(T0)).toEqual([]);
  120. expect(c.snapshot()).toMatchObject({ cap: 8, successStreak: 0, inFlight: 0, epoch: 0 });
  121. });
  122. it("ended 只减 inFlight", () => {
  123. const c = new ConcurrencyController(KEY, 8);
  124. c.waiters(T0, 1);
  125. roundTrip(c, K - 1);
  126. admit(c, 2);
  127. expect(c.ended(T0)).toEqual([]);
  128. expect(c.snapshot()).toMatchObject({ successStreak: K - 1, inFlight: 1, cap: 8 });
  129. });
  130. });
  131. describe("ConcurrencyController — 增(决策 4/15/33/35/40)", () => {
  132. function lowered(): ConcurrencyController {
  133. const c = new ConcurrencyController(KEY, 16);
  134. throttleNow(c); // cap 12, lastBad 16, lastGood 缺席, epoch 1
  135. return c;
  136. }
  137. it("K 次当前 epoch 的连续成功证明当前 cap(lastGood = cap),有等待者才 +1", () => {
  138. const c = lowered();
  139. c.waiters(T0, 1);
  140. expect(roundTrip(c, K - 1)).toEqual([]);
  141. expect(c.snapshot().lastGood).toBeUndefined();
  142. expect(roundTrip(c, 1)).toEqual([{ key: KEY, previous: 12, next: 13, reason: "recovered", lastGood: 12, lastBad: 16 }]);
  143. expect(c.snapshot().successStreak).toBe(0);
  144. });
  145. it("无等待者时 streak 照累积、lastGood 照证明,但 cap 不兑现;等待者出现后下一次成功即兑现", () => {
  146. const c = lowered();
  147. c.waiters(T0, 0);
  148. expect(roundTrip(c, K)).toEqual([]);
  149. expect(c.snapshot()).toMatchObject({ cap: 12, successStreak: K, lastGood: 12 });
  150. c.waiters(T0, 1);
  151. expect(roundTrip(c, 1)[0]?.next).toBe(13);
  152. });
  153. it("旧 epoch 的成功不计 streak(减 cap 后仍在飞的那批请求跑完不算证据)", () => {
  154. const c = new ConcurrencyController(KEY, 32);
  155. c.waiters(T0, 3);
  156. const stale = admit(c, 32);
  157. c.throttled(T0, stale[0]!, "rate_limited"); // cap 24, epoch 1
  158. for (const epoch of stale.slice(1)) expect(c.succeeded(T0, epoch)).toEqual([]);
  159. expect(c.snapshot()).toMatchObject({ cap: 24, successStreak: 0, inFlight: 0 });
  160. expect(c.snapshot().lastGood).toBeUndefined();
  161. // 新 epoch 的 K 次成功才兑现。
  162. expect(roundTrip(c, K)[0]?.next).toBe(25);
  163. });
  164. it("永不越过天花板;天花板上的完整 streak 仍把它证明为 lastGood", () => {
  165. const c = new ConcurrencyController(KEY, 2);
  166. throttleNow(c); // cap 1
  167. c.waiters(T0, 1);
  168. const changes = roundTrip(c, K * 3);
  169. expect(changes.map((x) => x.next)).toEqual([2]);
  170. expect(c.snapshot()).toMatchObject({ cap: 2, lastGood: 2 });
  171. });
  172. });
  173. describe("ConcurrencyController — 记住可用水位(决策 22/40)", () => {
  174. /** 从 cap 16 减到 12,再一路爬到 target(每级 K 次成功)。 */
  175. function climbTo(target: number): ConcurrencyController {
  176. const c = new ConcurrencyController(KEY, 16);
  177. throttleNow(c);
  178. c.waiters(T0, 1);
  179. while (c.snapshot().cap < target) roundTrip(c, K);
  180. return c;
  181. }
  182. it("lastGood 之上被限流 → 退回 lastGood 并记 lastBad,不按系数减,仍翻 epoch", () => {
  183. const c = climbTo(14);
  184. expect(c.snapshot()).toMatchObject({ cap: 14, lastGood: 13, epoch: 1 });
  185. expect(throttleNow(c)).toEqual([
  186. { key: KEY, previous: 14, next: 13, reason: "rate_limited", lastGood: 13, lastBad: 14 },
  187. ]);
  188. expect(c.snapshot().epoch).toBe(2);
  189. });
  190. it("lastGood 及以下被限流 → 墙下移:按系数减、清掉 lastGood(已证伪)、记 lastBad", () => {
  191. const c = climbTo(14);
  192. throttleNow(c); // 回 13
  193. expect(throttleNow(c)).toEqual([{ key: KEY, previous: 13, next: 9, reason: "rate_limited", lastBad: 13 }]);
  194. expect(c.snapshot().lastGood).toBeUndefined();
  195. });
  196. it("每级需 K 次成功(K−1 次不够);新 cap 要靠自己的 streak 重新证明", () => {
  197. const c = climbTo(14);
  198. throttleNow(c); // 14 > lastGood 13 ⇒ 回 13
  199. expect(throttleNow(c)[0]).toMatchObject({ previous: 13, next: 9 });
  200. expect(roundTrip(c, K - 1)).toEqual([]);
  201. expect(c.snapshot().lastGood).toBeUndefined();
  202. expect(roundTrip(c, 1)[0]).toMatchObject({ next: 10, lastGood: 9 });
  203. const small = new ConcurrencyController(KEY, 6);
  204. throttleNow(small); // cap 4, lastBad 6
  205. small.waiters(T0, 1);
  206. roundTrip(small, K); // cap 5, lastGood 4
  207. roundTrip(small, K); // cap 6, lastGood 5
  208. throttleNow(small); // 6 > lastGood 5 ⇒ 回 5,lastBad 6
  209. expect(small.snapshot()).toMatchObject({ cap: 5, lastGood: 5, lastBad: 6 });
  210. });
  211. it("真实上限 5、天花板 13:13 →429→ 9 →429→ 6 →429→ 4 → 5 → 6 →429→ 5,稳定在 5", () => {
  212. const c = new ConcurrencyController(KEY, 13);
  213. c.waiters(T0, 1);
  214. expect(throttleNow(c)[0]?.next).toBe(9);
  215. expect(throttleNow(c)[0]?.next).toBe(6);
  216. expect(throttleNow(c)[0]?.next).toBe(4);
  217. expect(roundTrip(c, K)[0]).toMatchObject({ next: 5, lastGood: 4 });
  218. expect(roundTrip(c, K)[0]).toMatchObject({ next: 6, lastGood: 5 });
  219. expect(throttleNow(c)[0]).toMatchObject({ previous: 6, next: 5, lastGood: 5, lastBad: 6 });
  220. // 此后每 K 次成功探一次 6,每次只花一个 429、退回 5。
  221. for (let round = 0; round < 3; round++) {
  222. expect(roundTrip(c, K)[0]?.next).toBe(6);
  223. expect(throttleNow(c)[0]).toMatchObject({ previous: 6, next: 5, lastBad: 6 });
  224. }
  225. expect(c.snapshot()).toMatchObject({ cap: 5, lastGood: 5, lastBad: 6 });
  226. });
  227. });
  228. describe("ConcurrencyController — 空闲重置(决策 20)", () => {
  229. it("空闲 5 分钟且无在飞时,任一信号先重置到天花板并清空 lastGood/lastBad/cooldown,翻 epoch", () => {
  230. const c = new ConcurrencyController(KEY, 16);
  231. throttleNow(c, "rate_limited", 60_000); // cap 12, epoch 1, inFlight 0
  232. const later = T0 + CONCURRENCY_IDLE_RESET_MS;
  233. expect(c.observe(later - 1)).toEqual([]);
  234. expect(c.observe(later)).toEqual([{ key: KEY, previous: 12, next: 16, reason: "idle_reset" }]);
  235. expect(c.snapshot()).toMatchObject({ cap: 16, successStreak: 0, epoch: 2 });
  236. expect(c.snapshot().lastGood).toBeUndefined();
  237. expect(c.snapshot().lastBad).toBeUndefined();
  238. expect(c.snapshot().cooldownUntil).toBeUndefined();
  239. // 重置是幂等的:再来一个空闲信号不再发事件、不再翻页。
  240. expect(c.observe(later + 1)).toEqual([]);
  241. expect(c.snapshot().epoch).toBe(2);
  242. });
  243. it("有在飞时不重置", () => {
  244. const c = new ConcurrencyController(KEY, 16);
  245. admit(c, 1);
  246. throttleNow(c); // inFlight 1
  247. expect(c.observe(T0 + CONCURRENCY_IDLE_RESET_MS * 2)).toEqual([]);
  248. expect(c.snapshot().cap).toBe(12);
  249. });
  250. it("重置后从天花板起步的第一次 429 按系数减(lastGood 已清空);重置与限流在同一信号里是两条 change", () => {
  251. const c = new ConcurrencyController(KEY, 16);
  252. throttleNow(c);
  253. const later = T0 + CONCURRENCY_IDLE_RESET_MS;
  254. // 治理器的准入路径:observe(重置)→ admitted → …;这里模拟一个跳过 observe 直接带旧 epoch 来的 429:
  255. // idleReset 先翻页,所以它按旧 epoch 处理——不减,只有 idle_reset 一条。
  256. const epoch = c.admitted(T0);
  257. c.ended(T0);
  258. expect(c.throttled(later, epoch, "rate_limited")).toEqual([
  259. { key: KEY, previous: 12, next: 16, reason: "idle_reset" },
  260. ]);
  261. // 走正规路径:observe 之后准入再限流,才是对天花板的评价。
  262. expect(c.observe(later)).toEqual([]);
  263. expect(throttleNow(c, "rate_limited", undefined, later)).toEqual([
  264. { key: KEY, previous: 16, next: 12, reason: "rate_limited", lastBad: 16 },
  265. ]);
  266. });
  267. });
  268. describe("ConcurrencyController — 增长上限(docs/dynamic-workflow/concurrency.md「Increase」「The governor」)", () => {
  269. it("起点是 initial;自动增长越过 initial、停在 growthLimit;完整 streak 仍把 growthLimit 证明为 lastGood", () => {
  270. const c = new ConcurrencyController(KEY, 4, 8);
  271. expect(c.snapshot()).toMatchObject({ initial: 4, growthLimit: 8, cap: 4 });
  272. c.waiters(T0, 1);
  273. const changes = roundTrip(c, K * 10);
  274. expect(changes.map((x) => x.next)).toEqual([5, 6, 7, 8]);
  275. expect(changes.every((x) => x.reason === "recovered")).toBe(true);
  276. expect(c.snapshot()).toMatchObject({ cap: 8, lastGood: 8 });
  277. });
  278. it("缺省 growthLimit = initial:与从前「天花板即上界」逐字相同", () => {
  279. const c = new ConcurrencyController(KEY, 6);
  280. expect(c.snapshot()).toMatchObject({ initial: 6, growthLimit: 6 });
  281. c.waiters(T0, 1);
  282. expect(roundTrip(c, K * 3)).toEqual([]);
  283. });
  284. it("抬高增长上限不立刻动 cap、不发事件;cap 此后一级一级爬上去", () => {
  285. const c = new ConcurrencyController(KEY, 4, 8);
  286. expect(c.setGrowthLimit(T0, 20)).toEqual([]);
  287. expect(c.snapshot()).toMatchObject({ growthLimit: 20, cap: 4 });
  288. c.waiters(T0, 1);
  289. expect(roundTrip(c, K * 20).map((x) => x.next)).toEqual(
  290. Array.from({ length: 16 }, (_, i) => 5 + i),
  291. );
  292. expect(c.snapshot().cap).toBe(20);
  293. });
  294. it("压低到 cap 之下:cap 当场拉到新上限、发 limit_lowered、lastGood 夹到上限、清 streak、翻 epoch", () => {
  295. const c = new ConcurrencyController(KEY, 4, 20);
  296. c.waiters(T0, 1);
  297. roundTrip(c, K * 12); // cap 16,lastGood 15
  298. expect(c.snapshot().cap).toBe(16);
  299. roundTrip(c, 2); // streak 2
  300. const stale = c.admitted(T0); // 旧 cap 下发出的请求
  301. const epochBefore = c.snapshot().epoch;
  302. expect(c.setGrowthLimit(T0, 8)).toEqual([
  303. { key: KEY, previous: 16, next: 8, reason: "limit_lowered", lastGood: 8 },
  304. ]);
  305. expect(c.snapshot()).toMatchObject({
  306. growthLimit: 8,
  307. cap: 8,
  308. lastGood: 8,
  309. successStreak: 0,
  310. epoch: epochBefore + 1,
  311. });
  312. // 那个旧 cap 下发出的请求撞 429:不是对新 cap 的裁决,不再减。
  313. expect(c.throttled(T0, stale, "rate_limited")).toEqual([]);
  314. expect(c.snapshot().cap).toBe(8);
  315. });
  316. it("压低但 cap 本就不高于新上限:不动 cap、不发事件、不翻 epoch", () => {
  317. const c = new ConcurrencyController(KEY, 4, 20);
  318. const epoch = c.snapshot().epoch;
  319. expect(c.setGrowthLimit(T0, 8)).toEqual([]);
  320. expect(c.snapshot()).toMatchObject({ growthLimit: 8, cap: 4, epoch });
  321. });
  322. it("增长上限永不低于 initial", () => {
  323. const c = new ConcurrencyController(KEY, 6, 12);
  324. expect(c.setGrowthLimit(T0, 2)).toEqual([]);
  325. expect(c.snapshot()).toMatchObject({ growthLimit: 6, cap: 6 });
  326. });
  327. it("空闲重置回 initial,而不是回增长上限", () => {
  328. const c = new ConcurrencyController(KEY, 4, 8);
  329. c.waiters(T0, 1);
  330. roundTrip(c, K * 10); // cap 8
  331. c.waiters(T0, 0);
  332. const later = T0 + CONCURRENCY_IDLE_RESET_MS;
  333. expect(c.observe(later)).toEqual([{ key: KEY, previous: 8, next: 4, reason: "idle_reset" }]);
  334. expect(c.snapshot()).toMatchObject({ cap: 4, growthLimit: 8 });
  335. expect(c.snapshot().lastGood).toBeUndefined();
  336. });
  337. });
  338. describe("ConcurrencyController — 抬高上界时直接起跳(seed,docs/dynamic-workflow/concurrency.md「Seeding」)", () => {
  339. it("seed 把 cap 当场拉到目标(不超过增长上限),发 seeded,清 lastGood、清 streak、翻 epoch", () => {
  340. const c = new ConcurrencyController(KEY, 8, 32);
  341. c.waiters(T0, 1);
  342. roundTrip(c, K + 2); // cap 9,lastGood 8,streak 2
  343. const epoch = c.snapshot().epoch;
  344. expect(c.seed(T0, 32)).toEqual([{ key: KEY, previous: 9, next: 32, reason: "seeded" }]);
  345. expect(c.snapshot()).toMatchObject({ cap: 32, successStreak: 0, epoch: epoch + 1 });
  346. expect(c.snapshot().lastGood).toBeUndefined();
  347. expect(c.snapshot().lastBad).toBeUndefined();
  348. });
  349. it("目标超过增长上限时只跳到增长上限", () => {
  350. const c = new ConcurrencyController(KEY, 8, 20);
  351. expect(c.seed(T0, 50)).toEqual([{ key: KEY, previous: 8, next: 20, reason: "seeded" }]);
  352. });
  353. it("目标不高于 cap:什么都不做", () => {
  354. const c = new ConcurrencyController(KEY, 8, 32);
  355. const epoch = c.snapshot().epoch;
  356. expect(c.seed(T0, 8)).toEqual([]);
  357. expect(c.seed(T0, 3)).toEqual([]);
  358. expect(c.snapshot()).toMatchObject({ cap: 8, epoch });
  359. });
  360. it("起跳之后第一次 429 按系数减(32 → 24),而不是退回旧的 lastGood", () => {
  361. const c = new ConcurrencyController(KEY, 8, 32);
  362. c.waiters(T0, 1);
  363. roundTrip(c, K); // lastGood 8
  364. c.seed(T0, 32);
  365. expect(throttleNow(c)).toEqual([
  366. { key: KEY, previous: 32, next: 24, reason: "rate_limited", lastBad: 32 },
  367. ]);
  368. });
  369. it("五分钟内有过任何 429(哪怕是旧 epoch 的)即不起跳;过了五分钟再起跳", () => {
  370. const c = new ConcurrencyController(KEY, 8, 32);
  371. const stale = c.admitted(T0);
  372. throttleNow(c); // 当前 epoch:8 → 6
  373. c.throttled(T0 + 1_000, stale, "rate_limited"); // 旧 epoch,同样算「刚被拒过」
  374. expect(c.snapshot().lastThrottledAt).toBe(T0 + 1_000);
  375. expect(c.seed(T0 + 2_000, 32)).toEqual([]);
  376. expect(c.snapshot().cap).toBe(6);
  377. // 一直有请求在飞,所以不会空闲重置;五分钟后证据过期,起跳放行。
  378. c.admitted(T0 + 1_000);
  379. const later = T0 + 1_000 + CONCURRENCY_IDLE_RESET_MS;
  380. expect(c.seed(later, 32)).toEqual([{ key: KEY, previous: 6, next: 32, reason: "seeded" }]);
  381. });
  382. it("空闲重置清掉 lastThrottledAt", () => {
  383. const c = new ConcurrencyController(KEY, 8, 32);
  384. throttleNow(c);
  385. const later = T0 + CONCURRENCY_IDLE_RESET_MS;
  386. expect(c.observe(later)).toHaveLength(1);
  387. expect(c.snapshot().lastThrottledAt).toBeUndefined();
  388. });
  389. });