Module: Sidekiq::CircuitBreaker::API

Extended by:
API
Included in:
API, Manager
Defined in:
lib/sidekiq/circuit_breaker/api.rb

Instance Method Summary collapse

Instance Method Details

#circuit_opened?(scope) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
# File 'lib/sidekiq/circuit_breaker/api.rb', line 45

def circuit_opened?(scope)
  Sidekiq.redis do |conn|
    conn.exists(open_key(scope))
  end
end

#failure_count_for_scope(scope) ⇒ Object



51
52
53
54
55
# File 'lib/sidekiq/circuit_breaker/api.rb', line 51

def failure_count_for_scope(scope)
  Sidekiq.redis do |conn|
    conn.get(failure_key(scope))
  end.to_i
end

#open_circuit(scope, ttl = 120) ⇒ Object

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
# File 'lib/sidekiq/circuit_breaker/api.rb', line 8

def open_circuit(scope, ttl = 120)
  raise ArgumentError, 'scope must not be nil' if scope.nil?
  Sidekiq.redis do |conn|
    conn.setex(open_key(scope), ttl, 0)
    # reset failure count
    conn.del(failure_key(scope))
  end
  true
end

#register_failure_for_scope(scope, ttl = 120) ⇒ Object

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/sidekiq/circuit_breaker/api.rb', line 18

def register_failure_for_scope(scope, ttl = 120)
  raise ArgumentError, 'scope must not be nil' if scope.nil?
  key = failure_key(scope)

  _, failure_count = Sidekiq.redis do |conn|
    conn.multi do
      conn.expire(key, ttl)
      conn.incr(key)
    end
  end

  failure_count
end

#register_success_for_scope(scope) ⇒ Object

Raises:

  • (ArgumentError)


32
33
34
35
36
37
# File 'lib/sidekiq/circuit_breaker/api.rb', line 32

def register_success_for_scope(scope)
  raise ArgumentError, 'scope must not be nil' if scope.nil?
  Sidekiq.redis do |conn|
    conn.del(failure_key(scope), open_key(scope))
  end
end

#time_to_open_the_circuit(scope) ⇒ Object



39
40
41
42
43
# File 'lib/sidekiq/circuit_breaker/api.rb', line 39

def time_to_open_the_circuit(scope)
  Sidekiq.redis do |conn|
    conn.ttl(open_key(scope))
  end
end