nats

package
v0.0.0-...-c7901e9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 5, 2026 License: MIT Imports: 6 Imported by: 0

README

nats

import "github.com/altessa-s/go-atlas/data/mongo/cursor_storages/nats"

Package nats implements cursor storage using NATS JetStream key-value store for MongoDB pagination state persistence.

Documentation

Overview

Package nats provides NATS JetStream KeyValue cursor storage for MongoDB pagination. Enables distributed cursor sharing across application instances with automatic expiration.

Example:

kv, _ := js.CreateKeyValue(ctx, jetstream.KeyValueConfig{
    Bucket: "cursors",
    TTL:    time.Hour,
})
storage := nats.New(kv)
result, _ := mongo.ListCursor(ctx, coll, mongo.WithListCursorStorage(storage))

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Storage

type Storage struct {
	*kvstore.JSONStorage
	natsbase.Base
}

Storage is a NATS JetStream KeyValue implementation of mongotools.CursorStorage. This implementation is suitable for production deployments with multiple instances where cursor sharing across instances is required.

Features:

  • Distributed: Cursors shared across all application instances
  • Automatic expiration: NATS KeyValue TTL handles cursor cleanup
  • Persistent: Survives application restarts (if JetStream is persistent)
  • Thread-safe: NATS handles concurrent access
  • Configurable bucket name: Allows multiple isolated cursor storages

Example usage:

nc, _ := nats.Connect(nats.DefaultURL)
js, _ := jetstream.New(nc)
kv, _ := js.CreateKeyValue(ctx, jetstream.KeyValueConfig{
    Bucket: "cursors",
    TTL:    1 * time.Hour,
})

storage := nats.New(kv)

result, err := mongotools.ListCursor(ctx, collection,
    mongotools.WithListCursorStorage(storage),
    mongotools.WithListCursorLimit(50),
)

func New

func New(kv jetstream.KeyValue) *Storage

New creates a new NATS JetStream KeyValue cursor storage.

The KeyValue bucket should be created by the caller with desired configuration (TTL, storage type, replicas, etc.).

Parameters:

  • kv: NATS JetStream KeyValue bucket for storing cursor metadata

Returns:

  • *Storage: Ready to use storage instance

Example:

nc, _ := nats.Connect(nats.DefaultURL)
js, _ := jetstream.New(nc)
kv, _ := js.CreateKeyValue(ctx, jetstream.KeyValueConfig{
    Bucket:  "cursors",
    TTL:     1 * time.Hour,
    Storage: jetstream.FileStorage,
})
storage := nats.New(kv)

func (*Storage) Delete

func (s *Storage) Delete(ctx context.Context, key string) error

Delete removes cursor metadata from NATS KeyValue. Returns nil if key doesn't exist (idempotent).

Thread-safe: NATS handles concurrent access.

func (*Storage) Ping

func (s *Storage) Ping(ctx context.Context) error

Ping checks if the NATS JetStream connection is alive. Useful for health checks and debugging.

Returns error if NATS is unreachable or unhealthy.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL