Class: ActiveRecord::Middleware::DatabaseSelector::Resolver
- Inherits:
-
Object
- Object
- ActiveRecord::Middleware::DatabaseSelector::Resolver
- Defined in:
- lib/active_record/middleware/database_selector/resolver.rb,
lib/active_record/middleware/database_selector/resolver/session.rb
Overview
The Resolver class is used by the DatabaseSelector middleware to determine which database the request should use.
To change the behavior of the Resolver class in your application, create a custom resolver class that inherits from DatabaseSelector::Resolver and implements the methods that need to be changed.
By default the Resolver class will send read traffic to the replica if it’s been 2 seconds since the last write.
Defined Under Namespace
Classes: Session
Constant Summary collapse
- SEND_TO_REPLICA_DELAY =
:nodoc:
2.seconds
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#delay ⇒ Object
readonly
Returns the value of attribute delay.
-
#instrumenter ⇒ Object
readonly
Returns the value of attribute instrumenter.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(context, options = {}) ⇒ Resolver
constructor
A new instance of Resolver.
- #read(&blk) ⇒ Object
- #reading_request?(request) ⇒ Boolean
- #update_context(response) ⇒ Object
- #write(&blk) ⇒ Object
Constructor Details
#initialize(context, options = {}) ⇒ Resolver
Returns a new instance of Resolver.
26 27 28 29 30 31 |
# File 'lib/active_record/middleware/database_selector/resolver.rb', line 26 def initialize(context, = {}) @context = context @options = @delay = @options && @options[:delay] ? @options[:delay] : SEND_TO_REPLICA_DELAY @instrumenter = ActiveSupport::Notifications.instrumenter end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
33 34 35 |
# File 'lib/active_record/middleware/database_selector/resolver.rb', line 33 def context @context end |
#delay ⇒ Object (readonly)
Returns the value of attribute delay.
33 34 35 |
# File 'lib/active_record/middleware/database_selector/resolver.rb', line 33 def delay @delay end |
#instrumenter ⇒ Object (readonly)
Returns the value of attribute instrumenter.
33 34 35 |
# File 'lib/active_record/middleware/database_selector/resolver.rb', line 33 def instrumenter @instrumenter end |
Class Method Details
.call(context, options = {}) ⇒ Object
22 23 24 |
# File 'lib/active_record/middleware/database_selector/resolver.rb', line 22 def self.call(context, = {}) new(context, ) end |
Instance Method Details
#read(&blk) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/active_record/middleware/database_selector/resolver.rb', line 35 def read(&blk) if read_from_primary? read_from_primary(&blk) else read_from_replica(&blk) end end |
#reading_request?(request) ⇒ Boolean
51 52 53 |
# File 'lib/active_record/middleware/database_selector/resolver.rb', line 51 def reading_request?(request) request.get? || request.head? end |
#update_context(response) ⇒ Object
47 48 49 |
# File 'lib/active_record/middleware/database_selector/resolver.rb', line 47 def update_context(response) context.save(response) end |
#write(&blk) ⇒ Object
43 44 45 |
# File 'lib/active_record/middleware/database_selector/resolver.rb', line 43 def write(&blk) write_to_primary(&blk) end |