Class: Legion::Extensions::MicrosoftTeams::Faraday::ThrottleCircuit

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/legion/extensions/microsoft_teams/faraday/throttle_circuit.rb

Constant Summary collapse

CIRCUIT_KEY =
'microsoft_teams:graph:circuit:v1:global'
DEFAULT_SOFT_PERCENTAGE =
0.8
DEFAULT_SOFT_TTL =
60
DEFAULT_FALLBACK_TTL =
60
DEFAULT_INSIGHTS_TTL =
600

Instance Method Summary collapse

Constructor Details

#initialize(app, soft_percentage: DEFAULT_SOFT_PERCENTAGE, soft_ttl: DEFAULT_SOFT_TTL, fallback_ttl: DEFAULT_FALLBACK_TTL, insights_ttl: DEFAULT_INSIGHTS_TTL, logger: nil) ⇒ ThrottleCircuit

Returns a new instance of ThrottleCircuit.



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/legion/extensions/microsoft_teams/faraday/throttle_circuit.rb', line 17

def initialize(app,
               soft_percentage: DEFAULT_SOFT_PERCENTAGE,
               soft_ttl: DEFAULT_SOFT_TTL,
               fallback_ttl: DEFAULT_FALLBACK_TTL,
               insights_ttl: DEFAULT_INSIGHTS_TTL,
               logger: nil)
  super(app)
  @soft_percentage = Float(soft_percentage)
  @soft_ttl = Integer(soft_ttl)
  @fallback_ttl = Integer(fallback_ttl)
  @insights_ttl = Integer(insights_ttl)
  @logger = logger
end

Instance Method Details

#call(env) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/legion/extensions/microsoft_teams/faraday/throttle_circuit.rb', line 31

def call(env)
  remaining = circuit_remaining
  if remaining&.positive?
    path = request_path(env)
    @logger&.debug("[throttle_circuit] circuit open, #{remaining}s remaining, blocking #{path}")
    raise Errors::Throttled.new(
      status:      429,
      retry_after: remaining,
      request:     path,
      attempts:    0
    )
  end

  response = @app.call(env)
  check_throttle_percentage(env, response)
  response
rescue Errors::Throttled => e
  set_hard_circuit(path: request_path(env), retry_after: e.retry_after)
  raise
end