Sha256: 7b5017a7d88698c3f524c8aa4fcf631c69002dd5d66197d92475abc0473b8736

Contents?: true

Size: 1.5 KB

Versions: 2

Compression:

Stored size: 1.5 KB

Contents

require 'uri'
require 'singleton'
require 'active_support'
require 'logger'
require 'timeout'
require 'moneta'

require 'circuitbox/version'
require 'circuitbox/memcache_store'
require 'circuitbox/circuit_breaker'
require 'circuitbox/notifier'

require 'circuitbox/errors/error'
require 'circuitbox/errors/open_circuit_error'
require 'circuitbox/errors/service_failure_error'

class Circuitbox
  attr_accessor :circuits, :circuit_store
  cattr_accessor :configure

  def self.instance
    @@instance ||= new
  end

  def initialize
    self.instance_eval(&@@configure) if @@configure
  end

  def self.configure(&block)
    @@configure = block if block
  end

  def self.reset
    @@instance = nil
    @@configure = nil
  end

  def self.circuit_store
    self.instance.circuit_store ||= Moneta.new(:Memory, expires: true)
  end

  def self.circuit_store=(store)
    self.instance.circuit_store = store
  end

  def self.[](service_identifier, options = {})
    self.circuit(service_identifier, options)
  end

  def self.circuit(service_identifier, options = {})
    service_name = self.parameter_to_service_name(service_identifier)

    self.instance.circuits ||= Hash.new
    self.instance.circuits[service_name] ||= CircuitBreaker.new(service_name, options)

    if block_given?
      self.instance.circuits[service_name].run { yield }
    else
      self.instance.circuits[service_name]
    end
  end

  def self.parameter_to_service_name(param)
    uri = URI(param.to_s)
    uri.host.present? ? uri.host : param.to_s
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
circuitbox-1.1.0 lib/circuitbox.rb
circuitbox-1.0.3 lib/circuitbox.rb