Class: Faulty::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/faulty/scope.rb

Overview

A Scope is a group of options and circuits

For most use-cases the default scope should be used, however, it's possible to create any number of scopes for applications that require a more complex configuration or for testing.

For the most part, scopes are independent, however for some cache and storage backends, you will need to ensure that the cache keys and circuit names don't overlap between scopes. For example, if using the Redis storage backend, you should specify different key prefixes for each scope.

Defined Under Namespace

Classes: Options

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) {|Options| ... } ⇒ Scope

Create a new Faulty Scope

Note, the process of creating a new scope is not thread safe, so make sure scopes are setup before spawning threads.

Parameters:

  • options (Hash)

    Attributes for Options

Yields:

  • (Options)

    For setting options in a block

See Also:



75
76
77
78
# File 'lib/faulty/scope.rb', line 75

def initialize(**options, &block)
  @circuits = Concurrent::Map.new
  @options = Options.new(options, &block)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/faulty/scope.rb', line 15

def options
  @options
end

Instance Method Details

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

Create or retrieve a circuit

Within a scope, circuit instances have unique names, so if the given circuit name already exists, then the existing circuit will be returned, otherwise a new circuit will be created. If an existing circuit is returned, then the #options param and block are ignored.

Parameters:

  • name (String)

    The name of the circuit

  • options (Hash)

    Attributes for Circuit::Options

Yields:

Returns:

  • (Circuit)

    The new circuit or the existing circuit if it already exists



91
92
93
94
95
96
97
# File 'lib/faulty/scope.rb', line 91

def circuit(name, **options, &block)
  name = name.to_s
  options = options.merge(circuit_options)
  @circuits.compute_if_absent(name) do
    Circuit.new(name, **options, &block)
  end
end

#list_circuitsArray<String>

Get a list of all circuit names

Returns:

  • (Array<String>)

    The circuit names



102
103
104
# File 'lib/faulty/scope.rb', line 102

def list_circuits
  options.storage.list
end