acquire method

  1. @override
Future<Lock?> acquire(
  1. String key, {
  2. Duration ttl = const Duration(seconds: 30),
  3. String? owner,
})
override

Attempts to acquire a lock for key, with ttl and optional owner.

Implementation

@override
Future<Lock?> acquire(
  String key, {
  Duration ttl = const Duration(seconds: 30),
  String? owner,
}) async {
  final now = stemNow();
  final existing = _locks[key];

  // Check if a non-expired lock already exists for this key.
  if (existing != null && !existing.isExpired(now)) {
    return null;
  }

  final resolvedOwner = owner ?? _InMemoryLock.generateOwner();
  final fencingToken = (_fencingTokens[key] ?? 0) + 1;
  _fencingTokens[key] = fencingToken;
  final lock = _InMemoryLock(
    key,
    resolvedOwner,
    fencingToken,
    now.add(ttl),
    this,
  );
  _locks[key] = lock;
  return lock;
}