Class: CB2::Breaker

Inherits:
Object
  • Object
show all
Defined in:
lib/cb2/breaker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Breaker

Returns a new instance of Breaker.



4
5
6
7
# File 'lib/cb2/breaker.rb', line 4

def initialize(options)
  @service  = options[:service] || "default"
  @strategy = initialize_strategy(options)
end

Instance Attribute Details

#serviceObject

Returns the value of attribute service.



2
3
4
# File 'lib/cb2/breaker.rb', line 2

def service
  @service
end

#strategyObject

Returns the value of attribute strategy.



2
3
4
# File 'lib/cb2/breaker.rb', line 2

def strategy
  @strategy
end

Instance Method Details

#initialize_strategy(options) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/cb2/breaker.rb', line 47

def initialize_strategy(options)
  strategy_options = options.dup.merge(service: self.service)

  if options[:strategy].respond_to?(:open?)
    return options[:strategy].new(strategy_options)
  end

  case options[:strategy].to_s
  when "", "percentage"
    CB2::Percentage.new(strategy_options)
  when "rolling_window"
    CB2::RollingWindow.new(strategy_options)
  when "stub"
    CB2::Stub.new(strategy_options)
  end
end

#open?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
# File 'lib/cb2/breaker.rb', line 26

def open?
  strategy.open?
rescue Redis::BaseError
  false
end

#process_countObject



32
33
34
35
# File 'lib/cb2/breaker.rb', line 32

def process_count
  strategy.count if strategy.respond_to?(:count)
rescue Redis::BaseError
end

#process_errorObject



42
43
44
45
# File 'lib/cb2/breaker.rb', line 42

def process_error
  strategy.error if strategy.respond_to?(:error)
rescue Redis::BaseError
end

#process_successObject



37
38
39
40
# File 'lib/cb2/breaker.rb', line 37

def process_success
  strategy.success if strategy.respond_to?(:success)
rescue Redis::BaseError
end

#runObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cb2/breaker.rb', line 9

def run
  if open?
    raise CB2::BreakerOpen.new("#{service} breaker open")
  end

  begin
    process_count
    ret = yield
    process_success
  rescue => e
    process_error
    raise e
  end

  return ret
end