Class: Semian::ProtectedResource

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/semian/protected_resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, bulkhead, circuit_breaker) ⇒ ProtectedResource

Returns a new instance of ProtectedResource.



20
21
22
23
24
25
# File 'lib/semian/protected_resource.rb', line 20

def initialize(name, bulkhead, circuit_breaker)
  @name = name
  @bulkhead = bulkhead
  @circuit_breaker = circuit_breaker
  @updated_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
end

Instance Attribute Details

#bulkheadObject (readonly)

Returns the value of attribute bulkhead.



17
18
19
# File 'lib/semian/protected_resource.rb', line 17

def bulkhead
  @bulkhead
end

#circuit_breakerObject (readonly)

Returns the value of attribute circuit_breaker.



17
18
19
# File 'lib/semian/protected_resource.rb', line 17

def circuit_breaker
  @circuit_breaker
end

#nameObject (readonly)

Returns the value of attribute name.



17
18
19
# File 'lib/semian/protected_resource.rb', line 17

def name
  @name
end

#updated_atObject

Returns the value of attribute updated_at.



18
19
20
# File 'lib/semian/protected_resource.rb', line 18

def updated_at
  @updated_at
end

Instance Method Details

#acquire(timeout: nil, scope: nil, adapter: nil, resource: nil) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/semian/protected_resource.rb', line 32

def acquire(timeout: nil, scope: nil, adapter: nil, resource: nil)
  acquire_circuit_breaker(scope, adapter, resource) do
    acquire_bulkhead(timeout, scope, adapter) do |_, wait_time|
      Semian.notify(:success, self, scope, adapter, wait_time)
      yield self
    end
  end
end

#destroyObject



27
28
29
30
# File 'lib/semian/protected_resource.rb', line 27

def destroy
  @bulkhead&.destroy
  @circuit_breaker&.destroy
end

#in_use?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/semian/protected_resource.rb', line 41

def in_use?
  circuit_breaker&.in_use? || bulkhead&.in_use?
end