Skip to content

Add resource locking

Bishwa Hang Rai requested to merge 415584-add-on-assignment-with-locking into master

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

  1. Check out the branch
  2. Create a new root group namespace: "root-group"
  3. 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.

Related to #415584

Edited by Bishwa Hang Rai

Merge request reports