Module: Faulty

Defined in:
lib/faulty.rb,
lib/faulty/cache.rb,
lib/faulty/error.rb,
lib/faulty/scope.rb,
lib/faulty/events.rb,
lib/faulty/result.rb,
lib/faulty/status.rb,
lib/faulty/circuit.rb,
lib/faulty/storage.rb,
lib/faulty/version.rb,
lib/faulty/cache/mock.rb,
lib/faulty/cache/null.rb,
lib/faulty/cache/rails.rb,
lib/faulty/cache/default.rb,
lib/faulty/storage/redis.rb,
lib/faulty/storage/memory.rb,
lib/faulty/cache/interface.rb,
lib/faulty/events/notifier.rb,
lib/faulty/immutable_options.rb,
lib/faulty/storage/interface.rb,
lib/faulty/events/log_listener.rb,
lib/faulty/events/callback_listener.rb,
lib/faulty/events/listener_interface.rb,
lib/faulty/cache/fault_tolerant_proxy.rb,
lib/faulty/events/honeybadger_listener.rb,
lib/faulty/storage/fault_tolerant_proxy.rb

Overview

The top-level namespace for Faulty

Fault-tolerance tools for ruby based on circuit-breakers

Defined Under Namespace

Modules: Cache, Events, ImmutableOptions, Storage Classes: AlreadyInitializedError, Circuit, CircuitError, CircuitFailureError, CircuitTrippedError, FaultyError, MissingDefaultScopeError, OpenCircuitError, Result, Scope, Status, UncheckedResultError, UninitializedError, WrongResultError

Class Method Summary collapse

Class Method Details

.[](scope_name) ⇒ Scope?

Get a scope by name

Returns:

  • (Scope, nil)

    The named scope if it is registered

Raises:



62
63
64
65
66
# File 'lib/faulty.rb', line 62

def [](scope_name)
  raise UninitializedError unless @scopes

  @scopes[scope_name]
end

.circuit(name, **config) {|Circuit::Options| ... } ⇒ Circuit

Get or create a circuit for the default scope

Parameters:

Yields:

Returns:

  • (Circuit)

    The new circuit or the existing circuit if it already exists

Raises:

  • UninitializedError If the default scope has not been created



97
98
99
# File 'lib/faulty.rb', line 97

def circuit(name, **config, &block)
  default.circuit(name, **config, &block)
end

.current_timeTime

The current time

Used by Faulty wherever the current time is needed. Can be overridden for testing

Returns:

  • (Time)

    The current time



114
115
116
# File 'lib/faulty.rb', line 114

def current_time
  Time.now.to_i
end

.defaultScope?

Get the default scope given during init

Returns:

  • (Scope, nil)

    The default scope if it is registered

Raises:



52
53
54
55
56
57
# File 'lib/faulty.rb', line 52

def default
  raise UninitializedError unless @scopes
  raise MissingDefaultScopeError unless @default_scope

  self[@default_scope]
end

.init(scope_name = :default, **config) {|Scope::Options| ... } ⇒ self

Start the Faulty environment

This creates a global shared Faulty state for configuration and for re-using State objects.

Not thread safe, should be executed before any worker threads are spawned.

If you prefer dependency-injection instead of global state, you can skip init and pass a Scope directly to your dependencies.

Parameters:

  • scope_name (Symbol) (defaults to: :default)

    The name of the default scope. Can be set to nil to skip creating a default scope.

  • config (Hash)

    Attributes for Faulty::Scope::Options

Yields:

Returns:

  • (self)


37
38
39
40
41
42
43
44
45
46
47
# File 'lib/faulty.rb', line 37

def init(scope_name = :default, **config, &block)
  raise AlreadyInitializedError if @scopes

  @default_scope = scope_name
  @scopes = Concurrent::Map.new
  register(scope_name, Scope.new(**config, &block)) unless scope_name.nil?
  self
rescue StandardError
  @scopes = nil
  raise
end

.list_circuitsArray<String>

Get a list of all circuit names for the default scope

Returns:

  • (Array<String>)

    The circuit names



104
105
106
# File 'lib/faulty.rb', line 104

def list_circuits
  options.storage.list
end

.optionsScope::Options

Get the options for the default scope

Returns:

Raises:

  • MissingDefaultScopeError If the default scope has not been created



87
88
89
# File 'lib/faulty.rb', line 87

def options
  default.options
end

.register(name, scope) ⇒ Scope?

Register a scope to the global Faulty state

Will not replace an existing scope with the same name. Check the return value if you need to know whether the scope already existed.

Parameters:

  • name (Symbol)

    The name of the scope to register

  • scope (Scope)

    The scope to register

Returns:

  • (Scope, nil)

    The previously-registered scope of that name if it already existed, otherwise nil.

Raises:



77
78
79
80
81
# File 'lib/faulty.rb', line 77

def register(name, scope)
  raise UninitializedError unless @scopes

  @scopes.put_if_absent(name, scope)
end

.versionObject

The current Faulty version



5
6
7
# File 'lib/faulty/version.rb', line 5

def self.version
  Gem::Version.new('0.1.4')
end