Skip to content

Commit 08fbf39

Browse files
committed
Merge pull request #707 from estolfo/RUBY-1052-connect-timeout
RUBY-1052 Use connect_timeout as the timeout for a server monitor's socket
2 parents 5032dd5 + f68787e commit 08fbf39

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

lib/mongo/server/connectable.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module Connectable
2525
# @since 2.1.0
2626
SSL = 'ssl'.freeze
2727

28-
# The default time in seconds to timeout a connection attempt.
28+
# The default time in seconds to timeout an operation executed on a socket.
2929
#
3030
# @since 2.0.0
3131
TIMEOUT = 5.freeze
@@ -64,12 +64,12 @@ def connected?
6464
!!@socket && @socket.alive?
6565
end
6666

67-
# Get the connection timeout.
67+
# Get the timeout to execute an operation on a socket.
6868
#
69-
# @example Get the connection timeout.
69+
# @example Get the timeout to execute an operation on a socket.
7070
# connection.timeout
7171
#
72-
# @return [ Float ] The connection timeout in seconds.
72+
# @return [ Float ] The operation timeout in seconds.
7373
#
7474
# @since 2.0.0
7575
def timeout

lib/mongo/server/monitor/connection.rb

+17
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,23 @@ class Monitor
2222
class Connection
2323
include Connectable
2424

25+
# The default time in seconds to timeout a connection attempt.
26+
#
27+
# @since 2.1.2
28+
CONNECT_TIMEOUT = 10.freeze
29+
30+
# Get the connection timeout.
31+
#
32+
# @example Get the connection timeout.
33+
# connection.timeout
34+
#
35+
# @return [ Float ] The connection timeout in seconds.
36+
#
37+
# @since 2.0.0
38+
def timeout
39+
@timeout ||= options[:connect_timeout] || CONNECT_TIMEOUT
40+
end
41+
2542
# Tell the underlying socket to establish a connection to the host.
2643
#
2744
# @example Connect to the host.

spec/mongo/server/monitor_spec.rb

+23
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,27 @@
192192
expect(thread.stop?).to be(true)
193193
end
194194
end
195+
196+
describe '#connection' do
197+
198+
context 'when there is a connect_timeout option set' do
199+
200+
let(:connect_timeout) do
201+
1
202+
end
203+
204+
let(:monitor) do
205+
described_class.new(address, listeners, TEST_OPTIONS.merge(connect_timeout: connect_timeout))
206+
end
207+
208+
it 'sets the value as the timeout on the connection' do
209+
expect(monitor.connection.timeout).to eq(connect_timeout)
210+
end
211+
212+
it 'set the value as the timeout on the socket' do
213+
monitor.connection.connect!
214+
expect(monitor.connection.send(:socket).timeout).to eq(connect_timeout)
215+
end
216+
end
217+
end
195218
end

0 commit comments

Comments
 (0)