Class: Gitlab::QueryLimiting::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/query_limiting/middleware.rb

Overview

Middleware for reporting (or raising) when a request performs more than a certain amount of database queries.

Constant Summary collapse

CONTROLLER_KEY =
'action_controller.instance'
ENDPOINT_KEY =
'api.endpoint'

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.


11
12
13
# File 'lib/gitlab/query_limiting/middleware.rb', line 11

def initialize(app)
  @app = app
end

Instance Method Details

#action_name(env) ⇒ Object


26
27
28
29
30
31
32
# File 'lib/gitlab/query_limiting/middleware.rb', line 26

def action_name(env)
  if env[CONTROLLER_KEY]
    action_for_rails(env)
  elsif env[ENDPOINT_KEY]
    action_for_grape(env)
  end
end

#call(env) ⇒ Object


15
16
17
18
19
20
21
22
23
24
# File 'lib/gitlab/query_limiting/middleware.rb', line 15

def call(env)
  transaction, retval = ::Gitlab::QueryLimiting::Transaction.run do
    @app.call(env)
  end

  transaction.action = action_name(env)
  transaction.act_upon_results

  retval
end