Module: Faulty::Patch

Defined in:
lib/faulty/patch.rb,
lib/faulty/patch/base.rb,
lib/faulty/patch/redis.rb,
lib/faulty/patch/mysql2.rb,
lib/faulty/patch/redis/patch.rb,
lib/faulty/patch/elasticsearch.rb,
lib/faulty/patch/redis/middleware.rb

Overview

Automatic wrappers for common core dependencies like database connections or caches

Defined Under Namespace

Modules: Base, Elasticsearch, Mysql2, Redis

Class Method Summary collapse

Class Method Details

.circuit_from_hash(default_name, hash, **options) {|Circuit::Options| ... } ⇒ Circuit?

Create a circuit from a configuration hash

This is intended to be used in contexts where the user passes in something like a connection hash to a third-party library. For example the Redis patch hooks into the normal Redis connection options to add a :faulty key, which is a hash of faulty circuit options. This is slightly different from the normal Faulty circuit options because we also accept an :instance key which is a faulty instance.

Examples:

# We pass in a faulty instance along with some circuit options
Patch.circuit_from_hash(
  :mysql,
  { host: 'localhost', faulty: {
    name: 'my_mysql', # A custom circuit name can be included
    instance: Faulty.new,
    sample_threshold: 5
    }
  }
)
# instance can be a registered faulty instance referenced by a string
or symbol
Faulty.register(:db_faulty, Faulty.new)
Patch.circuit_from_hash(
  :mysql,
  { host: 'localhost', faulty: { instance: :db_faulty } }
)
# If instance is a hash with the key :constant, the value can be
# a global constant name containing a Faulty instance
DB_FAULTY = Faulty.new
Patch.circuit_from_hash(
  :mysql,
  { host: 'localhost', faulty: { instance: { constant: 'DB_FAULTY' } } }
)
# Certain patches may want to enforce certain options like :errors
# This can be done via hash or the usual block syntax
Patch.circuit_from_hash(:mysql,
  { host: 'localhost', faulty: {} }
  errors: [Mysql2::Error]
)

Patch.circuit_from_hash(:mysql,
  { host: 'localhost', faulty: {} }
) do |conf|
  conf.errors = [Mysql2::Error]
end

Parameters:

  • default_name (String)

    The default name for the circuit

  • hash (Hash)

    A hash of user-provided options. Supports any circuit option and these additional options

  • options (Hash)

    Additional override options. Supports any circuit option and these additional ones.

Options Hash (hash):

  • :name (String)

    The circuit name. Defaults to default_name

  • :patch_errors (Boolean)

    By default, circuit errors will be subclasses of options[:patched_error_mapper]. The user can disable this by setting this option to false.

  • :instance (Faulty, String, Symbol, Hash{ constant: String })

    A reference to a faulty instance. See examples.

Options Hash (**options):

  • :patched_error_mapper (Module)

    The namespace module for patched errors or a mapping proc. See Circuit::Options :error_mapper

Yields:

Returns:

  • (Circuit, nil)

    The circuit if one was created



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/faulty/patch.rb', line 78

def circuit_from_hash(default_name, hash, **options, &block)
  return unless hash

  hash = symbolize_keys(hash)
  name = hash.delete(:name) || default_name
  patch_errors = hash.delete(:patch_errors) != false
  error_mapper = options.delete(:patched_error_mapper)
  hash[:error_mapper] ||= error_mapper if error_mapper && patch_errors
  faulty = resolve_instance(hash.delete(:instance))
  faulty.circuit(name, **hash, **options, &block)
end

.define_circuit_errors(namespace, base) ⇒ void

This method returns an undefined value.

Create a full set of CircuitErrors with a given base error class

For patches that need their errors to be subclasses of a common base.

Parameters:

  • namespace (Module)

    The module to define the error classes in

  • base (Class)

    The base class for the error classes



97
98
99
100
101
102
103
# File 'lib/faulty/patch.rb', line 97

def define_circuit_errors(namespace, base)
  circuit_error = Class.new(base) { include CircuitErrorBase }
  namespace.const_set('CircuitError', circuit_error)
  namespace.const_set('OpenCircuitError', Class.new(circuit_error))
  namespace.const_set('CircuitFailureError', Class.new(circuit_error))
  namespace.const_set('CircuitTrippedError', Class.new(circuit_error))
end