acquireLock method

  1. @override
Future acquireLock(
  1. String? correlationId,
  2. String key,
  3. int ttl,
  4. int timeout

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

  • correlationId (optional) transaction id to trace execution through call chain.
  • key a unique lock key to acquire.
  • ttl a lock timeout (time to live) in milliseconds.
  • timeout a lock acquisition timeout. Return Future that receives null for success. Throw error


Future acquireLock(
    String? correlationId, String key, int ttl, int timeout) async {
  var retryTime =
      .add(Duration(milliseconds: timeout))

  // Try to get lock first
  var result = await tryAcquireLock(correlationId, key, ttl);
  if (result) {
    return null;
  // Start retrying
  var now =;
  for (; now <= retryTime;) {
    await Future.delayed(Duration(milliseconds: _retryTimeout));
    result = await tryAcquireLock(correlationId, key, ttl);
    if (result) {
    now =;
  // When timeout expires throw exception
  throw ConflictException(correlationId, 'LOCK_TIMEOUT',
          'Acquiring lock ' + key + ' failed on timeout')
      .withDetails('key', key);