Skip to main content

nodii-usage

Python port of @nodii/usage (S08-39, LIB-EVT-22): the STOP check, the meter and the one usage-metered payload. A producer never holds a price and never names a payer.

from nodii_usage import OtherUnit, UsageConfig, define_usage

usage = define_usage(
    UsageConfig(
        source_service="nodii-geo-service",
        topic_prefix="geo",
        outbox_table="geo_outbox",
        pool=pool,  # asyncpg.Pool
        slice_keys=("geo.geocode_lookup.google",),
        projection_ready=lambda: snapshot_drained,
    )
)

decision = await usage.check_stop(tenant_id, "geo.geocode_lookup.google")
if decision.status != "allowed":
    ...  # `unavailable` is a refusal too: retry later, or suppress a broadcast

async with conn.transaction():
    await usage.meter(conn, OtherUnit(...))
  • check_stop fails closed: any failure returns status="unavailable", it never raises.
  • apply_usage_denial is the one write path. SetUsageControl, the event and the snapshot all go through it, and the highest source version wins.
  • define_usage_denial_replica gives the replica spec; run get_usage_migration_sql() for the projection and marker tables.

keys_gen.py is generated; never edit it.

uv run pytest python/usage/tests/

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: get_usage_digest_repair_sql() 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.

Metadata

Release files for nodii-usage 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nodii-usage 0.3.0
File Size Uploaded Provenance Actions
nodii_usage-0.3.0.tar.gz 22.5 kB Without provenance Details for nodii_usage-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for nodii-usage 0.3.0
File Interpreter ABI Platform Actions
nodii_usage-0.3.0-py3-none-any.whl Python 3 none any Details for nodii_usage-0.3.0-py3-none-any.whl

Total release size: 38.1 kB

Release files / nodii_usage-0.3.0.tar.gz

Download URL nodii_usage-0.3.0.tar.gz
Size 22.5 kB
Tags Source
SHA-256 checksum
How to use checksums
43b9e60c5e4b834254c9efe853d6728b46d8cdaac7d43a1d7e8fd278b493f2c5
BLAKE2b-256 checksum
How to use checksums
de6cc0d5b9a716a392dec19b3d40ec0b4b272399b345290e7b7c306f50bd689b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Amazon Linux","version":"2023","id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / nodii_usage-0.3.0-py3-none-any.whl

Download URL nodii_usage-0.3.0-py3-none-any.whl
Size 15.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
85a82b356ecdae39ddf167f4ed46730f4f658d2e8d13e40ce47a000e6a728643
BLAKE2b-256 checksum
How to use checksums
b1c23267a49beec3eefd8ae56c65e0d8716bd8891fa7cd331ce5b29859eea9d4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Amazon Linux","version":"2023","id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for nodii-usage 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page