Revert "zen: update tpm rate limit algo"

This reverts commit 6869186fc6.
This commit is contained in:
Frank
2026-05-08 06:31:12 -04:00
parent ec301f6a2c
commit 4a737493ac
@@ -1,4 +1,4 @@
import { and, Database, inArray, sql } from "@opencode-ai/console-core/drizzle/index.js" import { and, Database, eq, inArray, sql } from "@opencode-ai/console-core/drizzle/index.js"
import { ModelTpmRateLimitTable } from "@opencode-ai/console-core/schema/ip.sql.js" import { ModelTpmRateLimitTable } from "@opencode-ai/console-core/schema/ip.sql.js"
import { UsageInfo } from "./provider/provider" import { UsageInfo } from "./provider/provider"
@@ -6,16 +6,12 @@ export function createModelTpmLimiter(providers: { id: string; model: string; tp
const ids = providers.filter((p) => p.tpmLimit).map((p) => `${p.id}/${p.model}`) const ids = providers.filter((p) => p.tpmLimit).map((p) => `${p.id}/${p.model}`)
if (ids.length === 0) return if (ids.length === 0) return
const toInterval = (date: Date) => const yyyyMMddHHmm = parseInt(
parseInt( new Date(Date.now())
date .toISOString()
.toISOString() .replace(/[^0-9]/g, "")
.replace(/[^0-9]/g, "") .substring(0, 12),
.substring(0, 12), )
)
const now = Date.now()
const currInterval = toInterval(new Date(now))
const prevInterval = toInterval(new Date(now - 60_000))
return { return {
check: async () => { check: async () => {
@@ -23,18 +19,13 @@ export function createModelTpmLimiter(providers: { id: string; model: string; tp
tx tx
.select() .select()
.from(ModelTpmRateLimitTable) .from(ModelTpmRateLimitTable)
.where( .where(and(inArray(ModelTpmRateLimitTable.id, ids), eq(ModelTpmRateLimitTable.interval, yyyyMMddHHmm))),
and(
inArray(ModelTpmRateLimitTable.id, ids),
inArray(ModelTpmRateLimitTable.interval, [currInterval, prevInterval]),
),
),
) )
// convert to map of model to count // convert to map of model to count
return data.reduce( return data.reduce(
(acc, curr) => { (acc, curr) => {
acc[curr.id] = Math.max(acc[curr.id] ?? 0, curr.count) acc[curr.id] = curr.count
return acc return acc
}, },
{} as Record<string, number>, {} as Record<string, number>,
@@ -48,7 +39,7 @@ export function createModelTpmLimiter(providers: { id: string; model: string; tp
await Database.use((tx) => await Database.use((tx) =>
tx tx
.insert(ModelTpmRateLimitTable) .insert(ModelTpmRateLimitTable)
.values({ id, interval: currInterval, count: usage }) .values({ id, interval: yyyyMMddHHmm, count: usage })
.onDuplicateKeyUpdate({ set: { count: sql`${ModelTpmRateLimitTable.count} + ${usage}` } }), .onDuplicateKeyUpdate({ set: { count: sql`${ModelTpmRateLimitTable.count} + ${usage}` } }),
) )
}, },