system HF Staff commited on
Commit
765119d
·
verified ·
1 Parent(s): f0f5acb

Sync from felladrin/MiniSearch@faa1dc2 (part 2)

Browse files
server/authorizationSinceLastRestart.ts CHANGED
@@ -8,7 +8,10 @@
8
  * share it, none of which needs to know who asked.
9
  */
10
 
11
- import { getRejectedTokenCacheHits } from "./rejectedTokens.ts";
 
 
 
12
  import {
13
  RATE_LIMIT_DURATION_SECONDS,
14
  RATE_LIMIT_POINTS,
@@ -84,6 +87,7 @@ export function getAuthorizationStats() {
84
  authorized,
85
  rejectedRate: Number(((rejected / requests) * 100 || 0).toFixed(1)),
86
  rejectedTokenCacheHits: getRejectedTokenCacheHits(),
 
87
  reasons: { ...reasons },
88
  // Deep copy, so a caller holding a snapshot for comparison does not watch
89
  // it change under them as later requests arrive.
 
8
  * share it, none of which needs to know who asked.
9
  */
10
 
11
+ import {
12
+ getRejectedTokenCacheHits,
13
+ getRejectedTokenCount,
14
+ } from "./rejectedTokens.ts";
15
  import {
16
  RATE_LIMIT_DURATION_SECONDS,
17
  RATE_LIMIT_POINTS,
 
87
  authorized,
88
  rejectedRate: Number(((rejected / requests) * 100 || 0).toFixed(1)),
89
  rejectedTokenCacheHits: getRejectedTokenCacheHits(),
90
+ distinctRejectedTokens: getRejectedTokenCount(),
91
  reasons: { ...reasons },
92
  // Deep copy, so a caller holding a snapshot for comparison does not watch
93
  // it change under them as later requests arrive.
server/handleTokenVerification.test.ts CHANGED
@@ -199,6 +199,7 @@ describe("handleTokenVerification", () => {
199
  expect(Object.keys(stats).sort()).toEqual([
200
  "authorized",
201
  "bySurface",
 
202
  "limiter",
203
  "reasons",
204
  "rejectedRate",
 
199
  expect(Object.keys(stats).sort()).toEqual([
200
  "authorized",
201
  "bySurface",
202
+ "distinctRejectedTokens",
203
  "limiter",
204
  "reasons",
205
  "rejectedRate",
server/rejectedTokens.test.ts CHANGED
@@ -16,6 +16,18 @@ describe("rejectedTokens", () => {
16
  expect(isRejectedToken("b")).toBe(false);
17
  });
18
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  it("evicts the oldest token once the cap is reached, so the set stays bounded", async () => {
20
  const { addRejectedToken, isRejectedToken, MAX_REJECTED_TOKENS } =
21
  await import("./rejectedTokens");
 
16
  expect(isRejectedToken("b")).toBe(false);
17
  });
18
 
19
+ it("counts each distinct token once, however often it is recorded", async () => {
20
+ const { addRejectedToken, getRejectedTokenCount } = await import(
21
+ "./rejectedTokens"
22
+ );
23
+
24
+ addRejectedToken("a");
25
+ addRejectedToken("a");
26
+ addRejectedToken("b");
27
+
28
+ expect(getRejectedTokenCount()).toBe(2);
29
+ });
30
+
31
  it("evicts the oldest token once the cap is reached, so the set stays bounded", async () => {
32
  const { addRejectedToken, isRejectedToken, MAX_REJECTED_TOKENS } =
33
  await import("./rejectedTokens");
server/rejectedTokens.ts CHANGED
@@ -43,3 +43,12 @@ export function recordRejectedTokenCacheHit(): void {
43
  export function getRejectedTokenCacheHits(): number {
44
  return rejectedTokenCacheHits;
45
  }
 
 
 
 
 
 
 
 
 
 
43
  export function getRejectedTokenCacheHits(): number {
44
  return rejectedTokenCacheHits;
45
  }
46
+
47
+ /**
48
+ * How many distinct dead tokens the set holds, so `/status` can tell one stuck
49
+ * client replaying a single token from many stale links, which the hit count
50
+ * alone cannot. Never exceeds `MAX_REJECTED_TOKENS`.
51
+ */
52
+ export function getRejectedTokenCount(): number {
53
+ return rejectedTokens.size;
54
+ }