natslock

package
v0.0.0-...-110cbc6 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package natslock provides locking and leader election using NATS JetStream KV store

Index

Constants

This section is empty.

Variables

View Source
var ErrBadParameter = errors.New("bad parameters in request")

ErrBadParameter is returned when bad parameters are passed to a request

Functions

func NewKeyValue

func NewKeyValue(jets nats.JetStreamContext, name string, ttl time.Duration) (nats.KeyValue, error)

NewKeyValue returns a JetStream key-value store with the given name. If the bucket does not exist, it will be created with the given TTL.

Types

type Locker

type Locker struct {
	KVStore nats.KeyValue
	KVKey   string
	Logger  *zap.Logger
}

Locker is a distributed lock backed by a JetStream key-value store

func New

func New(opts ...Option) *Locker

New returns a new locker

func (*Locker) AcquireLead

func (l *Locker) AcquireLead(id uuid.UUID) (bool, error)

AcquireLead attempts to acquire the leader lock for the given id and returns true if successful. If the lock is already held by another id, it will return false.

func (*Locker) Name

func (l *Locker) Name() string

Name returns the name of the locker kv store

func (*Locker) ReleaseLead

func (l *Locker) ReleaseLead(id uuid.UUID) error

ReleaseLead releases the leader lock if the given id belongs to the current leader

func (*Locker) TTL

func (l *Locker) TTL() time.Duration

TTL returns the ttl of the locker kv store

type Option

type Option func(l *Locker)

Option is a functional configuration option

func WithKeyValueStore

func WithKeyValueStore(kv nats.KeyValue) Option

WithKeyValueStore sets the nats key value store

func WithLogger

func WithLogger(log *zap.Logger) Option

WithLogger sets logger

Jump to

Keyboard shortcuts

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