-
Notifications
You must be signed in to change notification settings - Fork 5
✨feat: Added new method TryGetAcquireLocks and override for ReleaseAppLock method to work with lists. #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…pLock method to work with lists.
✨feat: Added new method TryGetAcquireLocks and override for ReleaseAppLock method to work with lists.
| return allReleased; | ||
| } | ||
|
|
||
| public static async Task<bool> TryGetAcquireLocks(this SqlConnection connection, IEnumerable<int> keys, int retryTimeout = 100, int numberOfRetries = 3, IDbTransaction transaction = null, TimeSpan? lockTimeout = null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To keep the name aligned with existing GetAppLockAsync, this should be TryGetAppLockAsync
|
|
||
| public static async Task<bool> TryGetAcquireLocks(this SqlConnection connection, IEnumerable<int> keys, int retryTimeout = 100, int numberOfRetries = 3, IDbTransaction transaction = null, TimeSpan? lockTimeout = null) | ||
| { | ||
| if (keys == null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should also be a check if there is 0 keys in the IEnumerable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check added.
|
|
||
| foreach (var key in keys) | ||
| { | ||
| var lockName = $"Sql_lock_{key}"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't modify the name, the caller should pass in unique names
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
| { | ||
| var lockName = $"Sql_lock_{key}"; | ||
| var locked = await connection.GetAppLockAsync(lockName, transaction, lockTimeout); | ||
| if (locked) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd write this differently so it's more clear what the expected flow is.
if (!locked)
{
allLocked = false;
break;
}
acquiredLocks.Add(lockName);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can achieve the intent of the code without using a list of acquiredLocks. Looking at a scenario where in vast majority of cases locks will go through on the first go creating and discarding a list of names of acquired locks is a bit of waste.
You only need to track the index of the last created lock and then to ReleaseAppLockAsync you can pass something like this
connection.ReleaseAppLockAsync(keys.Take(index+1), transaction);
This way you take only one int in most scenarios, and in those scenarios where you do need to release them you then spend some extra computing, but that should be rare.
| } | ||
| } | ||
|
|
||
| if (!allLocked) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd also rewrite this
if (allLocked)
break;
await connection.ReleaseAppLockAsync(....);
await Task.Delay(retryTimeout);
| } | ||
| } | ||
|
|
||
| return false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There here
return allLocked;
Added new method TryGetAcquireLocks and override for ReleaseAppLock method to work with lists.