module Faulty::Patch::Mysql2

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 {Patch.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.

@example

require 'faulty/patch/mysql2'

mysql = Mysql2::Client.new(host: '127.0.0.1', faulty: {})
mysql.query('SELECT * FROM users') # raises Faulty::CircuitError if connection fails

# If the faulty key is not given, no circuit is used
mysql = Mysql2::Client.new(host: '127.0.0.1')
mysql.query('SELECT * FROM users') # not protected by a circuit

@see Patch.circuit_from_hash

Constants

QUERY_WHITELIST

Public Class Methods

new(opts = {}) click to toggle source
Calls superclass method
# File lib/faulty/patch/mysql2.rb, line 45
def initialize(opts = {})
  @faulty_circuit = Patch.circuit_from_hash(
    'mysql2',
    opts[:faulty],
    errors: [
      ::Mysql2::Error::ConnectionError,
      ::Mysql2::Error::TimeoutError
    ],
    patched_error_module: Faulty::Patch::Mysql2
  )

  super
end

Public Instance Methods

connect(*args) click to toggle source

Protect the initial connnection

Calls superclass method
# File lib/faulty/patch/mysql2.rb, line 67
def connect(*args)
  faulty_run { super }
end
ping() click to toggle source

Protect manual connection pings

Calls superclass method
# File lib/faulty/patch/mysql2.rb, line 60
def ping
  faulty_run { super }
rescue Faulty::Patch::Mysql2::FaultyError
  false
end
query(*args) click to toggle source

Protect queries unless they are whitelisted

Calls superclass method
# File lib/faulty/patch/mysql2.rb, line 72
def query(*args)
  return super if QUERY_WHITELIST.any? { |r| !r.match(args.first).nil? }

  faulty_run { super }
end