diff options
author | Eileen Uchitelle <eileencodes@gmail.com> | 2019-02-01 10:16:34 -0500 |
---|---|---|
committer | Eileen Uchitelle <eileencodes@gmail.com> | 2019-02-01 11:34:50 -0500 |
commit | 31f205234acb5f4b084eb726cf903c55d59677fa (patch) | |
tree | 2f94b169a30755e64227073cbeef0060d63d6193 /activerecord/lib/active_record/middleware | |
parent | 79bc9e81c3d47be6336223be39cb3bcaeddc0a39 (diff) | |
download | rails-31f205234acb5f4b084eb726cf903c55d59677fa.tar.gz rails-31f205234acb5f4b084eb726cf903c55d59677fa.tar.bz2 rails-31f205234acb5f4b084eb726cf903c55d59677fa.zip |
Refactor options for middleware
Right now we only have one option that's supported, the delay. However I
can see us supporting other options in the future.
This PR refactors the options to get passed into the resolver so whether
you're using middleware or using the config options you can pass options
to the resolver. This will also make it easy to add new options in the
future.
Diffstat (limited to 'activerecord/lib/active_record/middleware')
-rw-r--r-- | activerecord/lib/active_record/middleware/database_selector.rb | 7 | ||||
-rw-r--r-- | activerecord/lib/active_record/middleware/database_selector/resolver.rb | 13 |
2 files changed, 11 insertions, 9 deletions
diff --git a/activerecord/lib/active_record/middleware/database_selector.rb b/activerecord/lib/active_record/middleware/database_selector.rb index adcfca4f8d..3ab50f5f6b 100644 --- a/activerecord/lib/active_record/middleware/database_selector.rb +++ b/activerecord/lib/active_record/middleware/database_selector.rb @@ -35,13 +35,14 @@ module ActiveRecord # config.active_record.database_resolver = MyResolver # config.active_record.database_operations = MyResolver::MySession class DatabaseSelector - def initialize(app, resolver_klass = Resolver, operations_klass = Resolver::Session) + def initialize(app, resolver_klass = Resolver, operations_klass = Resolver::Session, options = {}) @app = app @resolver_klass = resolver_klass @operations_klass = operations_klass + @options = options end - attr_reader :resolver_klass, :operations_klass + attr_reader :resolver_klass, :operations_klass, :options # Middleware that determines which database connection to use in a multiple # database application. @@ -57,7 +58,7 @@ module ActiveRecord def select_database(request, &blk) operations = operations_klass.build(request) - database_resolver = resolver_klass.call(operations) + database_resolver = resolver_klass.call(operations, options) if reading_request?(request) database_resolver.read(&blk) diff --git a/activerecord/lib/active_record/middleware/database_selector/resolver.rb b/activerecord/lib/active_record/middleware/database_selector/resolver.rb index acdb9b3238..0eeb0453ef 100644 --- a/activerecord/lib/active_record/middleware/database_selector/resolver.rb +++ b/activerecord/lib/active_record/middleware/database_selector/resolver.rb @@ -18,16 +18,18 @@ module ActiveRecord class Resolver # :nodoc: SEND_TO_REPLICA_DELAY = 2.seconds - def self.call(resolver) - new(resolver) + def self.call(resolver, options = {}) + new(resolver, options) end - def initialize(resolver) + def initialize(resolver, options = {}) @resolver = resolver + @options = options + @delay = @options && @options[:delay] ? @options[:delay] : SEND_TO_REPLICA_DELAY @instrumenter = ActiveSupport::Notifications.instrumenter end - attr_reader :resolver, :instrumenter + attr_reader :resolver, :delay, :instrumenter def read(&blk) if read_from_primary? @@ -76,8 +78,7 @@ module ActiveRecord end def send_to_replica_delay - (ActiveRecord::Base.database_selector && ActiveRecord::Base.database_selector[:delay]) || - SEND_TO_REPLICA_DELAY + delay end def time_since_last_write_ok? |