Add resource locking
What does this MR do and why?
Add resource locking.
In order to handle race condition situation and not to let customer assign seats more than bought, we have added with_lock mechanism.
Screenshots or screen recordings
Screenshots are required for UI changes, and strongly recommended for all other merge requests.
n/a
How to set up and validate locally
- Check out the branch
- Create a new root group namespace: "root-group"
- Setup some seed records and test the race condition
namespace = Namespace.last
add_on = GitlabSubscriptions::AddOn.find_or_create_by!(name: "code_suggestions") {|e| e.description = "Test"}
add_on_purchase = GitlabSubscriptions::AddOnPurchase.create!(
add_on: add_on, namespace: namespace, expires_on: 1.month.from_now, quantity: 1, purchase_xid: 'A-S0001'
)
add_on_purchase.quantity # returns 1
add_on_purchase.assigned_users.count # returns 0
users = User.last(5) # get 5 users to add as group_member and assign on 1 available seats
users.map do |user|
namespace.add_developer(user)
Thread.new do
GitlabSubscriptions::UserAddOnAssignments::CreateService.new(
add_on_purchase: add_on_purchase,
user: user
).execute
end
end.each(&:join)
add_on_purchase.assigned_users.count # returns 1
MR acceptance checklist
This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.
-
I have evaluated the MR acceptance checklist for this MR.
Related to #415584
Edited by Bishwa Hang Rai