acquire method
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;
}