lock

package
Version: v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2021 License: MIT Imports: 6 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewDefaultLockFactory

func NewDefaultLockFactory() *build.Factory

Create a new instance of the factory. Returns *build.Factory

Types

type ILock

type ILock interface {
	// Makes a single attempt to acquire a lock by its key. It returns immediately a positive or negative result.
	TryAcquireLock(correlationId string, key string, ttl int64) (bool, error)

	// Makes multiple attempts to acquire a lock by its key within give time interval.
	AcquireLock(correlationId string, key string, ttl int64, timeout int64) error

	// Releases prevously acquired lock by its key.
	ReleaseLock(correlationId string, key string) error
}

Interface for locks to synchronize work or parallel processes and to prevent collisions.

The lock allows to manage multiple locks identified by unique keys.

type ILockOverrides added in v1.1.0

type ILockOverrides interface {
	ILock
}

type Lock

type Lock struct {
	Overrides ILockOverrides
	// contains filtered or unexported fields
}

Abstract lock that implements default lock acquisition routine.

Configuration parameters

options:
  retry_timeout: timeout in milliseconds to retry lock acquisition. (Default: 100)

func InheritLock

func InheritLock(overrides ILockOverrides) *Lock

Inherit lock fron ILock Returns *Lock

func (*Lock) AcquireLock

func (c *Lock) AcquireLock(correlationId string,
	key string, ttl int64, timeout int64) error

Makes multiple attempts to acquire a lock by its key within give time interval. Parameters:

  • correlationId string transaction id to trace execution through call chain.
  • key string a unique lock key to acquire. ttl int64 a lock timeout (time to live) in milliseconds. timeout int64 a lock acquisition timeout.

Returns error

func (*Lock) Configure

func (c *Lock) Configure(config *config.ConfigParams)

Configures component by passing configuration parameters. Parameters:

  • config *config.ConfigParams configuration parameters to be set.

type MemoryLock

type MemoryLock struct {
	Lock
	// contains filtered or unexported fields
}

Lock that is used to synchronize execution within one process using shared memory.

Remember: This implementation is not suitable for synchronization of distributed processes.

Configuration parameters

options:
  retry_timeout: timeout in milliseconds to retry lock acquisition. (Default: 100)

see ILock

see Lock

Example

lock := NewMemoryLock()
err = lock.Acquire("123", "key1")
    if err == nil {
        defer _ = lock.ReleaseLock("123", "key1")
        // Processing...
    }

func NewMemoryLock

func NewMemoryLock() *MemoryLock

Create new memory lock Returns *MemoryLock

func (*MemoryLock) ReleaseLock

func (c *MemoryLock) ReleaseLock(correlationId string,
	key string) error

Releases the lock with the given key. Parameters:

  • correlationId string not used.
  • key string the key of the lock that is to be released.

Return error

func (*MemoryLock) TryAcquireLock

func (c *MemoryLock) TryAcquireLock(correlationId string,
	key string, ttl int64) (bool, error)

Makes a single attempt to acquire a lock by its key. It returns immediately a positive or negative result. Parameters:

  • correlationId string transaction id to trace execution through call chain.
  • key string a unique lock key to acquire.
  • ttl int64 a lock timeout (time to live) in milliseconds.

Returns bool, error true if locked. Error object

type NullLock

type NullLock struct{}

Dummy lock implementation that doesn't do anything. It can be used in testing or in situations when lock is required but shall be disabled.

func NewNullLock

func NewNullLock() *NullLock

func (*NullLock) AcquireLock

func (c *NullLock) AcquireLock(correlationId string,
	key string, ttl int, timeout int) error

Makes multiple attempts to acquire a lock by its key within give time interval. Parameters:

  • correlationId string transaction id to trace execution through call chain.
  • key string a unique lock key to acquire. ttl int64 a lock timeout (time to live) in milliseconds. timeout int64 a lock acquisition timeout.

Returns error

func (*NullLock) ReleaseLock

func (c *NullLock) ReleaseLock(correlationId string,
	key string) error

Releases the lock with the given key. Parameters:

  • correlationId string not used.
  • key string the key of the lock that is to be released.

Return error

func (*NullLock) TryAcquireLock

func (c *NullLock) TryAcquireLock(correlationId string,
	key string, ttl int) (bool, error)

Makes a single attempt to acquire a lock by its key. It returns immediately a positive or negative result. Parameters:

  • correlationId string transaction id to trace execution through call chain.
  • key string a unique lock key to acquire.
  • ttl int64 a lock timeout (time to live) in milliseconds.

Returns bool, error true if locked. Error object

Jump to

Keyboard shortcuts

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