Class: SagaForge::CompositeRetryPolicy
- Inherits:
-
Object
- Object
- SagaForge::CompositeRetryPolicy
- Defined in:
- lib/saga_forge/composite_retry_policy.rb
Overview
An ordered list of RetryPolicy objects, each scoped to an error type via
its retry_on. On failure the first policy whose retry_on matches the
raised error (by is_a?) is applied, giving each error type its own
independent attempt budget and backoff curve. Put specific policies first
and a catch-all (retry_on: nil) last; an unmatched error is not retried.
Pure: it never reads storage. The per-error count is supplied by the caller through the block passed to #retry_backoff, keyed by the matched policy's budget_key (its declared errors) — the caller is expected to persist counts per budget_key, e.g. in Event#retry_budgets.
Instance Attribute Summary collapse
-
#policies ⇒ Object
readonly
Returns the value of attribute policies.
Instance Method Summary collapse
-
#initialize(policies) ⇒ CompositeRetryPolicy
constructor
A new instance of CompositeRetryPolicy.
-
#max_attempts ⇒ Object
Coarsest attempt bound across sub-policies, for a safety-net guard.
-
#policy_for(error) ⇒ Object
First sub-policy whose retry_on matches the error, or nil.
-
#retry_backoff(error, attempts:) ⇒ Object
Routes on the live error and delegates the decision to the matched sub-policy.
Constructor Details
#initialize(policies) ⇒ CompositeRetryPolicy
Returns a new instance of CompositeRetryPolicy.
17 18 19 20 21 22 |
# File 'lib/saga_forge/composite_retry_policy.rb', line 17 def initialize(policies) @policies = Array(policies) if @policies.empty? raise ArgumentError, "composite retry policy needs at least one policy" end end |
Instance Attribute Details
#policies ⇒ Object (readonly)
Returns the value of attribute policies.
15 16 17 |
# File 'lib/saga_forge/composite_retry_policy.rb', line 15 def policies @policies end |
Instance Method Details
#max_attempts ⇒ Object
Coarsest attempt bound across sub-policies, for a safety-net guard. nil (unbounded) if any sub-policy is unbounded.
43 44 45 46 |
# File 'lib/saga_forge/composite_retry_policy.rb', line 43 def max_attempts caps = @policies.map(&:max_attempts) caps.include?(nil) ? nil : caps.max end |
#policy_for(error) ⇒ Object
First sub-policy whose retry_on matches the error, or nil.
25 26 27 |
# File 'lib/saga_forge/composite_retry_policy.rb', line 25 def policy_for(error) @policies.find { |p| p.matches?(error) } end |
#retry_backoff(error, attempts:) ⇒ Object
Routes on the live error and delegates the decision to the matched
sub-policy. When a block is given it is called with the matched policy's
budget_key and must return that policy's running attempt count (1-based,
including the current failure); otherwise attempts is used.
33 34 35 36 37 38 39 |
# File 'lib/saga_forge/composite_retry_policy.rb', line 33 def retry_backoff(error, attempts:) sub = policy_for(error) return nil if sub.nil? count = block_given? ? yield(sub.budget_key) : attempts sub.retryable?(error, count) ? sub.backoff_for(count) : nil end |