Commit a0e97b1c authored by Imre Farkas's avatar Imre Farkas

Optimize Redis usage in User::ActivityService

Avoid checking Redis when User#last_activity_on is already up to date.
parent c8fe0d6a
...@@ -26,12 +26,15 @@ module Users ...@@ -26,12 +26,15 @@ module Users
def record_activity def record_activity
return if Gitlab::Database.read_only? return if Gitlab::Database.read_only?
today = Date.today
return if @user.last_activity_on == today
lease = Gitlab::ExclusiveLease.new("acitvity_service:#{@user.id}", lease = Gitlab::ExclusiveLease.new("acitvity_service:#{@user.id}",
timeout: LEASE_TIMEOUT) timeout: LEASE_TIMEOUT)
return unless lease.try_obtain return unless lease.try_obtain
@user.update_attribute(:last_activity_on, Date.today) @user.update_attribute(:last_activity_on, today)
Rails.logger.debug("Recorded activity: #{@activity} for User ID: #{@user.id} (username: #{@user.username})")
end end
end end
end end
---
title: Optimize Redis usage in User::ActivityService
merge_request: 25005
author:
type: performance
...@@ -26,6 +26,12 @@ describe Users::ActivityService do ...@@ -26,6 +26,12 @@ describe Users::ActivityService do
.from(last_activity_on) .from(last_activity_on)
.to(Date.today) .to(Date.today)
end end
it 'tries to obtain ExclusiveLease' do
expect(Gitlab::ExclusiveLease).to receive(:new).and_call_original
subject.execute
end
end end
context 'when a bad object is passed' do context 'when a bad object is passed' do
...@@ -46,6 +52,12 @@ describe Users::ActivityService do ...@@ -46,6 +52,12 @@ describe Users::ActivityService do
it 'does not update last_activity_on' do it 'does not update last_activity_on' do
expect { subject.execute }.not_to change(user, :last_activity_on) expect { subject.execute }.not_to change(user, :last_activity_on)
end end
it 'does not try to obtain ExclusiveLease' do
expect(Gitlab::ExclusiveLease).not_to receive(:new)
subject.execute
end
end end
context 'when in GitLab read-only instance' do context 'when in GitLab read-only instance' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment