dplock

package
v2.8.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2021 License: MIT Imports: 8 Imported by: 1

Documentation

Index

Constants

View Source
const PurgerPeriod = 5 * time.Minute

PurgerPeriod is the time period between expired lock purges

View Source
const TTL = 30

TTL is the 'time to live' for a lock in number of seconds

Variables

View Source
var AcquireMaxRetries = 10

AcquireMaxRetries is the maximum number of locking retries by the Acquire lock, discounting the first attempt

View Source
var AcquirePeriod = 250 * time.Millisecond

AcquirePeriod is the time period between acquire lock retries

View Source
var ErrAcquireMaxRetries = errors.New("cannot acquire lock, maximum number of retries has been reached")

ErrAcquireMaxRetries is an error returned when acquire fails after retrying to lock a resource 'AcquireMaxRetries' times

View Source
var ErrMongoDbClosing = errors.New("mongo db is being closed")

ErrMongoDbClosing is an error returned because MongoDB is being closed

View Source
var ErrUnlockMaxRetries = errors.New("cannot unlock, maximum number of retries has been reached")

ErrUnlockMaxRetries is an error logged when unlock fails after retrying to unlock a resource 'UnlockMaxRetries' times

View Source
var GenerateTimeID = func() int {
	return time.Now().Nanosecond()
}

GenerateTimeID returns the current timestamp in nanoseconds

View Source
var UnlockMaxRetries = 100

UnlockMaxRetries is the maximum number of unlocking retries by the Unlock lock, discounting the first attempt

View Source
var UnlockPeriod = 5 * time.Millisecond

UnlockPeriod is the time period between Unlock lock retries

Functions

This section is empty.

Types

type Client

type Client interface {
	XLock(ctx context.Context, resourceName, lockID string, ld lock.LockDetails) error
	Unlock(ctx context.Context, lockID string) ([]lock.LockStatus, error)
}

Client defines the lock Client methods from mongo-lock

type Lock

type Lock struct {
	Client        Client
	CloserChannel chan struct{}
	Purger        Purger
	WaitGroup     *sync.WaitGroup
	Resource      string
}

Lock is a MongoDB lock for a resource

func New

func New(ctx context.Context, mongoConnection *mongoDriver.MongoConnection, resource string) *Lock

New creates a new mongoDB lock for the provided session, db, collection and resource

func (*Lock) Acquire

func (l *Lock) Acquire(ctx context.Context, id string) (lockID string, err error)

Acquire tries to lock the provided id. If the resource is already locked, this function will block until the existing lock is released, at which point we acquire the lock and return.

func (*Lock) Close

func (l *Lock) Close(ctx context.Context)

Close closes the closer channel, and waits for the WaitGroup to finish.

func (*Lock) Init

func (l *Lock) Init(ctx context.Context, lockClient Client, lockPurger Purger)

Init initialises a lock with the provided client and purger, and starts the purger loop

func (*Lock) Lock

func (l *Lock) Lock(ctx context.Context, resourceID string) (lockID string, err error)

Lock acquires an exclusive mongoDB lock with the provided id, with the default TTL value. If the resource is already locked, an error will be returned.

func (*Lock) Unlock

func (l *Lock) Unlock(ctx context.Context, lockID string)

Unlock releases an exclusive mongoDB lock for the provided id (if it exists)

type Purger

type Purger interface {
	Purge(ctx context.Context) ([]lock.LockStatus, error)
}

Purger defines the lock Purger methods from mongo-lock

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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