Commit e35b85ae authored by Stan Hu's avatar Stan Hu

Refactor `cache_boolean` to `cache_value` for all Geo calls

parent 6ec3953e
......@@ -3,7 +3,7 @@ module Gitlab
class OauthApplicationUndefinedError < StandardError; end
def self.current_node
RequestStore.store[:geo_node_current] ||= begin
self.cache_value(:geo_node_current) do
GeoNode.find_by(host: Gitlab.config.gitlab.host,
port: Gitlab.config.gitlab.port,
relative_url_root: Gitlab.config.gitlab.relative_url_root)
......@@ -11,15 +11,15 @@ module Gitlab
end
def self.primary_node
RequestStore.store[:geo_primary_node] ||= GeoNode.find_by(primary: true)
self.cache_value(:geo_primary_node) { GeoNode.find_by(primary: true) }
end
def self.secondary_nodes
RequestStore.store[:geo_secondary_nodes] ||= GeoNode.where(primary: false)
self.cache_value(:geo_secondary_nodes) { GeoNode.where(primary: false) }
end
def self.enabled?
self.cache_boolean(:geo_node_enabled) { GeoNode.exists? }
self.cache_value(:geo_node_enabled) { GeoNode.exists? }
end
def self.license_allows?
......@@ -27,11 +27,11 @@ module Gitlab
end
def self.primary?
self.cache_boolean(:geo_node_primary) { self.enabled? && self.current_node && self.current_node.primary? }
self.cache_value(:geo_node_primary) { self.enabled? && self.current_node && self.current_node.primary? }
end
def self.secondary?
self.cache_boolean(:geo_node_secondary) { self.enabled? && self.current_node && !self.current_node.primary? }
self.cache_value(:geo_node_secondary) { self.enabled? && self.current_node && !self.current_node.primary? }
end
def self.geo_node?(host:, port:)
......@@ -49,17 +49,15 @@ module Gitlab
def self.oauth_authentication
return false unless Gitlab::Geo.secondary?
RequestStore.store[:geo_oauth_application] ||=
self.cache_value(:geo_oauth_application) do
Gitlab::Geo.current_node.oauth_application or raise OauthApplicationUndefinedError
end
end
def self.cache_boolean(key, &block)
def self.cache_value(key, &block)
return yield unless RequestStore.active?
val = RequestStore.store[key]
return val unless val.nil?
RequestStore[key] = yield
RequestStore.fetch(key) { yield }
end
end
end
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