From 6aaf01385e95376938275f484852b88fe84088b8 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Wed, 6 Feb 2019 11:55:53 -0800 Subject: Rename Session.build to Session.call This is more consistent with Resolver, which has build called. This allows using a Proc instead of a class, which could be nice if you need to vary switching logic based on the request in a more ad-hoc way (ie. check if it is an API request). --- activerecord/lib/active_record/middleware/database_selector.rb | 2 +- .../lib/active_record/middleware/database_selector/resolver/session.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'activerecord/lib/active_record/middleware') diff --git a/activerecord/lib/active_record/middleware/database_selector.rb b/activerecord/lib/active_record/middleware/database_selector.rb index 3ab50f5f6b..b95d8d6cb6 100644 --- a/activerecord/lib/active_record/middleware/database_selector.rb +++ b/activerecord/lib/active_record/middleware/database_selector.rb @@ -57,7 +57,7 @@ module ActiveRecord private def select_database(request, &blk) - operations = operations_klass.build(request) + operations = operations_klass.call(request) database_resolver = resolver_klass.call(operations, options) if reading_request?(request) diff --git a/activerecord/lib/active_record/middleware/database_selector/resolver/session.rb b/activerecord/lib/active_record/middleware/database_selector/resolver/session.rb index 33e0af5ee4..df7af054b7 100644 --- a/activerecord/lib/active_record/middleware/database_selector/resolver/session.rb +++ b/activerecord/lib/active_record/middleware/database_selector/resolver/session.rb @@ -10,7 +10,7 @@ module ActiveRecord # The last_write is used to determine whether it's safe to read # from the replica or the request needs to be sent to the primary. class Session # :nodoc: - def self.build(request) + def self.call(request) new(request.session) end -- cgit v1.2.3 From ed1f392ea6f3919aceda2d45f08cfa34780750f1 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Wed, 6 Feb 2019 11:57:30 -0800 Subject: Rename resolver ivar to operations in Resolver We're already in the resolver, we call this class "operations" in the middleware, so we should use the same naming here. --- .../active_record/middleware/database_selector/resolver.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'activerecord/lib/active_record/middleware') diff --git a/activerecord/lib/active_record/middleware/database_selector/resolver.rb b/activerecord/lib/active_record/middleware/database_selector/resolver.rb index a84c292714..775be95e0b 100644 --- a/activerecord/lib/active_record/middleware/database_selector/resolver.rb +++ b/activerecord/lib/active_record/middleware/database_selector/resolver.rb @@ -18,18 +18,18 @@ module ActiveRecord class Resolver # :nodoc: SEND_TO_REPLICA_DELAY = 2.seconds - def self.call(resolver, options = {}) - new(resolver, options) + def self.call(operations, options = {}) + new(operations, options) end - def initialize(resolver, options = {}) - @resolver = resolver + def initialize(operations, options = {}) + @operations = operations @options = options @delay = @options && @options[:delay] ? @options[:delay] : SEND_TO_REPLICA_DELAY @instrumenter = ActiveSupport::Notifications.instrumenter end - attr_reader :resolver, :delay, :instrumenter + attr_reader :operations, :delay, :instrumenter def read(&blk) if read_from_primary? @@ -68,7 +68,7 @@ module ActiveRecord instrumenter.instrument("database_selector.active_record.wrote_to_primary") do yield ensure - resolver.update_last_write_timestamp + operations.update_last_write_timestamp end end end @@ -82,7 +82,7 @@ module ActiveRecord end def time_since_last_write_ok? - Time.now - resolver.last_write_timestamp >= send_to_replica_delay + Time.now - operations.last_write_timestamp >= send_to_replica_delay end end end -- cgit v1.2.3 From a68bcde50660ba3adf4a2659f46212ad319cfeea Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Thu, 7 Feb 2019 11:59:45 -0800 Subject: Rename database selector operations to context --- .../active_record/middleware/database_selector.rb | 22 +++++++++++----------- .../middleware/database_selector/resolver.rb | 14 +++++++------- 2 files changed, 18 insertions(+), 18 deletions(-) (limited to 'activerecord/lib/active_record/middleware') diff --git a/activerecord/lib/active_record/middleware/database_selector.rb b/activerecord/lib/active_record/middleware/database_selector.rb index b95d8d6cb6..aad263695b 100644 --- a/activerecord/lib/active_record/middleware/database_selector.rb +++ b/activerecord/lib/active_record/middleware/database_selector.rb @@ -12,8 +12,8 @@ module ActiveRecord # # The resolver class defines when the application should switch (i.e. read # from the primary if a write occurred less than 2 seconds ago) and an - # operations class that sets a value that helps the resolver class decide - # when to switch. + # resolver context class that sets a value that helps the resolver class + # decide when to switch. # # Rails default middleware uses the request's session to set a timestamp # that informs the application when to read from a primary or read from a @@ -24,7 +24,7 @@ module ActiveRecord # # config.active_record.database_selector = { delay: 2.seconds } # config.active_record.database_resolver = ActiveRecord::Middleware::DatabaseSelector::Resolver - # config.active_record.database_operations = ActiveRecord::Middleware::DatabaseSelector::Resolver::Session + # config.active_record.database_resolver_context = ActiveRecord::Middleware::DatabaseSelector::Resolver::Session # # New applications will include these lines commented out in the production.rb. # @@ -33,16 +33,16 @@ module ActiveRecord # # config.active_record.database_selector = { delay: 2.seconds } # config.active_record.database_resolver = MyResolver - # config.active_record.database_operations = MyResolver::MySession + # config.active_record.database_resolver_context = MyResolver::MySession class DatabaseSelector - def initialize(app, resolver_klass = Resolver, operations_klass = Resolver::Session, options = {}) + def initialize(app, resolver_klass = Resolver, context_klass = Resolver::Session, options = {}) @app = app @resolver_klass = resolver_klass - @operations_klass = operations_klass + @context_klass = context_klass @options = options end - attr_reader :resolver_klass, :operations_klass, :options + attr_reader :resolver_klass, :context_klass, :options # Middleware that determines which database connection to use in a multiple # database application. @@ -57,13 +57,13 @@ module ActiveRecord private def select_database(request, &blk) - operations = operations_klass.call(request) - database_resolver = resolver_klass.call(operations, options) + context = context_klass.call(request) + resolver = resolver_klass.call(context, options) if reading_request?(request) - database_resolver.read(&blk) + resolver.read(&blk) else - database_resolver.write(&blk) + resolver.write(&blk) end end diff --git a/activerecord/lib/active_record/middleware/database_selector/resolver.rb b/activerecord/lib/active_record/middleware/database_selector/resolver.rb index 775be95e0b..80b8cd7cae 100644 --- a/activerecord/lib/active_record/middleware/database_selector/resolver.rb +++ b/activerecord/lib/active_record/middleware/database_selector/resolver.rb @@ -18,18 +18,18 @@ module ActiveRecord class Resolver # :nodoc: SEND_TO_REPLICA_DELAY = 2.seconds - def self.call(operations, options = {}) - new(operations, options) + def self.call(context, options = {}) + new(context, options) end - def initialize(operations, options = {}) - @operations = operations + def initialize(context, options = {}) + @context = context @options = options @delay = @options && @options[:delay] ? @options[:delay] : SEND_TO_REPLICA_DELAY @instrumenter = ActiveSupport::Notifications.instrumenter end - attr_reader :operations, :delay, :instrumenter + attr_reader :context, :delay, :instrumenter def read(&blk) if read_from_primary? @@ -68,7 +68,7 @@ module ActiveRecord instrumenter.instrument("database_selector.active_record.wrote_to_primary") do yield ensure - operations.update_last_write_timestamp + context.update_last_write_timestamp end end end @@ -82,7 +82,7 @@ module ActiveRecord end def time_since_last_write_ok? - Time.now - operations.last_write_timestamp >= send_to_replica_delay + Time.now - context.last_write_timestamp >= send_to_replica_delay end end end -- cgit v1.2.3