FIX: allow changing default DNS query timeout of 2s via GlobalSetting (#20383)

The current default timeout is hardcoded to 2 seconds which is proving
too low for certain cases, and resulting in sporadic timeouts due to slow DNS queries.
This commit is contained in:
Leonardo Mosquera 2023-02-20 19:54:29 -03:00 committed by GitHub
parent 8c80d330fa
commit 509fee0f5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 1 deletions

View File

@ -368,3 +368,6 @@ redirect_avatar_requests = false
# Force the entire cluster into postgres readonly mode. Equivalent to running `Discourse.enable_pg_force_readonly_mode`
pg_force_readonly_mode = false
# default DNS query timeout for FinalDestination (used when not explicitely given programmatically)
dns_query_timeout_secs =

View File

@ -3,7 +3,8 @@
class FinalDestination::Resolver
@mutex = Mutex.new
def self.lookup(addr, timeout: nil)
timeout ||= 2
timeout ||= default_dns_query_timeout
@mutex.synchronize do
@result = nil
@ -36,6 +37,14 @@ class FinalDestination::Resolver
private
def self.default_dns_query_timeout
if gs = GlobalSetting.dns_query_timeout_secs.presence
Integer(gs)
else
2
end
end
def self.ensure_lookup_thread
return if @thread&.alive?

View File

@ -34,6 +34,11 @@ describe FinalDestination::Resolver do
expect(alive_thread_count).to eq(start_thread_count + 1)
end
it "reads default query timeout from configuration" do
GlobalSetting.stubs(:dns_query_timeout_secs).returns(123)
expect(FinalDestination::Resolver.send(:default_dns_query_timeout)).to eq(123)
end
it "can lookup correctly" do
Addrinfo.stubs(:getaddrinfo).with { |addr| addr == "example.com" }.returns(mock_response)