Ruby-mongo-driver ping command does not return immediately when NoServerAvailable

Hello,

I noticed during failover, the ping command doesn’t return immediately as indicated by the documentation:

Typically, it will take 60s before it finally fails with NoServerAvailable: No primary server is available in cluster.

I was looking at the source code to determine if there was a way to set a timeout:
db.command(:ping => 1)

However I didn’t find anything. Any suggestions or guidance on how to force it to timeout sooner?

Thanks!

Hi @Joanne_Polsky,

The documentation for the ping command only indicates that the command will return immediately even if the server is write-locked. This is not the same thing as a server being unavailable; which is what would happen during a failover.

However I didn’t find anything. Any suggestions or guidance on how to force it to timeout sooner?

What I believe you’re looking to adjust is the server_selection_timeout, which defaults to 30s.

For example:

require 'bundler/inline'

gemfile do
  source 'https://rubygems.org'
  gem 'mongo'
end

client = Mongo::Client.new('mongodb://missing:27017/test', server_selection_timeout: 5)
client.command(ping: 1)