lock

package
v0.0.0-...-6ed4abe Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package lock NOTES

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FileLock

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

FileLock is used to protect the file operation, lock the file by path. !It is essentially a resource lock that cannot be shared between processes or between FileLock instances.

func NewFileLock

func NewFileLock() *FileLock

NewFileLock create a file lock

func (*FileLock) Acquire

func (flm *FileLock) Acquire(filePath string)

Acquire try get the file lock by file path

func (*FileLock) Release

func (flm *FileLock) Release(filePath string)

Release release the file lock by file path

func (*FileLock) TryAcquire

func (flm *FileLock) TryAcquire(filePath string) bool

TryAcquire try get the file lock by file path

type Interface

type Interface interface {
	// Acquire test if the caller can get the resource's lock.
	// if the caller got the lock, then it will return immediately with ture.
	// so that it can handle the cache job immediately. Otherwise, the spin
	// lock will wait until the lock is released by the one who has acquired
	// the lock.
	Acquire(resource string) *State

	// Release the resource lock so that the resource lock is released.
	// after the resource's lock is released, the other call which is still
	// waiting for the lock to be released will return immediately.
	Release(resource string, withLimit bool)
}

Interface defines all the supported operations for the resource's lock, which is used to manage the lock of different kind of resources.

func New

func New(opt Option) Interface

New initialize a resource lock instance.

type Option

type Option struct {
	// QPS should >=1
	QPS uint
	// Burst should >= 1, otherwise the limiter
	// can not work correctly.
	Burst uint
}

Option defines options to initial a resource lock.

type RedisLock

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

RedisLock is a distributed resource lock

func NewRedisLock

func NewRedisLock(bds bedis.Client, ttl uint) *RedisLock

NewRedisLock initialize a redis lock instance.

func (*RedisLock) Acquire

func (r *RedisLock) Acquire(res string)

Acquire caller try to get the resource's lock. if the caller got the lock, then it will return immediately with ture. Otherwise, the caller will wait until the lock is released by the one who has acquired the lock.

func (*RedisLock) Release

func (r *RedisLock) Release(res string)

Release the resource lock so that the resource lock is released. after the resource's lock is released, the other call which is still waiting for the lock to be released will return immediately.

func (*RedisLock) TryAcquire

func (r *RedisLock) TryAcquire(res string) bool

TryAcquire caller try to get the resource's lock. Whether or not the caller got the lock, it will return immediately.

type State

type State struct {
	// Acquired defines if the caller have acquired the lock,
	// which true means acquired the lock.
	Acquired bool
	// WithLimit means the caller does not acquire the lock and
	// the one who have acquired the lock request the other caller
	// to consume the resource with limit.
	WithLimit bool
	// contains filtered or unexported fields
}

State describe the state of the caller to acquire the lock.

func (*State) Release

func (s *State) Release(withLimit bool)

Release the resource lock so that the resource lock is released. after the resource's lock is released, the other call which is still waiting for the lock to be released will return immediately.

Jump to

Keyboard shortcuts

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