Class: ResilientReads::Configuration
- Inherits:
-
Object
- Object
- ResilientReads::Configuration
- Defined in:
- lib/resilient_reads/configuration.rb
Instance Attribute Summary collapse
-
#auto_detect_replicas ⇒ Object
When true and replicas is nil, auto-detect replica configs from database.yml.
-
#balancing_strategy ⇒ Object
Load balancing strategy: :round_robin or :random.
-
#by_default ⇒ Object
When true, all reads are distributed to replicas by default (via middleware).
-
#default_options ⇒ Object
Hash of default options for distribute_reads blocks.
-
#eager_load ⇒ Object
When true, ActiveRecord::Relation returned from distribute_reads blocks are automatically loaded to ensure execution on the replica.
-
#failover ⇒ Object
When true, queries fall back to primary if no healthy replicas are available.
-
#health_check_interval ⇒ Object
Seconds between background health checks on replicas.
-
#lag_check_interval ⇒ Object
Seconds to cache the lag check result per replica.
-
#lag_failover ⇒ Object
When true and max_lag is set, queries fall back to primary instead of raising.
-
#log_query_level ⇒ Object
Log level for per-query routing messages.
-
#log_query_routing ⇒ Object
When true, logs which connection (primary / replica name) handled each query routed through the adapter patch.
-
#logger ⇒ Object
Logger instance.
-
#max_lag ⇒ Object
Maximum acceptable replication lag in seconds.
-
#primary_delay ⇒ Object
Seconds to keep using primary after a write (read-your-own-write protection).
-
#query_cache_enabled ⇒ Object
When true, caches SQL pattern-matching results (write_query? / skip_replica_routing?) in an in-memory LRU cache so the regex doesn’t run on every identical query string.
-
#query_cache_max_size ⇒ Object
Maximum number of entries in the SQL pattern cache.
-
#replica_pattern ⇒ Object
Regex pattern for auto-detecting replica config names.
-
#replicas ⇒ Object
Explicit list of replica database config names (symbols).
Instance Method Summary collapse
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/resilient_reads/configuration.rb', line 69 def initialize @by_default = false @eager_load = false @balancing_strategy = :round_robin @health_check_interval = 30 @max_lag = nil @lag_check_interval = 5 @lag_failover = false @failover = true @logger = nil @log_query_routing = true @log_query_level = :info @replicas = nil @auto_detect_replicas = true @replica_pattern = /\Areplica\d*\z/ @primary_delay = 2 @default_options = {} @query_cache_enabled = true @query_cache_max_size = 10_000 end |
Instance Attribute Details
#auto_detect_replicas ⇒ Object
When true and replicas is nil, auto-detect replica configs from database.yml.
49 50 51 |
# File 'lib/resilient_reads/configuration.rb', line 49 def auto_detect_replicas @auto_detect_replicas end |
#balancing_strategy ⇒ Object
Load balancing strategy: :round_robin or :random
11 12 13 |
# File 'lib/resilient_reads/configuration.rb', line 11 def balancing_strategy @balancing_strategy end |
#by_default ⇒ Object
When true, all reads are distributed to replicas by default (via middleware).
4 5 6 |
# File 'lib/resilient_reads/configuration.rb', line 4 def by_default @by_default end |
#default_options ⇒ Object
Hash of default options for distribute_reads blocks.
59 60 61 |
# File 'lib/resilient_reads/configuration.rb', line 59 def @default_options end |
#eager_load ⇒ Object
When true, ActiveRecord::Relation returned from distribute_reads blocks are automatically loaded to ensure execution on the replica.
8 9 10 |
# File 'lib/resilient_reads/configuration.rb', line 8 def eager_load @eager_load end |
#failover ⇒ Object
When true, queries fall back to primary if no healthy replicas are available. When false, raises ResilientReads::NoHealthyReplica.
28 29 30 |
# File 'lib/resilient_reads/configuration.rb', line 28 def failover @failover end |
#health_check_interval ⇒ Object
Seconds between background health checks on replicas.
14 15 16 |
# File 'lib/resilient_reads/configuration.rb', line 14 def health_check_interval @health_check_interval end |
#lag_check_interval ⇒ Object
Seconds to cache the lag check result per replica. Prevents querying the replica for lag on every single read. Default: 5.
21 22 23 |
# File 'lib/resilient_reads/configuration.rb', line 21 def lag_check_interval @lag_check_interval end |
#lag_failover ⇒ Object
When true and max_lag is set, queries fall back to primary instead of raising.
24 25 26 |
# File 'lib/resilient_reads/configuration.rb', line 24 def lag_failover @lag_failover end |
#log_query_level ⇒ Object
Log level for per-query routing messages. Defaults to :info so messages appear in standard Rails development/production logs. Set to :debug if the output is too noisy.
41 42 43 |
# File 'lib/resilient_reads/configuration.rb', line 41 def log_query_level @log_query_level end |
#log_query_routing ⇒ Object
When true, logs which connection (primary / replica name) handled each query routed through the adapter patch. Set to false to silence per-query routing logs.
36 37 38 |
# File 'lib/resilient_reads/configuration.rb', line 36 def log_query_routing @log_query_routing end |
#logger ⇒ Object
Logger instance. Defaults to Rails.logger when available.
31 32 33 |
# File 'lib/resilient_reads/configuration.rb', line 31 def logger @logger end |
#max_lag ⇒ Object
Maximum acceptable replication lag in seconds. nil = no check.
17 18 19 |
# File 'lib/resilient_reads/configuration.rb', line 17 def max_lag @max_lag end |
#primary_delay ⇒ Object
Seconds to keep using primary after a write (read-your-own-write protection).
56 57 58 |
# File 'lib/resilient_reads/configuration.rb', line 56 def primary_delay @primary_delay end |
#query_cache_enabled ⇒ Object
When true, caches SQL pattern-matching results (write_query? / skip_replica_routing?) in an in-memory LRU cache so the regex doesn’t run on every identical query string.
64 65 66 |
# File 'lib/resilient_reads/configuration.rb', line 64 def query_cache_enabled @query_cache_enabled end |
#query_cache_max_size ⇒ Object
Maximum number of entries in the SQL pattern cache.
67 68 69 |
# File 'lib/resilient_reads/configuration.rb', line 67 def query_cache_max_size @query_cache_max_size end |
#replica_pattern ⇒ Object
Regex pattern for auto-detecting replica config names. Only configs matching this pattern AND having replica: true are used.
53 54 55 |
# File 'lib/resilient_reads/configuration.rb', line 53 def replica_pattern @replica_pattern end |
#replicas ⇒ Object
Explicit list of replica database config names (symbols). Example: [:replica, :replica2, :replica3] When nil, replicas are auto-detected from database.yml.
46 47 48 |
# File 'lib/resilient_reads/configuration.rb', line 46 def replicas @replicas end |