Class: ActiveRecordProxyAdapters::PrimaryReplicaProxy

Inherits:
Object
  • Object
show all
Includes:
Contextualizer, Hijackable, Mixin::Configuration
Defined in:
lib/active_record_proxy_adapters/primary_replica_proxy.rb

Overview

This is the base class for all proxies. It defines the methods that should be proxied and the logic to determine which database to use.

Direct Known Subclasses

Mysql2Proxy, PostgreSQLProxy, SQLite3Proxy

Constant Summary collapse

SQL_PRIMARY_MATCHERS =

All queries that match these patterns should be sent to the primary database

[
  /\A\s*select.+for update\Z/i, /select.+lock in share mode\Z/i,
  /\A\s*select.+(nextval|currval|lastval|get_lock|release_lock|pg_advisory_lock|pg_advisory_unlock)\(/i
].map(&:freeze).freeze
CTE_MATCHER =
/\A\s*WITH\s+(?<CTE>\S+\s+AS\s+\(\s?[\s\S]*\))/i
SQL_REPLICA_MATCHERS =

All queries that match these patterns should be sent to the replica database

[
  /\A\s*(select)\s/i,
  /#{CTE_MATCHER.source}\s*select/i
].map(&:freeze).freeze
SQL_ALL_MATCHERS =

All queries that match these patterns should be sent to all databases

[/\A\s*set\s/i].map(&:freeze).freeze
SQL_SKIP_ALL_MATCHERS =

Local sets queries should not be sent to all datbases

[/\A\s*set\s+local\s/i].map(&:freeze).freeze
WRITE_STATEMENT_MATCHERS =

These patterns define which database statments are considered write statments, so we can shortly re-route all requests to the primary database so the replica has time to replicate

[
  /(\A|\s+?)BEGIN/i,
  /(\A|\s+?)COMMIT/i,
  /(\A|\s+?)ROLLBACK/i,
  /(\A|\s+?)(INSERT\s+?INTO\s+?\S+?)/i,
  /(\A|\s+?)UPDATE\s+?\S+?/i,
  /(\A|\s+?)DELETE\s+?FROM\s+?\S+?/i,
  /(\A|\s+?)DROP\s/i
].map(&:freeze).freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::Configuration

#cache_key_for, #cache_store, #checkout_timeout, #context_store, #log_subscriber_prefix, #logger, #proxy_delay, #regexp_timeout_strategy

Methods included from Contextualizer

current_context, current_context=

Constructor Details

#initialize(primary_connection) ⇒ PrimaryReplicaProxy

Returns a new instance of PrimaryReplicaProxy.

Parameters:

  • primary_connection (ActiveRecord::ConnectionAdatpers::AbstractAdapter)


56
57
58
59
# File 'lib/active_record_proxy_adapters/primary_replica_proxy.rb', line 56

def initialize(primary_connection)
  @primary_connection    = primary_connection
  @active_record_context = ActiveRecordContext.new
end

Class Method Details

.hijacked_methodsObject



51
52
53
# File 'lib/active_record_proxy_adapters/primary_replica_proxy.rb', line 51

def self.hijacked_methods
  @hijacked_methods.to_a
end