summaryrefslogtreecommitdiff
path: root/lib/gitlab/database/load_balancing/resolver.rb
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2023-01-18 19:00:14 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2023-01-18 19:00:14 +0000
commit05f0ebba3a2c8ddf39e436f412dc2ab5bf1353b2 (patch)
tree11d0f2a6ec31c7793c184106cedc2ded3d9a2cc5 /lib/gitlab/database/load_balancing/resolver.rb
parentec73467c23693d0db63a797d10194da9e72a74af (diff)
downloadgitlab-ce-05f0ebba3a2c8ddf39e436f412dc2ab5bf1353b2.tar.gz
Add latest changes from gitlab-org/gitlab@15-8-stable-eev15.8.0-rc42
Diffstat (limited to 'lib/gitlab/database/load_balancing/resolver.rb')
-rw-r--r--lib/gitlab/database/load_balancing/resolver.rb25
1 files changed, 22 insertions, 3 deletions
diff --git a/lib/gitlab/database/load_balancing/resolver.rb b/lib/gitlab/database/load_balancing/resolver.rb
index a291080cc3d..3e3998cae92 100644
--- a/lib/gitlab/database/load_balancing/resolver.rb
+++ b/lib/gitlab/database/load_balancing/resolver.rb
@@ -7,8 +7,21 @@ module Gitlab
module Database
module LoadBalancing
class Resolver
+ FAR_FUTURE_TTL = 100.years.from_now
+
UnresolvableNameserverError = Class.new(StandardError)
+ Response = Class.new do
+ attr_reader :address, :ttl
+
+ def initialize(address:, ttl:)
+ raise ArgumentError unless ttl.present? && address.present?
+
+ @address = address
+ @ttl = ttl
+ end
+ end
+
def initialize(nameserver)
@nameserver = nameserver
end
@@ -28,13 +41,14 @@ module Gitlab
private
def ip_address
- IPAddr.new(@nameserver)
+ # IP addresses are valid forever
+ Response.new(address: IPAddr.new(@nameserver), ttl: FAR_FUTURE_TTL)
rescue IPAddr::InvalidAddressError
end
def ip_address_from_hosts_file
ip = Resolv::Hosts.new.getaddress(@nameserver)
- IPAddr.new(ip)
+ Response.new(address: IPAddr.new(ip), ttl: FAR_FUTURE_TTL)
rescue Resolv::ResolvError
end
@@ -42,7 +56,12 @@ module Gitlab
answer = Net::DNS::Resolver.start(@nameserver, Net::DNS::A).answer
return if answer.empty?
- answer.first.address
+ raw_response = answer.first
+
+ # Defaults to 30 seconds if there is no TTL present
+ ttl_in_seconds = raw_response.ttl.presence || 30
+
+ Response.new(address: answer.first.address, ttl: ttl_in_seconds.seconds.from_now)
rescue Net::DNS::Resolver::NoResponseError
raise UnresolvableNameserverError, "no response from DNS server(s)"
end