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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nats_counters-0.1.0.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nats_counters-0.1.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file nats_counters-0.1.0.tar.gz.

File metadata

  • Download URL: nats_counters-0.1.0.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using 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}

File hashes

Hashes for nats_counters-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f11e4e36c9f97ad62e20ae9c18d7ce1e2ab7b10aa97029e21e70f317b87ef14c
MD5 bbc6dec349e96a56142983c80fc39aae
BLAKE2b-256 5c08dd4abac8f54bf7c543c869393499c55402f3c5e449e1259b6d88ca928408

See more details on using hashes here.

File details

Details for the file nats_counters-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: nats_counters-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for nats_counters-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7152ca4fca12afee1daaa9a0d11242fd27738ee5cdfadcba0ad7cbbdf94d40a3
MD5 b80ea9e34619e84cc639b4f74c2bc282
BLAKE2b-256 8b907d4101fcc6118d631fa49f40bf0f3a51245c16eaefc661323267adcfa702

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page