diff options
author | Toon Claes <toon@gitlab.com> | 2017-05-02 22:52:14 +0200 |
---|---|---|
committer | Toon Claes <toon@gitlab.com> | 2017-05-08 08:48:38 +0200 |
commit | 6a915d6f2d462a376d8cecc062dd58e520339b5e (patch) | |
tree | f1c27077f36a5505c24686d8ba7a8c83f49e9a36 /spec/models | |
parent | 8b9cd3c072768ca810d2b33009e35d93a05e417f (diff) | |
download | gitlab-ce-6a915d6f2d462a376d8cecc062dd58e520339b5e.tar.gz |
Limit `update_tracked_fields` to write to database once/hour
Every time a user logs in or out, the Trackable attributes are written to the
database. This is causing a lot of load on the database, for data that isn't
really critical.
So to avoid the database being hammered, add a Gitlab::ExclusiveLease before
writing trackable attributes to the database. This lease expires after an hour,
so only when the attributes were written more than an hour ago, they can be
written again. Otherwise they are ignored.
Diffstat (limited to 'spec/models')
-rw-r--r-- | spec/models/user_spec.rb | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 63e71f5ff2f..0b59916342e 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -344,6 +344,25 @@ describe User, models: true do end end + describe '#update_tracked_fields!', :redis do + let(:request) { OpenStruct.new(remote_ip: "127.0.0.1") } + let(:user) { create(:user) } + + it 'writes trackable attributes' do + expect do + user.update_tracked_fields!(request) + end.to change { user.reload.current_sign_in_at } + end + + it 'does not write trackable attributes when called a second time within the hour' do + user.update_tracked_fields!(request) + + expect do + user.update_tracked_fields!(request) + end.not_to change { user.current_sign_in_at } + end + end + shared_context 'user keys' do let(:user) { create(:user) } let!(:key) { create(:key, user: user) } |