2023-12-18 11:21:21 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "resolv"
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module Utils
|
|
|
|
module DnsSrv
|
|
|
|
def self.lookup(domain)
|
|
|
|
Discourse
|
|
|
|
.cache
|
|
|
|
.fetch("dns_srv_lookup:#{domain}", expires_in: 5.minutes) do
|
|
|
|
resources = dns_srv_lookup_for_domain(domain)
|
|
|
|
|
2024-04-12 09:39:19 -04:00
|
|
|
server_election(resources)
|
2023-12-18 11:21:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def self.dns_srv_lookup_for_domain(domain)
|
|
|
|
resolver = Resolv::DNS.new
|
|
|
|
resources = resolver.getresources(domain, Resolv::DNS::Resource::IN::SRV)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.select_server(resources)
|
|
|
|
priority = resources.group_by(&:priority).keys.min
|
|
|
|
|
|
|
|
priority_resources = resources.select { |r| r.priority == priority }
|
|
|
|
|
|
|
|
total_weight = priority_resources.map(&:weight).sum
|
|
|
|
|
|
|
|
random_weight = rand(total_weight)
|
|
|
|
|
|
|
|
priority_resources.each do |resource|
|
|
|
|
random_weight -= resource.weight
|
|
|
|
|
|
|
|
return resource if random_weight < 0
|
|
|
|
end
|
2024-04-12 09:39:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.server_available?(server)
|
|
|
|
begin
|
|
|
|
conn = Faraday.new { |f| f.adapter FinalDestination::FaradayAdapter }
|
|
|
|
conn.head("https://#{server.target}:#{server.port}")
|
|
|
|
true
|
|
|
|
rescue StandardError
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
2023-12-18 11:21:21 -05:00
|
|
|
|
2024-04-12 09:39:19 -04:00
|
|
|
def self.server_election(resources)
|
|
|
|
return nil if resources.empty?
|
|
|
|
return resources.first if resources.length == 1
|
|
|
|
|
|
|
|
candidate = select_server(resources)
|
|
|
|
|
|
|
|
if server_available?(candidate)
|
|
|
|
candidate
|
|
|
|
else
|
|
|
|
server_election(resources - [candidate])
|
|
|
|
end
|
2023-12-18 11:21:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|