Class: InfluxDB2::WriteRetry
- Inherits:
-
Object
- Object
- InfluxDB2::WriteRetry
- Defined in:
- lib/influxdb2/client/write_retry.rb
Overview
Exponential random write retry.
Instance Method Summary collapse
- #_connection_error(error) ⇒ Object
- #get_backoff_time(attempts) ⇒ Object
-
#initialize(options = {}) ⇒ WriteRetry
constructor
A new instance of WriteRetry.
-
#retry ⇒ Object
Runs the supplied code block with a exponential backoff retry strategy.
Constructor Details
#initialize(options = {}) ⇒ WriteRetry
Returns a new instance of WriteRetry.
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/influxdb2/client/write_retry.rb', line 33 def initialize( = {}) @api_client = [:api_client] @max_retries = [:max_retries] || 5 raise "#{@error_msg_prefix} :max_retries must be greater than 0." unless @max_retries > 0 @retry_interval = [:retry_interval] || 5_000 @max_retry_delay = [:max_retry_delay] || 125_000 @max_retry_time = [:max_retry_time] || 180_000 @exponential_base = [:exponential_base] || 2 @jitter_interval = [:jitter_interval] || 0 raise "#{@error_msg_prefix} :retry_interval cannot be greater than :max_retry_delay." if @retry_interval > @max_retry_delay end |
Instance Method Details
#_connection_error(error) ⇒ Object
100 101 102 |
# File 'lib/influxdb2/client/write_retry.rb', line 100 def _connection_error(error) InfluxError::HTTP_ERRORS.any? { |c| error.instance_of? c } end |
#get_backoff_time(attempts) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/influxdb2/client/write_retry.rb', line 47 def get_backoff_time(attempts) range_start = @retry_interval range_stop = @retry_interval * @exponential_base i = 1 while i < attempts i += 1 range_start = range_stop range_stop *= @exponential_base break if range_stop > @max_retry_delay end range_stop = @max_retry_delay if range_stop > @max_retry_delay range_start + (range_stop - range_start) * rand end |
#retry ⇒ Object
Runs the supplied code block with a exponential backoff retry strategy.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/influxdb2/client/write_retry.rb', line 64 def retry raise "#{@error_msg_prefix} must be passed a block" unless block_given? attempts = 0 start_time = Time.now begin attempts += 1 yield attempts rescue InfluxError => e if attempts > @max_retries @api_client.log(:error, 'Maximum retry attempts reached.') raise e end if (Time.now - start_time) * 1000 > @max_retry_time @api_client.log(:error, "Maximum retry time #{@max_retry_time} ms exceeded") raise e end raise e if (e.code.nil? || e.code.to_i < 429) && !_connection_error(e.original) timeout = if e.retry_after.nil? || e.retry_after.empty? get_backoff_time(attempts) else (e.retry_after.to_f * 1000) + @jitter_interval * rand end = 'The retriable error occurred during writing of data. '\ "Reason: '#{e.}'. Retry in: #{timeout.to_f / 1000}s." @api_client.log(:warn, ) sleep timeout / 1000 retry end end |