@nodii/usage
v0.5.0
Published
STOP check, metering and one usage-metered event payload for every Nodii service that calls a paid provider (S08-39). Generated from usage-kinds.json. Polyglot ship: TS + Python + Go.
Readme
@nodii/usage
The STOP check, the meter and the one usage-metered payload (S08-39). A producer never holds a price and never names a payer.
const usage = defineUsage({ sourceService, topicPrefix, outboxTable, platformTenantId, pool, sliceKeys, projectionReady });
const stop = await usage.checkStop(tenantId, "geo.geocode_lookup.google");
if (stop.status !== "allowed") return refuse(stop);
await usage.meter(tx, {
usageKey: "geo.geocode_lookup.google",
tenantId,
provider: "google",
quantity: 1,
idempotencyId: lookupId,
occurredAt,
});checkStopreturnsallowed,stoppedorunavailable. Treatunavailableas a refusal: retry a transactional send later and suppress a broadcast (NOT-OPS-5).sliceKeyslists the keys this service spends on. Asking about any other key returnsunavailable.projectionReadymust be true only after the snapshot has drained. A tenant with no row was never denied, but only a drained projection can say so.applyUsageDenialis the one apply function. Pass theSetUsageControlRPC through it;defineUsageDenialReplicapasses the event and the snapshot through it. The highest source version wins (NOT-OPS-6).meternames the provider actually used (S08-20). A message unit takes its kind, sender and channel, and its payer and charge class follow from them. Any other unit is free only when it names a registered free rule (S08-19).platformTenantIdis required, anddefineUsagethrowsmissing_factunless it isPLATFORM_TENANT_ID(ENTA-37). A unit with no message object whosetenantIdis the Platform tenant meters with payerplatform, charge classfree:platform_ownand ₹0 (S08-2.1), whatever its key, even if it names a free rule. A unit with a message object keeps the payer and charge class from@nodii/notify(S08-2).checkStoptreats the Platform tenant like any other. No caller can name that class:platform_ownis not a registered free rule.- A free unit carries
amount_minor: 0. A billable unit carries no money: only tenant-service rates (S08-21).
Run getUsageMigrationSQL() in the service's migrations. Usage keys come from registries/usage-kinds.json, which holds keys and never prices (S08-41).
Known limit: widening the slice
The projection stores only the denials inside the service's slice, and one version of a tenant's denials is applied once. A service that adds a key to its slice therefore reads that key as allowed, for a tenant already denied on it, until tenant-service emits that tenant's next version. A redelivery or a snapshot of the same version does not rewrite the row. Because the digest covers only the slice (so the SetUsageControl RPC, which carries only the target's slice, and the event, which carries every denial, agree at one version), a same-version redelivery that the wider slice now sees more of is read as a payload mismatch and quarantined, and so is every later replay of that version; the row is unchanged, and the tenant's next version applies normally. The fix is an open question on STANDARDIZATIONS-AUDIT-19.
Upgrading from 0.2.0
Rows written before this release hold a digest of the whole message, while this release digests only the slice. Run the one-time repair once, as its own migration, after the rollout: getUsageDigestRepairSQL() returns an idempotent UPDATE that rewrites every row's digest from its stored denials (P-16). Without it, a tenant whose stored row came from a message with a denial outside the slice has every same-version replay quarantined as a payload mismatch until its next version; the stored denials stay correct either way. The repair rebuilds each digest from the row's stored denials, which are the slice the service used when the row was written. It assumes the slice has not changed since: if the service has narrowed its slice, a same-version replay still mismatches and is quarantined until the tenant's next version. Run it once the rollout has finished and no 0.2.0 consumer is still applying (a 0.2.0 pod that writes after the repair puts a whole-message digest back; the repair is idempotent, so re-running it after the rollout is safe). Run it as the table owner or nodii_owner, as migrations are: the projection has RLS, and a service role with no tenant set sees no rows, so the UPDATE would touch nothing. Check that the affected row count equals the table's row count.
