Module: Faulty::Patch::Mysql2
Overview
Patch Mysql2 to run connections and queries in a circuit
This module is not required by default
Pass a :faulty
key into your MySQL connection options to enable
circuit protection. See circuit_from_hash for the available
options.
COMMIT, ROLLBACK, and RELEASE SAVEPOINT queries are intentionally not protected by the circuit. This is to allow open transactions to be closed if possible.
By default, all circuit errors raised by this patch inherit from
::Mysql2::Error::ConnectionError
Constant Summary collapse
- QUERY_WHITELIST =
[ %r{\A(?:/\*.*?\*/)?\s*ROLLBACK}i, %r{\A(?:/\*.*?\*/)?\s*COMMIT}i, %r{\A(?:/\*.*?\*/)?\s*RELEASE\s+SAVEPOINT}i ].freeze
Instance Method Summary collapse
-
#connect(*args) ⇒ Object
Protect the initial connnection.
- #initialize(opts = {}) ⇒ Object
-
#ping ⇒ Object
Protect manual connection pings.
-
#query(*args) ⇒ Object
Protect queries unless they are whitelisted.
Methods included from Base
Instance Method Details
#connect(*args) ⇒ Object
Protect the initial connnection
70 71 72 |
# File 'lib/faulty/patch/mysql2.rb', line 70 def connect(*args) faulty_run { super } end |
#initialize(opts = {}) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/faulty/patch/mysql2.rb', line 48 def initialize(opts = {}) @faulty_circuit = Patch.circuit_from_hash( 'mysql2', opts[:faulty], errors: [ ::Mysql2::Error::ConnectionError, ::Mysql2::Error::TimeoutError ], patched_error_mapper: Faulty::Patch::Mysql2 ) super end |
#ping ⇒ Object
Protect manual connection pings
63 64 65 66 67 |
# File 'lib/faulty/patch/mysql2.rb', line 63 def ping faulty_run { super } rescue Faulty::Patch::Mysql2::FaultyError false end |
#query(*args) ⇒ Object
Protect queries unless they are whitelisted
75 76 77 78 79 |
# File 'lib/faulty/patch/mysql2.rb', line 75 def query(*args) return super if QUERY_WHITELIST.any? { |r| !r.match(args.first).nil? } faulty_run { super } end |