plugins

package
v1.7.1 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package extension provides application-level patterns built on Grove KV.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AtomicCounter

type AtomicCounter struct {
	// contains filtered or unexported fields
}

AtomicCounter provides a distributed atomic counter backed by a KV store. Unlike the CRDT counter, this requires a consistent store (no eventual consistency).

func NewAtomicCounter

func NewAtomicCounter(store *kv.Store, key string) *AtomicCounter

NewAtomicCounter creates a new atomic counter at the given key.

func (*AtomicCounter) Decrement

func (c *AtomicCounter) Decrement(ctx context.Context, delta int64) (int64, error)

Decrement subtracts delta from the counter and returns the new value.

func (*AtomicCounter) Get

func (c *AtomicCounter) Get(ctx context.Context) (int64, error)

Get returns the current counter value.

func (*AtomicCounter) Increment

func (c *AtomicCounter) Increment(ctx context.Context, delta int64) (int64, error)

Increment adds delta to the counter and returns the new value.

func (*AtomicCounter) Reset

func (c *AtomicCounter) Reset(ctx context.Context) error

Reset sets the counter to zero.

func (*AtomicCounter) Set

func (c *AtomicCounter) Set(ctx context.Context, value int64) error

Set sets the counter to a specific value.

type Job

type Job struct {
	ID         string    `json:"id"`
	Payload    any       `json:"payload"`
	EnqueuedAt time.Time `json:"enqueued_at"`
}

Job represents a job in the queue.

type Leaderboard

type Leaderboard struct {
	// contains filtered or unexported fields
}

Leaderboard provides a sorted leaderboard backed by a KV store. For Redis, this could be backed by ZSET natively via Unwrap. This generic implementation stores scores in a map.

func NewLeaderboard

func NewLeaderboard(store *kv.Store, key string) *Leaderboard

NewLeaderboard creates a new leaderboard at the given key.

func (*Leaderboard) Add

func (lb *Leaderboard) Add(ctx context.Context, member string, score float64) error

Add adds or updates a member's score.

func (*Leaderboard) Rank

func (lb *Leaderboard) Rank(ctx context.Context, member string) (int, error)

Rank returns the 1-based rank of a member (1 = highest score).

func (*Leaderboard) Remove

func (lb *Leaderboard) Remove(ctx context.Context, member string) error

Remove removes a member from the leaderboard.

func (*Leaderboard) Score

func (lb *Leaderboard) Score(ctx context.Context, member string) (float64, error)

Score returns the score for a member.

func (*Leaderboard) Size

func (lb *Leaderboard) Size(ctx context.Context) (int, error)

Size returns the number of members in the leaderboard.

func (*Leaderboard) TopN

func (lb *Leaderboard) TopN(ctx context.Context, n int) ([]LeaderboardEntry, error)

TopN returns the top N entries in descending order.

type LeaderboardEntry

type LeaderboardEntry struct {
	Member string  `json:"member"`
	Score  float64 `json:"score"`
}

LeaderboardEntry represents a single entry in the leaderboard.

type Lock

type Lock struct {
	// contains filtered or unexported fields
}

Lock is a distributed lock backed by a KV store. It uses SetNX for locking and DELETE for unlocking.

func NewLock

func NewLock(store *kv.Store, key string, ttl time.Duration) *Lock

NewLock creates a distributed lock with the given key and TTL.

func (*Lock) Acquire

func (l *Lock) Acquire(ctx context.Context) error

Acquire attempts to acquire the lock. Returns an error if the lock cannot be acquired.

func (*Lock) AcquireWithRetry

func (l *Lock) AcquireWithRetry(ctx context.Context, retryInterval time.Duration, maxRetries int) error

AcquireWithRetry attempts to acquire the lock with retries.

func (*Lock) Extend

func (l *Lock) Extend(ctx context.Context, ttl time.Duration) error

Extend extends the lock's TTL.

func (*Lock) Release

func (l *Lock) Release(ctx context.Context) error

Release releases the lock.

type Queue

type Queue struct {
	// contains filtered or unexported fields
}

Queue provides a simple distributed job queue backed by a KV store.

func NewQueue

func NewQueue(store *kv.Store, prefix string, opts ...QueueOption) *Queue

NewQueue creates a new job queue.

func (*Queue) Ack

func (q *Queue) Ack(ctx context.Context, jobID string) error

Ack acknowledges that a job has been processed and removes it.

func (*Queue) Dequeue

func (q *Queue) Dequeue(ctx context.Context) (*Job, error)

Dequeue retrieves and locks the next job from the queue.

func (*Queue) Enqueue

func (q *Queue) Enqueue(ctx context.Context, payload any) (string, error)

Enqueue adds a job to the queue.

func (*Queue) Size

func (q *Queue) Size(ctx context.Context) (int, error)

Size returns the number of pending jobs in the queue.

type QueueOption

type QueueOption func(*Queue)

QueueOption configures a Queue.

func WithVisibilityTimeout

func WithVisibilityTimeout(d time.Duration) QueueOption

WithVisibilityTimeout sets the visibility timeout for dequeued jobs.

type RateLimitResult

type RateLimitResult struct {
	Allowed   bool
	Remaining int
	ResetAt   time.Time
}

RateLimitResult contains the result of a rate limit check.

type RateLimiter

type RateLimiter struct {
	// contains filtered or unexported fields
}

RateLimiter provides distributed rate limiting backed by a KV store.

func NewRateLimiter

func NewRateLimiter(store *kv.Store, prefix string, rate int, window time.Duration) *RateLimiter

NewRateLimiter creates a new rate limiter that allows `rate` requests per `window`.

func (*RateLimiter) Allow

func (rl *RateLimiter) Allow(ctx context.Context, key string) (*RateLimitResult, error)

Allow checks if the request identified by key is within the rate limit. It uses a sliding window counter approach.

func (*RateLimiter) Reset

func (rl *RateLimiter) Reset(ctx context.Context, key string) error

Reset resets the rate limit for the given key.

type SessionOption

type SessionOption func(*SessionStore)

SessionOption configures a SessionStore.

func WithSessionPrefix

func WithSessionPrefix(prefix string) SessionOption

WithSessionPrefix sets the key prefix for sessions.

func WithSessionTTL

func WithSessionTTL(ttl time.Duration) SessionOption

WithSessionTTL sets the default session TTL.

type SessionStore

type SessionStore struct {
	// contains filtered or unexported fields
}

SessionStore provides HTTP session storage backed by a KV store.

func NewSessionStore

func NewSessionStore(store *kv.Store, opts ...SessionOption) *SessionStore

NewSessionStore creates a new session store.

func (*SessionStore) Create

func (ss *SessionStore) Create(ctx context.Context, data any) (string, error)

Create creates a new session with the given data and returns the session ID.

func (*SessionStore) Delete

func (ss *SessionStore) Delete(ctx context.Context, id string) error

Delete removes a session.

func (*SessionStore) Exists

func (ss *SessionStore) Exists(ctx context.Context, id string) (bool, error)

Exists checks if a session exists.

func (*SessionStore) Get

func (ss *SessionStore) Get(ctx context.Context, id string, dest any) error

Get retrieves session data by ID.

func (*SessionStore) Touch

func (ss *SessionStore) Touch(ctx context.Context, id string) error

Touch refreshes the session TTL without changing data.

func (*SessionStore) Update

func (ss *SessionStore) Update(ctx context.Context, id string, data any) error

Update replaces session data and resets the TTL.

Jump to

Keyboard shortcuts

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