Files
accounted/lib/connect/instance/manual-sync-throttle.ts
T
admin e7f0070e84
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s
fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
2026-11-10 09:37:22 +01:00

31 lines
1.0 KiB
TypeScript

/**
* Cooldown for the operator-triggered connector sync (POST /api/connector/sync).
* Every run POSTs the instance's company count to the hosted entitlements
* endpoint, so a click storm in the settings panel must not turn into a
* request storm against the hosted service. Module-level state is enough:
* self-hosted runs as one long-lived process, and the hourly cron is
* unaffected (it never goes through this gate).
*/
export const MANUAL_SYNC_COOLDOWN_MS = 60_000
let inFlight = false
let lastStartedAt = 0
/** Claims the sync slot; false while a sync runs or the cooldown holds. */
export function tryBeginManualSync(now: number = Date.now()): boolean {
if (inFlight || now - lastStartedAt < MANUAL_SYNC_COOLDOWN_MS) return false
inFlight = true
lastStartedAt = now
return true
}
export function endManualSync(): void {
inFlight = false
}
/** Test hook: clears the in-flight flag and the cooldown window. */
export function resetManualSyncThrottle(): void {
inFlight = false
lastStartedAt = 0
}