summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDylan Griffith <dyl.griffith@gmail.com>2018-02-21 16:27:05 +1100
committerDylan Griffith <dyl.griffith@gmail.com>2018-02-21 16:27:05 +1100
commit2e216dd43bb02d741b2fcbfc1fe40042ad85a590 (patch)
tree061a3b26283b22f0aee7bc960a9f7ae1440f72ec
parent11bf575fe64e55cc932c5629e2d8d103109e0b2b (diff)
downloadgitlab-ce-query-counting-should-not-include-cached-queries.tar.gz
Do not count rails sql cache as queries in query limitingquery-counting-should-not-include-cached-queries
-rw-r--r--lib/gitlab/query_limiting/active_support_subscriber.rb6
-rw-r--r--spec/lib/gitlab/query_limiting/active_support_subscriber_spec.rb31
2 files changed, 27 insertions, 10 deletions
diff --git a/lib/gitlab/query_limiting/active_support_subscriber.rb b/lib/gitlab/query_limiting/active_support_subscriber.rb
index 66049c94ec6..4c83581c4b1 100644
--- a/lib/gitlab/query_limiting/active_support_subscriber.rb
+++ b/lib/gitlab/query_limiting/active_support_subscriber.rb
@@ -3,8 +3,10 @@ module Gitlab
class ActiveSupportSubscriber < ActiveSupport::Subscriber
attach_to :active_record
- def sql(*)
- Transaction.current&.increment
+ def sql(event)
+ unless event.payload[:name] == 'CACHE'
+ Transaction.current&.increment
+ end
end
end
end
diff --git a/spec/lib/gitlab/query_limiting/active_support_subscriber_spec.rb b/spec/lib/gitlab/query_limiting/active_support_subscriber_spec.rb
index b49bc5c328c..f8faeffb935 100644
--- a/spec/lib/gitlab/query_limiting/active_support_subscriber_spec.rb
+++ b/spec/lib/gitlab/query_limiting/active_support_subscriber_spec.rb
@@ -1,19 +1,34 @@
require 'spec_helper'
describe Gitlab::QueryLimiting::ActiveSupportSubscriber do
+ let(:transaction) { instance_double(Gitlab::QueryLimiting::Transaction, increment: true) }
+
+ before do
+ allow(Gitlab::QueryLimiting::Transaction)
+ .to receive(:current)
+ .and_return(transaction)
+ end
+
describe '#sql' do
it 'increments the number of executed SQL queries' do
- transaction = double(:transaction)
-
- allow(Gitlab::QueryLimiting::Transaction)
- .to receive(:current)
- .and_return(transaction)
+ User.count
expect(transaction)
- .to receive(:increment)
- .at_least(:once)
+ .to have_received(:increment)
+ .once
+ end
- User.count
+ context 'when the query is actually a rails cache hit' do
+ it 'does not increment the number of executed SQL queries' do
+ ActiveRecord::Base.connection.cache do
+ User.count
+ User.count
+ end
+
+ expect(transaction)
+ .to have_received(:increment)
+ .once
+ end
end
end
end