diff options
author | Lin Jen-Shin <godfat@godfat.org> | 2019-02-14 17:07:24 +0300 |
---|---|---|
committer | Lin Jen-Shin <godfat@godfat.org> | 2019-02-14 17:07:24 +0300 |
commit | 1fc89ca9452c814d73245ec205d8e846d8760439 (patch) | |
tree | f2a2c898bc73ae0aa3fb858e054c40696b8333ac | |
parent | ed7144ad58c32936c902d93da23eeb2159ee0158 (diff) | |
parent | a0e97b1c6a786462733a73b681cff60b103d729c (diff) |
Merge branch 'if-24642-activity_service_optimization' into 'master'
Optimize Redis usage in User::ActivityService
See merge request gitlab-org/gitlab-ce!25005
-rw-r--r-- | app/services/users/activity_service.rb | 7 | ||||
-rw-r--r-- | changelogs/unreleased/24642-activity_service_optimization.yml | 5 | ||||
-rw-r--r-- | spec/services/users/activity_service_spec.rb | 12 |
3 files changed, 22 insertions, 2 deletions
diff --git a/app/services/users/activity_service.rb b/app/services/users/activity_service.rb index db03ba8756f..e50840a9158 100644 --- a/app/services/users/activity_service.rb +++ b/app/services/users/activity_service.rb @@ -26,12 +26,15 @@ module Users def record_activity return if Gitlab::Database.read_only? + today = Date.today + + return if @user.last_activity_on == today + lease = Gitlab::ExclusiveLease.new("acitvity_service:#{@user.id}", timeout: LEASE_TIMEOUT) return unless lease.try_obtain - @user.update_attribute(:last_activity_on, Date.today) - Rails.logger.debug("Recorded activity: #{@activity} for User ID: #{@user.id} (username: #{@user.username})") + @user.update_attribute(:last_activity_on, today) end end end diff --git a/changelogs/unreleased/24642-activity_service_optimization.yml b/changelogs/unreleased/24642-activity_service_optimization.yml new file mode 100644 index 00000000000..bdfa769959e --- /dev/null +++ b/changelogs/unreleased/24642-activity_service_optimization.yml @@ -0,0 +1,5 @@ +--- +title: Optimize Redis usage in User::ActivityService +merge_request: 25005 +author: +type: performance diff --git a/spec/services/users/activity_service_spec.rb b/spec/services/users/activity_service_spec.rb index 719b4adf212..3c0a4ac8e18 100644 --- a/spec/services/users/activity_service_spec.rb +++ b/spec/services/users/activity_service_spec.rb @@ -26,6 +26,12 @@ describe Users::ActivityService do .from(last_activity_on) .to(Date.today) end + + it 'tries to obtain ExclusiveLease' do + expect(Gitlab::ExclusiveLease).to receive(:new).and_call_original + + subject.execute + end end context 'when a bad object is passed' do @@ -46,6 +52,12 @@ describe Users::ActivityService do it 'does not update last_activity_on' do expect { subject.execute }.not_to change(user, :last_activity_on) end + + it 'does not try to obtain ExclusiveLease' do + expect(Gitlab::ExclusiveLease).not_to receive(:new) + + subject.execute + end end context 'when in GitLab read-only instance' do |