File size: 10,256 Bytes
6111b2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/**

 * sqliteQuotaStore.ts β€” SQLite-backed QuotaStore implementation.

 *

 * Uses a Sliding Window Counter with 2 buckets per (apiKeyId, dimensionKey):

 *   effective = prev Γ— (1 βˆ’ elapsed/window) + curr

 *   currentBucketIndex = Math.floor(nowMs / WINDOW_MS[window])

 *   currentBucketStartMs = currentBucketIndex Γ— WINDOW_MS[window]

 *   elapsed = nowMs βˆ’ currentBucketStartMs

 *

 * Concurrency: per-(apiKeyId|dimensionKey) in-memory mutex prevents races on

 * the read-modify-write sequence (same anti-thundering-herd pattern used by

 * auth.ts::markAccountUnavailable). UPSERT in incrementBucket is still atomic

 * at the SQLite level.

 *

 * Part of: Group B β€” Quota Sharing Engine (plan 22, frente F6).

 */

import {
  getPool,
  getBucket,
  incrementBucket,
  getPair,
  sumPoolDimension,
} from "@/lib/localDb";
import { WINDOW_MS, dimensionKeyToString } from "./dimensions";
import type { DimensionKey } from "./dimensions";
import type { QuotaStore, PoolUsageSnapshot } from "./types";
import { computeBurnRateFromWindow } from "./burnRate";

// ---------------------------------------------------------------------------
// In-memory mutex (anti-thundering-herd, same pattern as auth.ts)
// ---------------------------------------------------------------------------

const _mutexes = new Map<string, Promise<void>>();

function mutexKey(apiKeyId: string, dimKey: string): string {
  return `${apiKeyId}|${dimKey}`;
}

async function withMutex<T>(key: string, fn: () => Promise<T>): Promise<T> {
  const current = _mutexes.get(key) ?? Promise.resolve();
  let resolve!: () => void;
  const next = new Promise<void>((res) => {
    resolve = res;
  });
  _mutexes.set(key, next);

  try {
    await current;
    return await fn();
  } finally {
    resolve();
    // Clean up only if this promise is still the active one
    if (_mutexes.get(key) === next) {
      _mutexes.delete(key);
    }
  }
}

// ---------------------------------------------------------------------------
// Sliding window helpers
// ---------------------------------------------------------------------------

function slidingWindowEffective(

  curr: number,

  prev: number,

  nowMs: number,

  windowMs: number

): number {
  const currentBucketIndex = Math.floor(nowMs / windowMs);
  const currentBucketStartMs = currentBucketIndex * windowMs;
  const elapsed = nowMs - currentBucketStartMs;
  const weight = 1 - elapsed / windowMs;
  return prev * weight + curr;
}

// ---------------------------------------------------------------------------
// SqliteQuotaStore
// ---------------------------------------------------------------------------

export class SqliteQuotaStore implements QuotaStore {
  /**

   * Increment consumption for (apiKeyId, dim) by `cost` and return the

   * new sliding-window effective value.

   */
  async consume(apiKeyId: string, dim: DimensionKey, cost: number): Promise<number> {
    const nowMs = Date.now();
    const dimKey = dimensionKeyToString(dim);
    const windowMs = WINDOW_MS[dim.window];
    const currentBucket = Math.floor(nowMs / windowMs);

    return withMutex(mutexKey(apiKeyId, dimKey), async () => {
      // UPSERT is atomic at the DB level
      incrementBucket(apiKeyId, dimKey, currentBucket, cost, nowMs);

      // Read fresh pair to compute effective
      const { curr, prev } = getPair(apiKeyId, dimKey, currentBucket);
      return slidingWindowEffective(curr, prev, nowMs, windowMs);
    });
  }

  /**

   * Peek at the current effective consumption without modifying any counters.

   */
  async peek(apiKeyId: string, dim: DimensionKey): Promise<number> {
    const nowMs = Date.now();
    const dimKey = dimensionKeyToString(dim);
    const windowMs = WINDOW_MS[dim.window];
    const currentBucket = Math.floor(nowMs / windowMs);

    const { curr, prev } = getPair(apiKeyId, dimKey, currentBucket);
    return slidingWindowEffective(curr, prev, nowMs, windowMs);
  }

  /**

   * Return the real pool-wide consumption for a dimension in the current

   * sliding window, summed across ALL apiKeyIds that share the same

   * dimensionKey (i.e. same poolId + unit + window).

   *

   * Uses the same 2-bucket sliding-window formula as peek(), applied once

   * to the pool totals so the result is consistent with per-key semantics.

   */
  async poolConsumedTotal(poolId: string, dim: DimensionKey): Promise<number> {
    const nowMs = Date.now();
    const dimKey = dimensionKeyToString(dim);
    const windowMs = WINDOW_MS[dim.window];
    const currentBucket = Math.floor(nowMs / windowMs);

    const { currTotal, prevTotal } = sumPoolDimension(dimKey, currentBucket);
    return slidingWindowEffective(currTotal, prevTotal, nowMs, windowMs);
  }

