dplock

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2020 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 GenerateTimeID = func() int {
	return time.Now().Nanosecond()
}

GenerateTimeID returns the current timestamp in nanoseconds

Functions

This section is empty.

Types

type Client

type Client interface {
	XLock(resourceName, lockID string, ld lock.LockDetails) error
	Unlock(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, session *mgo.Session, db, 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(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(lockID string) error

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

type Purger

type Purger interface {
	Purge() ([]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