tryAcquireLock method

  1. @override
Future<bool> tryAcquireLock(
  1. String? correlationId,
  2. String key,
  3. int ttl

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

  • 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. Return Future that receives a lock result Throws error.


Future<bool> tryAcquireLock(
    String? correlationId, String key, int ttl) async {
  var expireTime = _locks[key];
  var now =;

  if (expireTime == null || expireTime < now) {
    _locks[key] = now + ttl;
    return true;
  } else {
    return false;