withLock<T> static method

Future<T> withLock<T>(
  1. String path,
  2. Future<T> action(), {
  3. required StaleLockPolicy staleWhen,
  4. Duration timeout = const Duration(minutes: 6),
  5. Duration pollInterval = const Duration(milliseconds: 50),
  6. Duration maxPollInterval = const Duration(seconds: 1),
  7. Duration? heartbeatInterval,
})

Acquires the lock at path, runs action, and releases it afterwards (even if action throws). See acquire for the parameters.

Implementation

static Future<T> withLock<T>(
  String path,
  Future<T> Function() action, {
  required StaleLockPolicy staleWhen,
  Duration timeout = const Duration(minutes: 6),
  Duration pollInterval = const Duration(milliseconds: 50),
  Duration maxPollInterval = const Duration(seconds: 1),
  Duration? heartbeatInterval,
}) async {
  final lock = await acquire(
    path,
    staleWhen: staleWhen,
    timeout: timeout,
    pollInterval: pollInterval,
    maxPollInterval: maxPollInterval,
    heartbeatInterval: heartbeatInterval,
  );
  try {
    return await action();
  } finally {
    await lock.release();
  }
}