lock

package
v1.2.4 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2021 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// LeaseFieldName is the name of the fields that specifies the lock lease duration for a lock
	LeaseFieldName = "dynoLockLeaseDuration"
	// VersionFieldName is the name of the fields that specifies the version of the record used for locking
	VersionFieldName = "dynoLockVersion"
	// ExpiresFieldName is the name of the fields that specifies when the lock will expire
	ExpiresFieldName = "dynoLockExpires"
)
View Source
const (
	//DefaultTimeout is the default timeout for the lock
	DefaultTimeout = time.Second
	//DefaultLeaseDuration is the default lock lease duration
	DefaultLeaseDuration = time.Millisecond * 500
	//DefaultHeartbeatFrequency is the default hertbean freq
	DefaultHeartbeatFrequency = time.Millisecond * 100
)

Variables

View Source
var (
	//ErrLockFailedToAcquire returned by Lock.Acquire if lock could not be acquired
	ErrLockFailedToAcquire = errors.New("lock failed to be acquired")
	//ErrLockFailedLeaseRenewal returned by Lock if lock lease could not be renewed
	ErrLockFailedLeaseRenewal = errors.New("lock failed to renew its lease")
	//ErrConditionalCheckFailedException returned when lock fails a conditional check
	ErrConditionalCheckFailedException = errors.New("lock failed on conditional check")
	// ErrLockTimeout returned when lock cannot be acquired due to a timeout
	ErrLockTimeout = errors.New("lock timeout")
)

Functions

This section is empty.

Types

type Lock

type Lock struct {
	DB                     *dyno.Session
	Key                    map[string]types.AttributeValue // the item that is being locked
	TableName              string
	SessionID              *string       // this lock sessionId
	HasLock                bool          // whether or not we have a lock
	LockTimeout            time.Duration // the lock timeout
	LockHeartbeatFrequency time.Duration // the lock heartbeat frequency. We will update the lock expiration with every heartbeat
	LeaseDuration          time.Duration // the lease duration or how much time we will lease the lock for. This must be greater than or equal to heartbeat freq.

	Context context.Context // context of the lock, should be released on cancel
	// contains filtered or unexported fields
}

Lock is a representation of a lock on a specific record in a table

func Acquire

func Acquire(tableName string, itemKey interface{}, db *dyno.Session, opts ...Opt) (lock *Lock, err error)

Acquire acquires a lock for a given table with a given map of key fields

func MustAcquire

func MustAcquire(tableName string, itemKey interface{}, sess *dyno.Session, opts ...Opt) *Lock

MustAcquire acquires a lock or panics

func (*Lock) Acquire

func (dl *Lock) Acquire() (err error)

Acquire the lock by attempting to update release the lock.

will wait for a given time duration to acquire the lock

func (*Lock) MustRelease

func (dl *Lock) MustRelease()

MustRelease attempts to release the lock and panics on error

func (*Lock) Release

func (dl *Lock) Release() error

Release the lock

func (*Lock) StartHeartbeat

func (dl *Lock) StartHeartbeat()

StartHeartbeat starts a go routine that will update the lease no the lock even “freq“

type Opt

type Opt func(*Opts)

Opt is an option

func OptContext

func OptContext(ctx context.Context) Opt

OptContext sets the lock's context

func OptHeartbeatFrequency

func OptHeartbeatFrequency(freq time.Duration) Opt

OptHeartbeatFrequency sets the heartbeat frequency for the lock

func OptLeaseDuration

func OptLeaseDuration(dur time.Duration) Opt

OptLeaseDuration sets the lease duration

func OptTimeout

func OptTimeout(timeout time.Duration) Opt

OptTimeout sets the timeout for the lock

type Opts

type Opts struct {
	Table              *dyno.Table `validate:"required"`
	Item               interface{} `validate:"required"`
	Timeout            time.Duration
	HeartbeatFrequency time.Duration
	LeaseDuration      time.Duration
	Context            context.Context
}

Opts is used as the input for the Acquire func Required:

Document: the record that will be locked
KeyValues: the key fields for the record that will be locked

Optional:

Timeout: the timeout as DurationNano for the lock
HeartbeatFreq: the freq of the heartbeat to renew the lock lease
LeaseDuration: the duration of the lease as a DurationNano

Jump to

Keyboard shortcuts

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