RedisLock

Distributed lock that is implemented based on the Redis in-memory database.

Extends: Lock

Implements: IReferenceable, IConfigurable, IOpenable

Description

The RedisLock class enables the creation of a distributed lock using Redis in-memory database, providing efficient synchronization and coordination among multiple processes or nodes in distributed systems, thereby enhancing performance and reliability

Configuration parameters

  • connection(s):
    • discovery_key: (optional) key to retrieve the connection from IDiscovery
    • host: host name or IP address
    • port: port number
    • uri: resource URI or connection string with all parameters in it
  • credential(s):
    • store_key: key to retrieve parameters from credential store
    • username: username (currently is not used)
    • password: user’s password
  • options:
    • retry_timeout: timeout in milliseconds to retry lock acquisition. (Default: 100)
    • retries: number of retries (default: 3)

References

  • *:discovery:*:*:1.0 - (optional) IDiscovery services
  • *:credential-store:*:*:1.0 - (optional) ICredentialStore to resolve credentials

Instance methods

close

Closes the component and frees used resources.

public close(context: IContext): Promise<void>

  • context: IContext - (optional) a context to trace execution through a call chain.

configure

Configures a component by passing its configuration parameters.

public configure(config: ConfigParams): void

  • config: ConfigParams - configuration parameters to be set.

isOpen

Checks if the component is open.

public isOpen(): boolean

  • returns: boolean - true if the component is open and false otherwise.

open

Opens the component.

public open(context: IContext): Promise<void>

  • context: IContext - (optional) a context to trace execution through a call chain.

releaseLock

Releases a prevously acquired lock by its key.

public releaseLock(context: IContext, key: string): Promise<void>

  • context: IContext - (optional) a context to trace execution through a call chain.
  • key: string - unique lock key to release.

setReferences

Sets references to dependent components.

public setReferences(references: IReferences): void

  • references: IReferences - references to locate the component dependencies.

tryAcquireLock

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

public tryAcquireLock(context: IContext, key: string, ttl: number): Promise<boolean>

  • context: IContext - (optional) a context to trace execution through a call chain.
  • key: string - unique lock key to acquire.
  • ttl: number - lock timeout (time to live) in milliseconds.
  • returns: Promise<boolean> - true if lock was successfully acquired and false otherwise.

Examples

let lock = new RedisRedis();
lock.configure(ConfigParams.fromTuples(
  "host", "localhost",
  "port", 6379
));
lock.open("123");
await lock.acquire("123", "key1");
try {
  // Processing...
} finally {
  await lock.releaseLock("123", "key1");
}