Class: Expeditor::Service
- Inherits:
-
Object
- Object
- Expeditor::Service
show all
- Defined in:
- lib/expeditor/service.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(opts = {}) ⇒ Service
Returns a new instance of Service.
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/expeditor/service.rb', line 8
def initialize(opts = {})
@mutex = Mutex.new
@executor = opts.fetch(:executor) { Concurrent::ThreadPoolExecutor.new }
@threshold = opts.fetch(:threshold, 0.5)
@non_break_count = opts.fetch(:non_break_count, 20)
@sleep = opts.fetch(:sleep, 1)
granularity = 10
@rolling_number_opts = {
size: granularity,
per_time: opts.fetch(:period, 10).to_f / granularity
}
reset_status!
@fallback_enabled = true
end
|
Instance Attribute Details
#executor ⇒ Object
Returns the value of attribute executor.
5
6
7
|
# File 'lib/expeditor/service.rb', line 5
def executor
@executor
end
|
#fallback_enabled ⇒ Object
Returns the value of attribute fallback_enabled.
6
7
8
|
# File 'lib/expeditor/service.rb', line 6
def fallback_enabled
@fallback_enabled
end
|
Instance Method Details
#break ⇒ Object
39
40
41
|
# File 'lib/expeditor/service.rb', line 39
def break
@rolling_number.increment :break
end
|
#breaking? ⇒ Boolean
51
52
53
|
# File 'lib/expeditor/service.rb', line 51
def breaking?
@breaking
end
|
#current_status ⇒ Object
104
105
106
107
|
# File 'lib/expeditor/service.rb', line 104
def current_status
warn 'Expeditor::Service#current_status is deprecated. Please use #status instead.'
@rolling_number.current
end
|
#dependency ⇒ Object
43
44
45
|
# File 'lib/expeditor/service.rb', line 43
def dependency
@rolling_number.increment :dependency
end
|
#failure ⇒ Object
27
28
29
|
# File 'lib/expeditor/service.rb', line 27
def failure
@rolling_number.increment :failure
end
|
#fallback_enabled? ⇒ Boolean
47
48
49
|
# File 'lib/expeditor/service.rb', line 47
def fallback_enabled?
!!fallback_enabled
end
|
#rejection ⇒ Object
31
32
33
|
# File 'lib/expeditor/service.rb', line 31
def rejection
@rolling_number.increment :rejection
end
|
#reset_status! ⇒ Object
109
110
111
112
113
114
115
|
# File 'lib/expeditor/service.rb', line 109
def reset_status!
@mutex.synchronize do
@rolling_number = Expeditor::RollingNumber.new(**@rolling_number_opts)
@breaking = false
@break_start = nil
end
end
|
#run_if_allowed ⇒ Object
Run given block when the request is allowed, otherwise raise Expeditor::CircuitBreakError. When breaking and sleep time was passed, the circuit breaker tries to close the circuit. So subsequent single command execution is allowed (will not be breaked) to check the service is healthy or not. The circuit breaker only allows one request so other subsequent requests will be aborted with CircuitBreakError. When the test request succeeds, the circuit breaker resets the service status and closes the circuit.
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/expeditor/service.rb', line 63
def run_if_allowed
if @breaking
now = Time.now
allow_single_request = false
@mutex.synchronize do
allow_single_request = now - @break_start > @sleep
@break_start = now if allow_single_request
end
if allow_single_request
result = yield
reset_status!
result
else
raise CircuitBreakError
end
else
open = calc_open
if open
change_state(true, Time.now)
raise CircuitBreakError
else
yield
end
end
end
|
#shutdown ⇒ Object
shutdown thread pool after shutdown, if you create thread, RejectedExecutionError is raised.
95
96
97
|
# File 'lib/expeditor/service.rb', line 95
def shutdown
@executor.shutdown
end
|
#status ⇒ Object
99
100
101
|
# File 'lib/expeditor/service.rb', line 99
def status
@rolling_number.total
end
|
#success ⇒ Object
23
24
25
|
# File 'lib/expeditor/service.rb', line 23
def success
@rolling_number.increment :success
end
|
#timeout ⇒ Object
35
36
37
|
# File 'lib/expeditor/service.rb', line 35
def timeout
@rolling_number.increment :timeout
end
|