debian-mirror-gitlab/lib/gitlab/database/load_balancing/rack_middleware.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

89 lines
2.8 KiB
Ruby
Raw Normal View History

2021-09-04 01:27:46 +05:30
# frozen_string_literal: true
module Gitlab
module Database
module LoadBalancing
# Rack middleware to handle sticking when serving Rails requests. Grape
# API calls are handled separately as different API endpoints need to
# stick based on different objects.
class RackMiddleware
STICK_OBJECT = 'load_balancing.stick_object'
def initialize(app)
@app = app
end
def call(env)
# Ensure that any state that may have run before the first request
# doesn't linger around.
clear
unstick_or_continue_sticking(env)
result = @app.call(env)
2021-09-30 23:02:18 +05:30
ActiveSupport::Notifications.instrument('web_transaction_completed.load_balancing')
2021-09-04 01:27:46 +05:30
stick_if_necessary(env)
result
ensure
clear
end
# Determine if we need to stick based on currently available user data.
#
# Typically this code will only be reachable for Rails requests as
# Grape data is not yet available at this point.
def unstick_or_continue_sticking(env)
2021-11-18 22:05:49 +05:30
namespaces_and_ids = sticking_namespaces(env)
2021-09-04 01:27:46 +05:30
2021-12-11 22:18:48 +05:30
namespaces_and_ids.each do |(sticking, namespace, id)|
sticking.unstick_or_continue_sticking(namespace, id)
2021-09-04 01:27:46 +05:30
end
end
# Determine if we need to stick after handling a request.
def stick_if_necessary(env)
2021-11-18 22:05:49 +05:30
namespaces_and_ids = sticking_namespaces(env)
2021-09-04 01:27:46 +05:30
2021-12-11 22:18:48 +05:30
namespaces_and_ids.each do |sticking, namespace, id|
sticking.stick_if_necessary(namespace, id)
2021-09-04 01:27:46 +05:30
end
end
def clear
2021-11-18 22:05:49 +05:30
::Gitlab::Database::LoadBalancing.release_hosts
2021-10-27 15:23:28 +05:30
::Gitlab::Database::LoadBalancing::Session.clear_session
2021-09-04 01:27:46 +05:30
end
# Determines the sticking namespace and identifier based on the Rack
# environment.
#
# For Rails requests this uses warden, but Grape and others have to
# manually set the right environment variable.
2021-11-18 22:05:49 +05:30
def sticking_namespaces(env)
2021-09-04 01:27:46 +05:30
warden = env['warden']
if warden && warden.user
2021-11-18 22:05:49 +05:30
# When sticking per user, _only_ sticking the main connection could
# result in the application trying to read data from a different
# connection, while that data isn't available yet.
#
# To prevent this from happening, we scope sticking to all the
# models that support load balancing. In the future (if we
# determined this to be OK) we may be able to relax this.
::Gitlab::Database::LoadBalancing.base_models.map do |model|
2021-12-11 22:18:48 +05:30
[model.sticking, :user, warden.user.id]
2021-11-18 22:05:49 +05:30
end
2021-09-04 01:27:46 +05:30
elsif env[STICK_OBJECT].present?
env[STICK_OBJECT].to_a
else
[]
end
end
end
end
end
end