philiprehberger-retry_kit 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '087ec5bc777335b701a4c95fed80654168c61f7f99db577dfbe11c0f548bbc90'
4
+ data.tar.gz: aa0f0381cb4d79add6829653d1aabb0d37127ae99edfe158921b7f0d30dd4a39
5
+ SHA512:
6
+ metadata.gz: 6b5d3c1f93a480f75d5007b223fdd26c24a17f5d7f428c3f094bbd54a2051635a1066fcb8b57d986628550559b77445120afe9499a916b7bfdc76f5037866849
7
+ data.tar.gz: 640c8a218b0b51fb89c4a416e6ee84b9719bcd810001f9c3a71b38458f7a801269ca16c436710a9189772d0e3a4d31ede051d33e9ba90effdfe39a114f12a584
data/CHANGELOG.md ADDED
@@ -0,0 +1,18 @@
1
+ # Changelog
2
+
3
+ All notable changes to this gem will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ## [0.1.0] - 2026-03-10
11
+
12
+ ### Added
13
+ - Initial release
14
+ - Exponential, linear, and constant backoff strategies
15
+ - Full and equal jitter modes
16
+ - Circuit breaker with configurable threshold and cooldown
17
+ - Retry executor with max attempts, retryable error filtering, and on_retry callback
18
+ - Convenience `RetryKit.run` class method
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 philiprehberger
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,142 @@
1
+ # philiprehberger-retry_kit
2
+
3
+ [![Tests](https://github.com/philiprehberger/rb-retry-kit/actions/workflows/ci.yml/badge.svg)](https://github.com/philiprehberger/rb-retry-kit/actions/workflows/ci.yml)
4
+ [![Gem Version](https://badge.fury.io/rb/philiprehberger-retry_kit.svg)](https://rubygems.org/gems/philiprehberger-retry_kit)
5
+
6
+ Retry with exponential backoff, jitter, and circuit breaker for resilient Ruby applications.
7
+
8
+ ## Requirements
9
+
10
+ - Ruby >= 3.1
11
+
12
+ ## Installation
13
+
14
+ Add to your Gemfile:
15
+
16
+ ```ruby
17
+ gem "philiprehberger-retry_kit"
18
+ ```
19
+
20
+ Then run:
21
+
22
+ ```bash
23
+ bundle install
24
+ ```
25
+
26
+ Or install directly:
27
+
28
+ ```bash
29
+ gem install philiprehberger-retry_kit
30
+ ```
31
+
32
+ ## Usage
33
+
34
+ ```ruby
35
+ require "philiprehberger/retry_kit"
36
+
37
+ # Simple retry with defaults (3 attempts, exponential backoff, full jitter)
38
+ result = Philiprehberger::RetryKit.run do
39
+ api.call
40
+ end
41
+ ```
42
+
43
+ ### Custom Options
44
+
45
+ ```ruby
46
+ Philiprehberger::RetryKit.run(
47
+ max_attempts: 5,
48
+ backoff: :exponential,
49
+ base_delay: 1,
50
+ max_delay: 60,
51
+ jitter: :equal,
52
+ on: [Net::ReadTimeout, Errno::ECONNRESET]
53
+ ) do
54
+ http_request
55
+ end
56
+ ```
57
+
58
+ ### Retry Callback
59
+
60
+ ```ruby
61
+ Philiprehberger::RetryKit.run(
62
+ max_attempts: 4,
63
+ on_retry: ->(error, attempt, delay) {
64
+ puts "Attempt #{attempt} failed: #{error.message}. Retrying in #{delay}s..."
65
+ }
66
+ ) do
67
+ flaky_operation
68
+ end
69
+ ```
70
+
71
+ ### Backoff Strategies
72
+
73
+ ```ruby
74
+ # Exponential: 0.5s, 1s, 2s, 4s, ...
75
+ Philiprehberger::RetryKit.run(backoff: :exponential)
76
+
77
+ # Linear: 0.5s, 1s, 1.5s, 2s, ...
78
+ Philiprehberger::RetryKit.run(backoff: :linear)
79
+
80
+ # Constant: 1s, 1s, 1s, ...
81
+ Philiprehberger::RetryKit.run(backoff: :constant, base_delay: 1)
82
+ ```
83
+
84
+ ### Circuit Breaker
85
+
86
+ ```ruby
87
+ breaker = Philiprehberger::RetryKit::CircuitBreaker.new(
88
+ failure_threshold: 5,
89
+ cooldown: 30
90
+ )
91
+
92
+ # Use with retry
93
+ Philiprehberger::RetryKit.run(circuit_breaker: breaker) do
94
+ external_service.call
95
+ end
96
+
97
+ # Use standalone
98
+ breaker.call { risky_operation }
99
+
100
+ # Check state
101
+ breaker.state # => :closed, :open, or :half_open
102
+ breaker.failure_count
103
+ breaker.reset
104
+ ```
105
+
106
+ ### Backoff Utilities
107
+
108
+ ```ruby
109
+ Philiprehberger::RetryKit::Backoff.exponential(3, base_delay: 0.5, max_delay: 30)
110
+ # => 4.0
111
+
112
+ Philiprehberger::RetryKit::Backoff.jitter(4.0, mode: :full)
113
+ # => 0.0..4.0 (random)
114
+ ```
115
+
116
+ ## API
117
+
118
+ | Method / Class | Description |
119
+ |----------------|-------------|
120
+ | `RetryKit.run(**options, &block)` | Execute a block with retry logic |
121
+ | `Executor.new(**options)` | Create a reusable retry executor |
122
+ | `Executor#call(&block)` | Execute the block with retries |
123
+ | `CircuitBreaker.new(failure_threshold:, cooldown:)` | Create a circuit breaker |
124
+ | `CircuitBreaker#call(&block)` | Execute through the circuit breaker |
125
+ | `CircuitBreaker#state` | Current state (`:closed`, `:open`, `:half_open`) |
126
+ | `CircuitBreaker#reset` | Reset to closed state |
127
+ | `Backoff.exponential(attempt, base_delay:, max_delay:)` | Calculate exponential delay |
128
+ | `Backoff.linear(attempt, base_delay:, max_delay:)` | Calculate linear delay |
129
+ | `Backoff.constant(attempt, delay:)` | Calculate constant delay |
130
+ | `Backoff.jitter(delay, mode:)` | Apply jitter to a delay |
131
+
132
+ ## Development
133
+
134
+ ```bash
135
+ bundle install
136
+ bundle exec rspec # Run tests
137
+ bundle exec rubocop # Check code style
138
+ ```
139
+
140
+ ## License
141
+
142
+ MIT
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Philiprehberger
4
+ module RetryKit
5
+ # Backoff strategy calculators.
6
+ module Backoff
7
+ module_function
8
+
9
+ # Exponential backoff: base_delay * 2^attempt
10
+ #
11
+ # @param attempt [Integer] the current attempt number (0-based)
12
+ # @param base_delay [Numeric] the base delay in seconds
13
+ # @param max_delay [Numeric] the maximum delay cap in seconds
14
+ # @return [Numeric] delay in seconds
15
+ def exponential(attempt, base_delay: 0.5, max_delay: 30)
16
+ delay = base_delay * (2**attempt)
17
+ [delay, max_delay].min
18
+ end
19
+
20
+ # Linear backoff: base_delay * (attempt + 1)
21
+ #
22
+ # @param attempt [Integer] the current attempt number (0-based)
23
+ # @param base_delay [Numeric] the base delay in seconds
24
+ # @param max_delay [Numeric] the maximum delay cap in seconds
25
+ # @return [Numeric] delay in seconds
26
+ def linear(attempt, base_delay: 0.5, max_delay: 30)
27
+ delay = base_delay * (attempt + 1)
28
+ [delay, max_delay].min
29
+ end
30
+
31
+ # Constant backoff: always the same delay.
32
+ #
33
+ # @param _attempt [Integer] ignored
34
+ # @param delay [Numeric] the constant delay in seconds
35
+ # @return [Numeric] delay in seconds
36
+ def constant(_attempt, delay: 1)
37
+ delay
38
+ end
39
+
40
+ # Add jitter to a delay value.
41
+ #
42
+ # @param delay [Numeric] the base delay
43
+ # @param mode [Symbol] jitter mode — :full, :equal, or :decorrelated
44
+ # @return [Float] jittered delay
45
+ def jitter(delay, mode: :full)
46
+ case mode
47
+ when :full
48
+ rand * delay
49
+ when :equal
50
+ (delay / 2.0) + (rand * delay / 2.0)
51
+ when :none
52
+ delay.to_f
53
+ else
54
+ raise ArgumentError, "Unknown jitter mode: #{mode}. Use :full, :equal, or :none"
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Philiprehberger
4
+ module RetryKit
5
+ # A simple circuit breaker that tracks failures and opens the circuit
6
+ # when a threshold is exceeded, preventing further calls until a
7
+ # cooldown period has elapsed.
8
+ class CircuitBreaker
9
+ # Raised when the circuit is open and a call is attempted.
10
+ class OpenError < Error; end
11
+
12
+ STATES = %i[closed open half_open].freeze
13
+
14
+ attr_reader :state, :failure_count, :failure_threshold, :cooldown
15
+
16
+ # @param failure_threshold [Integer] number of failures before opening
17
+ # @param cooldown [Numeric] seconds to wait before transitioning to half-open
18
+ def initialize(failure_threshold: 5, cooldown: 30)
19
+ @failure_threshold = failure_threshold
20
+ @cooldown = cooldown
21
+ @failure_count = 0
22
+ @state = :closed
23
+ @last_failure_time = nil
24
+ @mutex = Mutex.new
25
+ end
26
+
27
+ # Execute a block through the circuit breaker.
28
+ #
29
+ # @yield the block to execute
30
+ # @return the block's return value
31
+ # @raise [OpenError] if the circuit is open
32
+ def call(&block)
33
+ raise ArgumentError, "Block required" unless block
34
+
35
+ @mutex.synchronize { check_state! }
36
+
37
+ result = block.call
38
+ record_success
39
+ result
40
+ rescue OpenError
41
+ raise
42
+ rescue StandardError => e
43
+ record_failure
44
+ raise e
45
+ end
46
+
47
+ # Reset the circuit breaker to closed state.
48
+ def reset
49
+ @mutex.synchronize do
50
+ @failure_count = 0
51
+ @state = :closed
52
+ @last_failure_time = nil
53
+ end
54
+ end
55
+
56
+ private
57
+
58
+ def check_state!
59
+ case @state
60
+ when :open
61
+ unless cooldown_elapsed?
62
+ raise OpenError, "Circuit is open (#{@failure_count} failures, cooldown: #{@cooldown}s)"
63
+ end
64
+
65
+ @state = :half_open
66
+ when :half_open
67
+ # Allow one attempt through
68
+ end
69
+ end
70
+
71
+ def record_success
72
+ @mutex.synchronize do
73
+ @failure_count = 0
74
+ @state = :closed
75
+ @last_failure_time = nil
76
+ end
77
+ end
78
+
79
+ def record_failure
80
+ @mutex.synchronize do
81
+ @failure_count += 1
82
+ @last_failure_time = Time.now
83
+
84
+ @state = :open if @failure_count >= @failure_threshold
85
+ end
86
+ end
87
+
88
+ def cooldown_elapsed?
89
+ @last_failure_time && (Time.now - @last_failure_time) >= @cooldown
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Philiprehberger
4
+ module RetryKit
5
+ # Executes a block with configurable retry logic, backoff, and optional circuit breaker.
6
+ class Executor
7
+ # @param options [Hash] retry configuration options
8
+ # @option options [Integer] :max_attempts (3) maximum number of attempts
9
+ # @option options [Symbol] :backoff (:exponential) backoff strategy
10
+ # @option options [Numeric] :base_delay (0.5) base delay in seconds
11
+ # @option options [Numeric] :max_delay (30) maximum delay cap in seconds
12
+ # @option options [Symbol] :jitter (:full) jitter mode
13
+ # @option options [Array<Class>] :on ([StandardError]) exception classes to retry on
14
+ # @option options [CircuitBreaker, nil] :circuit_breaker (nil) optional circuit breaker
15
+ # @option options [Proc, nil] :on_retry (nil) callback before each retry
16
+ def initialize(**options)
17
+ @max_attempts = options.fetch(:max_attempts, 3)
18
+ @backoff = options.fetch(:backoff, :exponential)
19
+ @base_delay = options.fetch(:base_delay, 0.5)
20
+ @max_delay = options.fetch(:max_delay, 30)
21
+ @jitter = options.fetch(:jitter, :full)
22
+ @retryable_errors = Array(options.fetch(:on, [StandardError]))
23
+ @circuit_breaker = options[:circuit_breaker]
24
+ @on_retry = options[:on_retry]
25
+ end
26
+
27
+ # Execute the block with retry logic.
28
+ #
29
+ # @yield the block to execute
30
+ # @return the block's return value
31
+ # @raise the last exception if all attempts are exhausted
32
+ def call(&block)
33
+ raise ArgumentError, "Block required" unless block
34
+
35
+ attempt_with_retries(0, &block)
36
+ end
37
+
38
+ private
39
+
40
+ def attempt_with_retries(attempt, &)
41
+ execute_attempt(&)
42
+ rescue CircuitBreaker::OpenError
43
+ raise
44
+ rescue *@retryable_errors => e
45
+ raise e if attempt + 1 >= @max_attempts
46
+
47
+ delay = compute_delay(attempt)
48
+ @on_retry&.call(e, attempt + 1, delay)
49
+ sleep(delay)
50
+ attempt_with_retries(attempt + 1, &)
51
+ end
52
+
53
+ def execute_attempt(&)
54
+ if @circuit_breaker
55
+ @circuit_breaker.call(&)
56
+ else
57
+ yield
58
+ end
59
+ end
60
+
61
+ def compute_delay(attempt)
62
+ raw = backoff_delay(attempt)
63
+ Backoff.jitter(raw, mode: @jitter)
64
+ end
65
+
66
+ def backoff_delay(attempt)
67
+ case @backoff
68
+ when :exponential then Backoff.exponential(attempt, base_delay: @base_delay, max_delay: @max_delay)
69
+ when :linear then Backoff.linear(attempt, base_delay: @base_delay, max_delay: @max_delay)
70
+ when :constant then Backoff.constant(attempt, delay: @base_delay)
71
+ else raise ArgumentError, "Unknown backoff strategy: #{@backoff}"
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Philiprehberger
4
+ module RetryKit
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Philiprehberger
4
+ module RetryKit
5
+ class Error < StandardError; end
6
+
7
+ # Execute a block with retry logic.
8
+ #
9
+ # @param options [Hash] options passed to Executor.new
10
+ # @yield the block to execute
11
+ # @return the block's return value
12
+ # @see Executor#initialize for available options
13
+ def self.run(**options, &)
14
+ Executor.new(**options).call(&)
15
+ end
16
+ end
17
+ end
18
+
19
+ require_relative "retry_kit/version"
20
+ require_relative "retry_kit/backoff"
21
+ require_relative "retry_kit/circuit_breaker"
22
+ require_relative "retry_kit/executor"
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: philiprehberger-retry_kit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Philip Rehberger
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2026-03-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A lightweight retry library with exponential/linear/constant backoff,
14
+ configurable jitter strategies, and an optional circuit breaker for resilient Ruby
15
+ applications.
16
+ email:
17
+ - me@philiprehberger.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - CHANGELOG.md
23
+ - LICENSE
24
+ - README.md
25
+ - lib/philiprehberger/retry_kit.rb
26
+ - lib/philiprehberger/retry_kit/backoff.rb
27
+ - lib/philiprehberger/retry_kit/circuit_breaker.rb
28
+ - lib/philiprehberger/retry_kit/executor.rb
29
+ - lib/philiprehberger/retry_kit/version.rb
30
+ homepage: https://github.com/philiprehberger/rb-retry-kit
31
+ licenses:
32
+ - MIT
33
+ metadata:
34
+ homepage_uri: https://github.com/philiprehberger/rb-retry-kit
35
+ source_code_uri: https://github.com/philiprehberger/rb-retry-kit
36
+ changelog_uri: https://github.com/philiprehberger/rb-retry-kit/blob/main/CHANGELOG.md
37
+ rubygems_mfa_required: 'true'
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 3.1.0
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 3.5.22
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: Retry with exponential backoff, jitter, and circuit breaker
57
+ test_files: []