Skip to content

Commit 5b6a4a2

Browse files
committed
fix(billing): keep the ledger statement bound in the billing constants module
1 parent 237c22b commit 5b6a4a2

4 files changed

Lines changed: 12 additions & 11 deletions

File tree

‎apps/sim/lib/billing/constants.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ export const DEFAULT_OVERAGE_THRESHOLD = 100
3939
*/
4040
export const BILLING_LOCK_TIMEOUT_MS = 5_000
4141

42+
/**
43+
* Bound on one ledger sum. A large payer's period covers millions of rows, and from a cold
44+
* cache or under heavy I/O the sum can run for tens of seconds; past this the database ends it
45+
* and the read fails, so a caller that admits on the sum fails closed rather than waiting
46+
* without limit. The usage gate derives its coalescing deadline from this bound, so the sum
47+
* always ends at the database before the gate gives up on it.
48+
*/
49+
export const USAGE_LEDGER_STATEMENT_TIMEOUT_MS = 60_000
50+
4251
/**
4352
* Available credit tiers. Each tier maps a credit amount to the underlying dollar
4453
* cost and carries that tier's fixed weekly refresh allowance.

‎apps/sim/lib/billing/core/usage-gate-cache.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { LRUCache } from 'lru-cache'
2+
import { USAGE_LEDGER_STATEMENT_TIMEOUT_MS } from '@/lib/billing/constants'
23
import {
34
type AttributedUsageLimitsResult,
45
type BillingAttributionSnapshot,
56
checkAttributedUsageLimits,
67
} from '@/lib/billing/core/billing-attribution'
7-
import { USAGE_LEDGER_STATEMENT_TIMEOUT_MS } from '@/lib/billing/core/usage-log'
88
import { coalesceLocally } from '@/lib/concurrency/singleflight'
99

1010
/**

‎apps/sim/lib/billing/core/usage-log.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ vi.mock('@/lib/billing/subscriptions/utils', () => ({
3333
isOrgScopedSubscription: mockIsOrgScopedSubscription,
3434
}))
3535

36+
import { USAGE_LEDGER_STATEMENT_TIMEOUT_MS } from '@/lib/billing/constants'
3637
import {
3738
CUMULATIVE_COST_EPSILON,
3839
CumulativeUsageContextMismatchError,
@@ -44,7 +45,6 @@ import {
4445
resolveCumulativeTopUp,
4546
UNKNOWN_CURSOR_MESSAGE,
4647
UnknownUsageCursorError,
47-
USAGE_LEDGER_STATEMENT_TIMEOUT_MS,
4848
} from '@/lib/billing/core/usage-log'
4949
import { asOrchestrationError } from '@/lib/core/orchestration/types'
5050
import { HttpError } from '@/lib/core/utils/http-error'

‎apps/sim/lib/billing/core/usage-log.ts‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
textKey,
1616
timestampKey,
1717
} from '@/lib/api/list-query'
18+
import { USAGE_LEDGER_STATEMENT_TIMEOUT_MS } from '@/lib/billing/constants'
1819
import { defaultBillingPeriod } from '@/lib/billing/core/billing-period'
1920
import { getHighestPrioritySubscription } from '@/lib/billing/core/plan'
2021
import {
@@ -212,15 +213,6 @@ async function resolveBillingContext(
212213
}
213214
}
214215

215-
/**
216-
* Bound on one ledger sum. A large payer's period covers millions of rows, and from a cold
217-
* cache or under heavy I/O the sum can run for tens of seconds; past this the database ends it
218-
* and the read fails, so a caller that admits on the sum fails closed rather than waiting
219-
* without limit. The usage gate derives its coalescing deadline from this bound, so the sum
220-
* always ends at the database before the gate gives up on it.
221-
*/
222-
export const USAGE_LEDGER_STATEMENT_TIMEOUT_MS = 60_000
223-
224216
/**
225217
* Returns attributed ledger usage for a billing entity/period. The ledger is
226218
* the sole source of truth for usage — there is no userStats baseline.

0 commit comments

Comments
 (0)