Skip to main content

nats-counters

Distributed counters built on NATS JetStream streams.

A counter is a JetStream stream configured with allow_msg_counter (ADR-49, requires nats-server 2.12+). Each subject in the stream is an independent, arbitrary-precision counter, and increments are atomic.

Install

uv add nats-counters

Usage

from nats.client import connect
from nats.jetstream import new as jetstream
from nats import counters

nc = await connect("nats://localhost:4222")
js = jetstream(nc)

stream = await js.create_stream(
    name="COUNTERS",
    subjects=["events.>"],
    allow_msg_counter=True,   # enable counters
    allow_direct=True,        # required for reads
)
counter = counters.from_stream(js, stream)

# Increment / decrement; returns the new total.
await counter.add("events.orders", 1)    # -> 1
await counter.add("events.orders", 10)   # -> 11
await counter.add("events.orders", -1)   # -> 10

# Read the current value.
await counter.load("events.orders")      # -> 10

# Full entry with the last increment and source history.
entry = await counter.get("events.orders")
entry.value    # 10
entry.incr     # -1
entry.sources  # {source_id: {subject: value}} or None

Or wrap an existing stream by name:

counter = await counters.get_counter(js, "COUNTERS")

Status

Single-subject operations (add, load, get), batch and wildcard queries (get_multiple), and source tracking are implemented. Batch queries use the batch direct get support provided by nats-jetstream-extra.

License

Apache 2.0

Metadata

Release files for nats-counters 0.1.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 nats-counters 0.1.0
File Size Uploaded
nats_counters-0.1.0.tar.gz 4.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for nats-counters 0.1.0
File Interpreter ABI Platform
nats_counters-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 9.3 kB

Release files / nats_counters-0.1.0.tar.gz

Download URL nats_counters-0.1.0.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
f11e4e36c9f97ad62e20ae9c18d7ce1e2ab7b10aa97029e21e70f317b87ef14c
BLAKE2b-256 checksum
How to use checksums
5c08dd4abac8f54bf7c543c869393499c55402f3c5e449e1259b6d88ca928408
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / nats_counters-0.1.0-py3-none-any.whl

Download URL nats_counters-0.1.0-py3-none-any.whl
Size 5.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7152ca4fca12afee1daaa9a0d11242fd27738ee5cdfadcba0ad7cbbdf94d40a3
BLAKE2b-256 checksum
How to use checksums
8b907d4101fcc6118d631fa49f40bf0f3a51245c16eaefc661323267adcfa702
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

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