Class: Expeditor::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/expeditor/service.rb

Direct Known Subclasses

Expeditor::Services::Default

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
# File 'lib/expeditor/service.rb', line 8

def initialize(opts = {})
  @executor = opts.fetch(:executor) { Concurrent::ThreadPoolExecutor.new }
  @threshold = opts.fetch(:threshold, 0.5) # is 0.5 ok?
  @non_break_count = opts.fetch(:non_break_count, 100) # is 100 ok?
  @sleep = opts.fetch(:sleep, 1)
  @bucket_opts = {
    size: 10,
    per: opts.fetch(:period, 10).to_f / 10
  }
  reset_status!
  @fallback_enabled = true
end

Instance Attribute Details

#executorObject (readonly)

Returns the value of attribute executor.



5
6
7
# File 'lib/expeditor/service.rb', line 5

def executor
  @executor
end

#fallback_enabledObject

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

#breakObject



37
38
39
# File 'lib/expeditor/service.rb', line 37

def break
  @bucket.increment :break
end

#current_statusObject



73
74
75
# File 'lib/expeditor/service.rb', line 73

def current_status
  @bucket.current
end

#dependencyObject



41
42
43
# File 'lib/expeditor/service.rb', line 41

def dependency
  @bucket.increment :dependency
end

#failureObject



25
26
27
# File 'lib/expeditor/service.rb', line 25

def failure
  @bucket.increment :failure
end

#fallback_enabled?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/expeditor/service.rb', line 45

def fallback_enabled?
  !!fallback_enabled
end

#open?Boolean

break circuit?

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/expeditor/service.rb', line 50

def open?
  if @breaking
    if Time.now - @break_start > @sleep
      @breaking = false
      @break_start = nil
    else
      return true
    end
  end
  open = calc_open
  if open
    @breaking = true
    @break_start = Time.now
  end
  open
end

#rejectionObject



29
30
31
# File 'lib/expeditor/service.rb', line 29

def rejection
  @bucket.increment :rejection
end

#reset_status!Object



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

def reset_status!
  @bucket = Expeditor::Bucket.new(@bucket_opts)
  @breaking = false
  @break_start = nil
end

#shutdownObject

shutdown thread pool after shutdown, if you create thread, RejectedExecutionError is raised.



69
70
71
# File 'lib/expeditor/service.rb', line 69

def shutdown
  @executor.shutdown
end

#successObject



21
22
23
# File 'lib/expeditor/service.rb', line 21

def success
  @bucket.increment :success
end

#timeoutObject



33
34
35
# File 'lib/expeditor/service.rb', line 33

def timeout
  @bucket.increment :timeout
end