  /**

   * Return a PoolUsageSnapshot for the given pool, aggregating per-key

   * consumption across all dimensions and computing fairShare / deficit /

   * borrowing flags.

   */
  async poolUsage(poolId: string): Promise<PoolUsageSnapshot> {
    const nowMs = Date.now();
    const pool = getPool(poolId);

    if (!pool) {
      return {
        poolId,
        generatedAt: new Date(nowMs).toISOString(),
        dimensions: [],
      };
    }

    // QuotaPool does not carry dimension definitions β€” those live in the
    // ProviderPlan, resolved separately. Without a plan we cannot enumerate
    // dimension keys here, so this lightweight snapshot returns no dimensions.
    // The REST route (F8) calls poolUsageWithDimensions() with the resolved
    // plan to produce the full per-dimension response.
    return {
      poolId,
      generatedAt: new Date(nowMs).toISOString(),
      dimensions: [],
    };
  }

  /**

   * Build a PoolUsageSnapshot for a given pool with explicit dimensions from

   * the provider plan. This is the richer version used by REST routes (F8)

   * that already resolved the plan.

   *

   * This method is not part of the QuotaStore interface but is available on

   * the concrete class for callers that have plan data.

   */
  async poolUsageWithDimensions(
    poolId: string,
    planDimensions: Array<{ unit: string; window: string; limit: number }>
  ): Promise<PoolUsageSnapshot> {
    const nowMs = Date.now();
    const pool = getPool(poolId);

    if (!pool) {
      return {
        poolId,
        generatedAt: new Date(nowMs).toISOString(),
        dimensions: [],
      };
    }

    const { allocations } = pool;
    const totalWeight = allocations.reduce((sum, a) => sum + a.weight, 0);

    const dimensionSnapshots: PoolUsageSnapshot["dimensions"] = [];

    for (const planDim of planDimensions) {
      const windowMs = WINDOW_MS[planDim.window as keyof typeof WINDOW_MS];
      if (!windowMs) continue;

      let consumedTotal = 0;
      const perKey: PoolUsageSnapshot["dimensions"][number]["perKey"] = [];

      for (const alloc of allocations) {
        const dim: DimensionKey = {
          poolId,
          unit: planDim.unit as DimensionKey["unit"],
          window: planDim.window as DimensionKey["window"],
        };
        const consumed = await this.peek(alloc.apiKeyId, dim);
        consumedTotal += consumed;

        const effectiveWeight = totalWeight > 0 ? alloc.weight : 0;
        const fairShare = (effectiveWeight / 100) * planDim.limit;
        const deficit = consumed - fairShare;
        const borrowing = consumed > fairShare;

        perKey.push({
          apiKeyId: alloc.apiKeyId,
          consumed,
          fairShare,
          deficit,
          borrowing,
        });
      }

      dimensionSnapshots.push({
        unit: planDim.unit as PoolUsageSnapshot["dimensions"][number]["unit"],
        window: planDim.window as PoolUsageSnapshot["dimensions"][number]["window"],
        limit: planDim.limit,
        consumedTotal,
        perKey,
      });
    }

    // Burn rate: derive from the sliding window (single-snapshot, no history needed).
    const tokenDim = dimensionSnapshots.find((d) => d.unit === "tokens");
    let burnRate: PoolUsageSnapshot["burnRate"];
    if (tokenDim && tokenDim.consumedTotal > 0) {
      const windowMs = WINDOW_MS[tokenDim.window as keyof typeof WINDOW_MS];
      const remaining = tokenDim.limit - tokenDim.consumedTotal;
      const rateResult = computeBurnRateFromWindow(tokenDim.consumedTotal, windowMs, remaining);
      burnRate = {
        tokensPerSecond: rateResult.tokensPerSecond,
        timeToExhaustionMs: rateResult.timeToExhaustionMs,
      };
    }

    return {
      poolId,
      generatedAt: new Date(nowMs).toISOString(),
      dimensions: dimensionSnapshots,
      burnRate,
    };
  }

  /**

   * Clear consumption counters for (apiKeyId, dim). Test-only.

   * Implemented by writing a large negative delta to bring curr + prev to 0,

   * OR by directly zeroing out the bucket rows.

   *

   * We zero by reading current and then applying -curr as delta.

   * The previous bucket is left as-is (its weight will decay naturally).

   */
  async clear(apiKeyId: string, dim: DimensionKey): Promise<void> {
    const nowMs = Date.now();
    const dimKey = dimensionKeyToString(dim);
    const windowMs = WINDOW_MS[dim.window];
    const currentBucket = Math.floor(nowMs / windowMs);
    const prevBucket = currentBucket - 1;

    await withMutex(mutexKey(apiKeyId, dimKey), async () => {
      // Zero current bucket
      const currVal = getBucket(apiKeyId, dimKey, currentBucket);
      if (currVal !== 0) {
        incrementBucket(apiKeyId, dimKey, currentBucket, -currVal, nowMs);
      }
      // Zero previous bucket
      const prevVal = getBucket(apiKeyId, dimKey, prevBucket);
      if (prevVal !== 0) {
        incrementBucket(apiKeyId, dimKey, prevBucket, -prevVal, nowMs);
      }
    });
  }
}

// Singleton per process
let _instance: SqliteQuotaStore | null = null;

export function getSqliteQuotaStore(): SqliteQuotaStore {
  if (!_instance) {
    _instance = new SqliteQuotaStore();
  }
  return _instance;
}

export function resetSqliteQuotaStore(): void {
  _instance = null;
}