dlocker

package module
v0.0.6-beta Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2022 License: Apache-2.0 Imports: 5 Imported by: 3

README

Distributed locker

Embed distributed locker (distributed mutex). You need to implement persistent lock storage in order to use it (or use the provided mongodb storage provider). With Extender you can extend lock several or infinity times.

For critical data updates (during lock), it is recommended to additionally use data versioning.

For more details see examples.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LockContext

type LockContext interface {
	context.Context

	GetLock() models.Lock
	GetLockRecord() models.LockRecord
}

Lock context

type LockContextImp

type LockContextImp struct {
	context.Context
	// contains filtered or unexported fields
}

func NewLockContext

func NewLockContext(
	ctx context.Context,
	lock models.Lock,
	lr models.LockRecord,
) LockContextImp

func (LockContextImp) GetLock

func (ctx LockContextImp) GetLock() models.Lock

func (LockContextImp) GetLockRecord

func (ctx LockContextImp) GetLockRecord() models.LockRecord

type LockExtender

type LockExtender interface {
	LockWithWait(ctx context.Context) (exCtx context.Context, err error)
	Unlock(ctx context.Context, wait bool)
}

Extends existing lock

type LockExtenderImp

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

func NewLockExtender

func NewLockExtender(
	locker Locker,
	lock models.Lock,
	count int,
) *LockExtenderImp

func (*LockExtenderImp) LockWithWait

func (ex *LockExtenderImp) LockWithWait(
	ctx context.Context,
) (exCtx context.Context, err error)

func (*LockExtenderImp) Unlock

func (ex *LockExtenderImp) Unlock(ctx context.Context, wait bool)

type Locker

type Locker interface {
	// Set a lock (and wait for previous) or return ErrNoLuck if no luck or an unexpected error occurs
	LockWithWait(
		ctx context.Context,
		lock models.Lock,
	) (lockCtx LockContext, cancel context.CancelFunc, err error)

	// Extend existing lock
	ExtendLock(
		ctx context.Context,
		lockCtx LockContext,
	) (newLockCtx LockContext, cancel context.CancelFunc, err error)

	// Release lock or return ErrNoLuck if no luck or an unexpected error occurs
	Unlock(
		ctx context.Context,
		lockCtx LockContext,
	) (err error)
}

Manages locks

type LockerImp

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

func NewLocker

func NewLocker(
	sp StorageProvider,
) *LockerImp

func (*LockerImp) ExtendLock

func (s *LockerImp) ExtendLock(
	ctx context.Context,
	lockCtx LockContext,
) (newLockCtx LockContext, cancel context.CancelFunc, err error)

func (*LockerImp) LockWithWait

func (s *LockerImp) LockWithWait(
	ctx context.Context,
	lock models.Lock,
) (lockCtx LockContext, cancel context.CancelFunc, err error)

Set a lock (and wait for previous) or return ErrNoLuck if no luck or an unexpected error occurs

func (*LockerImp) Unlock

func (s *LockerImp) Unlock(
	ctx context.Context,
	lockCtx LockContext,
) (err error)

type StorageProvider

type StorageProvider interface {
	// Returns LockRecord or error ErrNotFound if not found or unexpected error.
	GetLockRecord(
		ctx context.Context,
		jobName string,
	) (lr models.LockRecord, err error)

	// Creates LockRecord or returns error ErrDuplicate if already exists or unexpected error.
	CreateLockRecord(
		ctx context.Context,
		lr models.LockRecord,
	) (err error)

	// Updates LockRecord with new values or return error ErrNotFound if not found or unexpected error.
	// Returns ErrNotSupported in case storage does not support updates (in this case storage must handle ttl)
	UpdateLockRecord(
		ctx context.Context,
		jobName string,
		version string,
		patch models.LockRecordPatch,
	) (err error)
}

Creates lock records in a persistent storage.

Directories

Path Synopsis
examples
storageproviders
testsp
!!! For test use only
!!! For test use only

Jump to

Keyboard shortcuts

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