Documentation
¶
Overview ¶
Package jetstreamutil provides application-neutral helpers for NATS JetStream: key-value reads through the stream leader, retried resource provisioning, and consistent classification of OCC conflicts.
Index ¶
- func CreateJetStreamResourceWithRetry[T any](ctx context.Context, policy JetStreamResourceRetryPolicy, ...) (T, error)
- func IsSequenceConflict(err error) bool
- type JetStreamResourceRetryPolicy
- type KeyValue
- func (kv *KeyValue) DeleteAt(ctx context.Context, key string, revision uint64) (uint64, error)
- func (kv *KeyValue) Get(ctx context.Context, key string) (jetstream.KeyValueEntry, error)
- func (kv *KeyValue) GetAnyReplica(ctx context.Context, key string) (jetstream.KeyValueEntry, error)
- func (kv *KeyValue) GetRevision(ctx context.Context, key string, revision uint64) (jetstream.KeyValueEntry, error)
- func (kv *KeyValue) Latest(ctx context.Context, filter string) (jetstream.KeyValueEntry, error)
- func (kv *KeyValue) UpdateWithTTL(ctx context.Context, key string, value []byte, revision uint64, ...) (uint64, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CreateJetStreamResourceWithRetry ¶
func CreateJetStreamResourceWithRetry[T any](ctx context.Context, policy JetStreamResourceRetryPolicy, create func(context.Context) (T, error)) (T, error)
CreateJetStreamResourceWithRetry retries transient JetStream provisioning errors. create must honor ctx and be safe to repeat after a remotely successful operation whose response was lost. Use it for resource provisioning, not event publishing. Resource names, configuration, metadata, and request timeouts remain caller-owned. A request deadline is retryable only while the parent ctx remains active. Parent cancellation or expiry takes precedence over the callback result. Exhaustion returns the last operation error, and every failure returns the zero value of T.
func IsSequenceConflict ¶
IsSequenceConflict reports whether err is a JetStream optimistic-concurrency conflict caused by an expected-last-sequence mismatch. pkg/events keeps a private copy so that it does not depend on this module; change both together.
Types ¶
type JetStreamResourceRetryPolicy ¶
type JetStreamResourceRetryPolicy struct {
// MaxAttempts includes the initial call and must be positive.
MaxAttempts int
// RetryDelay is the initial exponential delay when MaxRetryDelay is positive.
// Otherwise it is multiplied by the failed attempt number. It must be
// non-negative and the largest linear delay must fit in time.Duration.
RetryDelay time.Duration
// MaxRetryDelay selects exponential backoff with equal jitter when positive.
// Each wait is between half and all of the exponential delay, capped here.
// Zero preserves linear backoff. It must be at least RetryDelay.
MaxRetryDelay time.Duration
// RequestTimeout bounds each callback when positive, independently of the
// overall startup context. Zero leaves request deadlines to the caller.
RequestTimeout time.Duration
}
JetStreamResourceRetryPolicy bounds retries of repeatable resource provisioning. Applications choose the retry budget; the framework does not own resource policy.
type KeyValue ¶
KeyValue is a jetstream.KeyValue whose Get, GetRevision, and Latest observe every committed write.
JetStream serves KeyValue.Get through DirectGet when the bucket allows it, which is the default for buckets that nats.go creates. Any replica can then answer, and a follower can lag behind a write that already committed: it returns an older revision or no entry. Read-your-writes, OCC updates, claims, and revocations then decide from stale state.
KeyValue routes Get, GetRevision, and Latest through the stream leader, and adds UpdateWithTTL, DeleteAt, and GetAnyReplica. Every other method is the bound bucket's, including watchers, key listings, and history. The bucket's Create also checks for a delete marker through DirectGet, so it can report jetstream.ErrKeyExists for a key that a lagging replica still shows. GetAnyReplica is the opt-in fast read for hot paths.
The bucket must be a plain bucket on the default JetStream API: not a mirror, not in another domain, and not reached through a subject transform or API prefix.
func NewKeyValue ¶
NewKeyValue binds leader-routed reads to bucket. It rejects a JetStream context with a domain or a non-default API prefix.
func (*KeyValue) DeleteAt ¶
DeleteAt deletes key if its latest revision is revision, and returns the revision of the delete marker. jetstream.KeyValue.Delete does not return it, but a caller needs it to wait until a watcher observes the deletion. It returns an error that matches jetstream.ErrKeyRevisionMismatch on a revision conflict.
func (*KeyValue) Get ¶
Get returns the latest committed entry for key through the stream leader. Like jetstream.KeyValue.Get, it returns jetstream.ErrKeyNotFound for a missing, deleted, purged, or expired key.
func (*KeyValue) GetAnyReplica ¶
GetAnyReplica is the bucket's own Get. When the bucket allows direct gets, any replica can answer, so the result can be an older revision, or a miss for an entry that a lagging replica has not applied yet. Use it on hot paths to accept a result quickly, and decide a negative result again with Get.
func (*KeyValue) GetRevision ¶
func (kv *KeyValue) GetRevision(ctx context.Context, key string, revision uint64) (jetstream.KeyValueEntry, error)
GetRevision returns the entry for key at revision through the stream leader. It returns jetstream.ErrKeyNotFound when that revision belongs to another key or removes this one. Revision 0 means the latest revision, as in nats.go.
func (*KeyValue) Latest ¶
Latest returns the newest entry whose key matches filter, read through the stream leader. filter can contain the wildcards `*` and `>`. Unlike Get, Latest also returns a delete, purge, or expiry marker; its Operation is jetstream.KeyValueDelete or jetstream.KeyValuePurge. It returns jetstream.ErrKeyNotFound only when no message matches.
func (*KeyValue) UpdateWithTTL ¶
func (kv *KeyValue) UpdateWithTTL(ctx context.Context, key string, value []byte, revision uint64, ttl time.Duration) (uint64, error)
UpdateWithTTL replaces key if its latest revision is revision, and sets a new per-message TTL. jetstream.KeyValue supports a TTL on Create only. The bucket must allow per-message TTLs. It returns the new revision, or an error that matches jetstream.ErrKeyRevisionMismatch on a revision conflict.