- protocol/booter: bits4 handshake, proc surfaces, boot policy v4, telemetry v4 - coreutils: nohup + ensure-man-pages; seed-man-pages fixes for extra pages - kernel-runner: no static node:module (Pear); bare-module createRequire - docs: handbook, reference, ADR, compatibility matrix, verify scripts
47 lines
1.4 KiB
JavaScript
47 lines
1.4 KiB
JavaScript
/** @type {Map<string, number>} */
|
|
const delegateBuckets = new Map()
|
|
|
|
/**
|
|
* @param {string} kind
|
|
* @param {Record<string, string | undefined> | null | undefined} env
|
|
*/
|
|
function delegateMaxPerMinute(kind, env) {
|
|
if (!env) return 0
|
|
const spec = env[`BARE_OS_DELEGATE_${String(kind).toUpperCase()}_MAX_PER_MIN`]
|
|
if (spec != null && String(spec).trim() !== '') {
|
|
const n = Number.parseInt(String(spec), 10)
|
|
if (Number.isFinite(n) && n > 0) return n
|
|
}
|
|
const g = env.BARE_OS_DELEGATE_MAX_PER_MIN
|
|
if (g != null && String(g).trim() !== '') {
|
|
const n = Number.parseInt(String(g), 10)
|
|
if (Number.isFinite(n) && n > 0) return n
|
|
}
|
|
return 0
|
|
}
|
|
|
|
/**
|
|
* @param {string} kind
|
|
* @param {Record<string, string | undefined> | null | undefined} env
|
|
* @returns {boolean}
|
|
*/
|
|
export function bareOsDelegateRateAllow(kind, env) {
|
|
const max = delegateMaxPerMinute(kind, env)
|
|
if (!max) return true
|
|
const minute = Math.floor(Date.now() / 60000)
|
|
const key = `${kind}:${minute}`
|
|
const cur = delegateBuckets.get(key) || 0
|
|
if (cur >= max) return false
|
|
delegateBuckets.set(key, cur + 1)
|
|
if (delegateBuckets.size > 128) {
|
|
const drop = [...delegateBuckets.keys()].slice(0, 64)
|
|
for (const k of drop) delegateBuckets.delete(k)
|
|
}
|
|
return true
|
|
}
|
|
|
|
/** Sample of per-minute counters (keys `kind:minuteEpoch`). */
|
|
export function bareOsDelegateRateBucketsSnapshot() {
|
|
return Object.fromEntries(delegateBuckets)
|
|
}
|