lock

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: Apache-2.0 Imports: 6 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")

Functions

This section is empty.

Types

type Lock

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

Lock represents a lock

func NewLock

func NewLock() *Lock

NewLock initializes and returns a new Lock instance.

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) 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(k string) (bool, error)

Unlock surprisingly unlocks the lock

type LockInfo

type LockInfo struct {
	Name         string
	Key          string // The lock key
	LastAccessed time.Time
	Locked       bool
}

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) *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(gcInterval time.Duration, gcMinIdle time.Duration) (m *Manager, closer 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: - 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(): a function that can be called to stop the Manager

func (*Manager) Lock

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

Lock obtains a lock on the named lock. It blocks until a lock is obtained or is canceled / 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) (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

Directories

Path Synopsis
This file contains the lock timer manager struct and methods.
This file contains the lock timer manager struct and methods.

Jump to

Keyboard shortcuts

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