1# frozen_string_literal: true
2
3module HasUniqueInternalUsers
4  extend ActiveSupport::Concern
5
6  class_methods do
7    private
8
9    def unique_internal(scope, username, email_pattern, &block)
10      scope.first || create_unique_internal(scope, username, email_pattern, &block)
11    end
12
13    def create_unique_internal(scope, username, email_pattern, &creation_block)
14      # Since we only want a single one of these in an instance, we use an
15      # exclusive lease to ensure than this block is never run concurrently.
16      lease_key = "user:unique_internal:#{username}"
17      lease = Gitlab::ExclusiveLease.new(lease_key, timeout: 1.minute.to_i)
18
19      until uuid = lease.try_obtain
20        # Keep trying until we obtain the lease. To prevent hammering Redis too
21        # much we'll wait for a bit between retries.
22        sleep(1)
23      end
24
25      # Recheck if the user is already present. One might have been
26      # added between the time we last checked (first line of this method)
27      # and the time we acquired the lock.
28      existing_user = uncached { scope.first }
29      return existing_user if existing_user.present?
30
31      uniquify = Uniquify.new
32
33      username = uniquify.string(username) { |s| User.find_by_username(s) }
34
35      email = uniquify.string(-> (n) { Kernel.sprintf(email_pattern, n) }) do |s|
36        User.find_by_email(s)
37      end
38
39      user = scope.build(
40        username: username,
41        email: email,
42        &creation_block
43      )
44
45      Users::UpdateService.new(user, user: user).execute(validate: false)
46      user
47    ensure
48      Gitlab::ExclusiveLease.cancel(lease_key, uuid)
49    end
50  end
51end
52