lock

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

This file contains the Lock struct definition and its methods. Lock objects use channels to lock / unlock which allows for implementing a timeout when waiting to acquire a lock.

This file contains the Manager and ManagedLock struct definitions and methods along with their helper functions.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidLockKey = errors.New("invalid lock key")
	ErrLockNotLocked  = errors.New("lock is not locked")
)
View Source
var (
	ErrLockDoesNotExist = errors.New("a lock by that name does not exist")
	ErrManagerShutdown  = errors.New("lock manager is shutdown")
	ErrLockSizeMismatch = errors.New("lock size mismatch")
	ErrInvalidLockSize  = errors.New("lock size must be greater than 0")
)

Functions

This section is empty.

Types

type Lock

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

func NewLock

func NewLock(ctx context.Context, n int32) *Lock

func (*Lock) Keys added in v1.1.0

func (l *Lock) Keys() []string

Keys returns the lock's keys

func (*Lock) Lock

func (l *Lock) Lock(key string, ctx context.Context) <-chan interface{}

Lock blocks until the lock is obtained or is canceled / timed out by context and returns a channel with the result. The interface returned will be either a struct{} (meaning the lock was acquired) or an error

func (*Lock) Size added in v1.1.0

func (l *Lock) Size() int32

Size returns the lock's size

func (*Lock) TryLock

func (l *Lock) TryLock(key string) (bool, error)

TryLock tries to obtain the lock and immediately fails or succeeds

func (*Lock) Unlock

func (l *Lock) Unlock(key string) (bool, error)

Unlock surprisingly unlocks the lock

type LockInfo

type LockInfo struct {
	Name         string
	Keys         string
	LastAccessed time.Time
	Locked       bool
	Size         int
}

LockInfo is a struct that contains only basic information about a lock

type ManagedLock

type ManagedLock struct {
	Name string

	*Lock
	// contains filtered or unexported fields
}

A ManagedLock is a lock with a some record keeping fields for garbage collection

func NewManagedLock

func NewManagedLock(name string, ctx context.Context, size int32) *ManagedLock

NewManagedLock returns a managed lock object with the given name

type Manager

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

A Manager polices access to a group of locks addressable by their names

func NewManager

func NewManager(shards uint32, gcInterval time.Duration, gcMinIdle time.Duration) (*Manager, func())

NewManager creates a new Manager with the given garbage collection interval, minimum idle time, and logger, and returns a pointer to the Manager.

Parameters:

  • ctx context.Context: the context for the Manager
  • shards uint32: the number of lock shards to use
  • gcInterval time.Duration: the interval for lock garbage collection
  • gcMinIdle time.Duration: the minimum time a lock must be idle before being considered for garbage collection

Returns: - *Manager: a pointer to the newly created Manager

func (*Manager) Lock

func (m *Manager) Lock(name string, key string, size int32, ctx context.Context) (<-chan interface{}, error)

Lock obtains a lock on the named lock. It blocks until a lock is obtained or is canceled or timed out by context

func (*Manager) Locks

func (m *Manager) Locks() []LockInfo

Locks returns a list of all locks

func (*Manager) TryLock

func (m *Manager) TryLock(name string, key string, size int32) (bool, error)

TryLock tries lock the named lock and immediately fails / succeeds

func (*Manager) Unlock

func (m *Manager) Unlock(name string, key string) (bool, error)

Unlock surprisingly unlocks the named lock

Jump to

Keyboard shortcuts

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