Skip to content

Commit cf69bf4

Browse files
committed
RUBY-367 deprecate async refresh
1 parent fa10508 commit cf69bf4

File tree

3 files changed

+16
-44
lines changed

3 files changed

+16
-44
lines changed

docs/REPLICA_SETS.md

+4-8
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,15 @@ be used for reads.
4444
Refresh mode is disabled by default.
4545

4646
However, if you expect to make live changes to your secondaries, and you want this to be reflected without
47-
having to manually restart your app server, then you should enable it. You can enable synchronously, which will
48-
refresh the replica set data in a synchronous fashion (which may ocassionally slow down your queries):
47+
having to manually restart your app server, then you should enable it. You can enable this mode
48+
synchronously, which will refresh the replica set data in a synchronous fashion (which may
49+
ocassionally slow down your queries):
4950

5051
@connection = ReplSetConnection.new(['n1.mydb.net', 27017], :refresh_mode => :sync)
5152

52-
If you want to refresh via a background thread, use the `:async` mode. NOTE: the background
53-
version may be more effective on platforms that use native threads, such as JRuby:
54-
55-
@connection = ReplSetConnection.new(['n1.mydb.net', 27017], :refresh_mode => :async)
56-
5753
If you want to change the default refresh interval of 90 seconds, you can do so like this:
5854

59-
@connection = ReplSetConnection.new(['n1.mydb.net', 27017], :refresh_mode => :async,
55+
@connection = ReplSetConnection.new(['n1.mydb.net', 27017], :refresh_mode => :sync,
6056
:refresh_interval => 60)
6157

6258
Do not set this value to anything lower than 30, or you may start to experience performance issues.

lib/mongo/repl_set_connection.rb

+11-35
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,10 @@ class ReplSetConnection < Connection
5656
# @option opts [Float] :connect_timeout (nil) The number of seconds to wait before timing out a
5757
# connection attempt.
5858
# @option opts [Boolean] :ssl (false) If true, create the connection to the server using SSL.
59-
# @option opts [Boolean] :refresh_mode (false) Set this to :async to enable a background thread that
60-
# periodically updates the state of the connection. If, for example, you initially connect while a secondary
61-
# is down, this will reconnect to that secondary behind the scenes to
62-
# prevent you from having to reconnect manually. If set to :sync, refresh will happen
63-
# synchronously. If +false+, no automatic refresh will occur unless there's a connection failure.
59+
# @option opts [Boolean] :refresh_mode (false) Set this to :sync to periodically update the
60+
# state of the connection every :refresh_interval seconds. Replica set connection failures
61+
# will always trigger a complete refresh. This option is useful when you want to add new nodes
62+
# or remove replica set nodes not currently in use by the driver.
6463
# @option opts [Integer] :refresh_interval (90) If :refresh_mode is enabled, this is the number of seconds
6564
# between calls to check the replica set's state.
6665
# @option opts [Boolean] :require_primary (true) If true, require a primary node for the connection
@@ -107,9 +106,12 @@ def initialize(*args)
107106
@manager = nil
108107
@pool_mutex = Mutex.new
109108

110-
if ![:sync, :async, false].include?(@refresh_mode)
109+
if @refresh_mode == :async
110+
warn ":async refresh mode has been deprecated. Refresh
111+
mode will be disabled."
112+
elsif ![:sync, false].include?(@refresh_mode)
111113
raise MongoArgumentError,
112-
"Refresh mode must be one of :sync, :async, or false."
114+
"Refresh mode must be either :sync or false."
113115
end
114116

115117
# Are we allowing reads from secondaries?
@@ -124,15 +126,8 @@ def initialize(*args)
124126
end
125127

126128
@connected = false
127-
128-
# Store the refresher thread
129-
@refresh_thread = nil
130129
@refresh_version = 0
131130

132-
# Maps
133-
@threads_to_sockets = Hash.new { |h, k| h[k] = Hash.new }
134-
@tag_map = nil
135-
136131
# Replica set name
137132
if opts[:rs_name]
138133
warn ":rs_name option has been deprecated and will be removed in v2.0. " +
@@ -161,7 +156,6 @@ def connect
161156
manager.connect
162157

163158
update_config(manager)
164-
initiate_refresh_mode
165159

166160
if @require_primary && self.primary.nil? #TODO: in v2.0, we'll let this be optional and do a lazy connect.
167161
close
@@ -213,7 +207,6 @@ def hard_refresh!
213207
old_manager = @manager
214208
update_config(background_manager)
215209
old_manager.close(:soft => true)
216-
initiate_refresh_mode
217210

218211
return true
219212
end
@@ -265,7 +258,6 @@ def read_preference
265258
def close
266259
@connected = false
267260
@manager.close(:soft => true) if @manager
268-
@threads_to_sockets.clear
269261
end
270262

271263
# If a ConnectionFailure is raised, this method will be called
@@ -470,6 +462,8 @@ def setup(opts)
470462
write_logging_startup_message
471463
end
472464

465+
@last_refresh = Time.now
466+
473467
should_connect = opts.fetch(:connect, true)
474468
connect if should_connect
475469
end
@@ -482,24 +476,6 @@ def update_config(new_manager)
482476
@refresh_version += 1
483477
end
484478

485-
# If we're using async refresh, start
486-
# a background thread to run the refresh method
487-
# every @refresh_interval seconds.
488-
def initiate_refresh_mode
489-
if @refresh_mode == :async
490-
return if @refresh_thread && @refresh_thread.alive?
491-
@refresh_thread = Thread.new do
492-
while true do
493-
sleep(@refresh_interval)
494-
refresh
495-
end
496-
end
497-
@refresh_thread.priority = 1000
498-
end
499-
500-
@last_refresh = Time.now
501-
end
502-
503479
# Checkout a socket connected to a node with one of
504480
# the provided tags. If no such node exists, raise
505481
# an exception.

test/load/thin/load.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require File.join(File.dirname(__FILE__), '..', '..', '..', 'lib', 'mongo')
22
require 'logger'
33

4-
$con = Mongo::ReplSetConnection.new(['localhost', 30000], ['localhost', 30001], :read => :secondary, :refresh_mode => :async, :refresh_interval => 30)
4+
$con = Mongo::ReplSetConnection.new(['localhost', 30000], ['localhost', 30001], :read => :secondary, :refresh_mode => :sync, :refresh_interval => 30)
55
$db = $con['foo']
66

77
class Load < Sinatra::Base

0 commit comments

Comments
 (0)