Class: ClickhouseRuby::RetryHandler
- Inherits:
-
Object
- Object
- ClickhouseRuby::RetryHandler
- Defined in:
- lib/clickhouse_ruby/retry_handler.rb
Overview
Implements retry logic with exponential backoff and jitter
This class handles transient failures in ClickHouse connections by retrying with an exponential backoff strategy and optional jitter.
Constant Summary collapse
- RETRIABLE_ERRORS =
Errors that should trigger a retry
[ ConnectionError, ConnectionTimeout, ConnectionNotEstablished, PoolTimeout, ].freeze
- RETRIABLE_HTTP_CODES =
HTTP status codes that should trigger a retry
%w[500 502 503 504 429].freeze
Instance Attribute Summary collapse
-
#initial_backoff ⇒ Float
readonly
Initial backoff delay in seconds.
-
#jitter ⇒ Symbol
readonly
Jitter strategy (:full, :equal, or :none).
-
#max_attempts ⇒ Integer
readonly
Maximum number of attempts.
-
#max_backoff ⇒ Float
readonly
Maximum backoff delay in seconds.
-
#multiplier ⇒ Float
readonly
Exponential backoff multiplier.
Instance Method Summary collapse
-
#initialize(max_attempts: 3, initial_backoff: 1.0, max_backoff: 120.0, multiplier: 1.6, jitter: :equal) ⇒ RetryHandler
constructor
Creates a new RetryHandler.
-
#retriable?(error) ⇒ Boolean
Checks if an error is retriable.
-
#with_retry(idempotent: true, query_id: nil) {|query_id| ... } ⇒ Object
Executes a block with retry logic.
Constructor Details
#initialize(max_attempts: 3, initial_backoff: 1.0, max_backoff: 120.0, multiplier: 1.6, jitter: :equal) ⇒ RetryHandler
Creates a new RetryHandler
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/clickhouse_ruby/retry_handler.rb', line 55 def initialize( max_attempts: 3, initial_backoff: 1.0, max_backoff: 120.0, multiplier: 1.6, jitter: :equal ) @max_attempts = max_attempts @initial_backoff = initial_backoff @max_backoff = max_backoff @multiplier = multiplier @jitter = jitter end |
Instance Attribute Details
#initial_backoff ⇒ Float (readonly)
Returns initial backoff delay in seconds.
37 38 39 |
# File 'lib/clickhouse_ruby/retry_handler.rb', line 37 def initial_backoff @initial_backoff end |
#jitter ⇒ Symbol (readonly)
Returns jitter strategy (:full, :equal, or :none).
46 47 48 |
# File 'lib/clickhouse_ruby/retry_handler.rb', line 46 def jitter @jitter end |
#max_attempts ⇒ Integer (readonly)
Returns maximum number of attempts.
34 35 36 |
# File 'lib/clickhouse_ruby/retry_handler.rb', line 34 def max_attempts @max_attempts end |
#max_backoff ⇒ Float (readonly)
Returns maximum backoff delay in seconds.
40 41 42 |
# File 'lib/clickhouse_ruby/retry_handler.rb', line 40 def max_backoff @max_backoff end |
#multiplier ⇒ Float (readonly)
Returns exponential backoff multiplier.
43 44 45 |
# File 'lib/clickhouse_ruby/retry_handler.rb', line 43 def multiplier @multiplier end |
Instance Method Details
#retriable?(error) ⇒ Boolean
Checks if an error is retriable
112 113 114 115 |
# File 'lib/clickhouse_ruby/retry_handler.rb', line 112 def retriable?(error) RETRIABLE_ERRORS.any? { |klass| error.is_a?(klass) } || retriable_http_error?(error) end |
#with_retry(idempotent: true, query_id: nil) {|query_id| ... } ⇒ Object
Executes a block with retry logic
Yields to the block with an optional query_id. If the block raises a retriable error, retries with exponential backoff up to max_attempts. Non-retriable errors are re-raised immediately.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/clickhouse_ruby/retry_handler.rb', line 88 def with_retry(idempotent: true, query_id: nil) attempts = 0 generated_query_id = query_id || SecureRandom.uuid begin attempts += 1 yield(generated_query_id) rescue *RETRIABLE_ERRORS => e handle_retry(attempts, e, idempotent) retry rescue QueryError => e # Check if HTTP code is retriable (server error or rate limit) if retriable_http_error?(e) handle_retry(attempts, e, idempotent) retry end raise end end |