|
| 1 | +# The MIT License |
| 2 | +# |
| 3 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +# of this software and associated documentation files (the "Software"), to deal |
| 5 | +# in the Software without restriction, including without limitation the rights |
| 6 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +# copies of the Software, and to permit persons to whom the Software is |
| 8 | +# furnished to do so, subject to the following conditions: |
| 9 | +# |
| 10 | +# The above copyright notice and this permission notice shall be included in |
| 11 | +# all copies or substantial portions of the Software. |
| 12 | +# |
| 13 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +# THE SOFTWARE. |
| 20 | + |
| 21 | +module InfluxDB2 |
| 22 | + # Exponential random write retry. |
| 23 | + class WriteRetry |
| 24 | + @error_msg_prefix = 'Error with options to with_retries:' |
| 25 | + |
| 26 | + # @param [Hash] options the retry options. |
| 27 | + # @option options [Integer] :max_retries (5) The maximum number of times to run the block. |
| 28 | + # @option options [Integer] :retry_interval (5000) number of milliseconds to retry unsuccessful write. |
| 29 | + # @option options [Integer] :max_retry_delay (125_000) maximum delay when retrying write in milliseconds. |
| 30 | + # @option options [Integer] :max_retry_time (180_000) maximum total retry timeout in milliseconds. |
| 31 | + # @option options [Integer] :exponential_base base for the exponential retry delay |
| 32 | + # @option options [Integer] :jitter_delay random milliseconds added to write interval |
| 33 | + def initialize(options = {}) |
| 34 | + @api_client = options[:api_client] |
| 35 | + @max_retries = options[:max_retries] || 5 |
| 36 | + raise "#{@error_msg_prefix} :max_retries must be greater than 0." unless @max_retries > 0 |
| 37 | + |
| 38 | + @retry_interval = options[:retry_interval] || 5_000 |
| 39 | + @max_retry_delay = options[:max_retry_delay] || 125_000 |
| 40 | + @max_retry_time = options[:max_retry_time] || 180_000 |
| 41 | + @exponential_base = options[:exponential_base] || 2 |
| 42 | + @jitter_interval = options[:jitter_interval] || 0 |
| 43 | + raise "#{@error_msg_prefix} :retry_interval cannot be greater than :max_retry_delay." if |
| 44 | + @retry_interval > @max_retry_delay |
| 45 | + end |
| 46 | + |
| 47 | + def get_backoff_time(attempts) |
| 48 | + range_start = @retry_interval |
| 49 | + range_stop = @retry_interval * @exponential_base |
| 50 | + |
| 51 | + i = 1 |
| 52 | + while i < attempts |
| 53 | + i += 1 |
| 54 | + range_start = range_stop |
| 55 | + range_stop *= @exponential_base |
| 56 | + break if range_stop > @max_retry_delay |
| 57 | + end |
| 58 | + |
| 59 | + range_stop = @max_retry_delay if range_stop > @max_retry_delay |
| 60 | + range_start + (range_stop - range_start) * rand |
| 61 | + end |
| 62 | + |
| 63 | + # Runs the supplied code block with a exponential backoff retry strategy. |
| 64 | + def retry |
| 65 | + raise "#{@error_msg_prefix} must be passed a block" unless block_given? |
| 66 | + |
| 67 | + attempts = 0 |
| 68 | + start_time = Time.now |
| 69 | + begin |
| 70 | + attempts += 1 |
| 71 | + yield attempts |
| 72 | + rescue InfluxError => e |
| 73 | + if attempts > @max_retries |
| 74 | + @api_client.log(:error, 'Maximum retry attempts reached.') |
| 75 | + raise e |
| 76 | + end |
| 77 | + |
| 78 | + if (Time.now - start_time) * 1000 > @max_retry_time |
| 79 | + @api_client.log(:error, "Maximum retry time #{@max_retry_time} ms exceeded") |
| 80 | + raise e |
| 81 | + end |
| 82 | + |
| 83 | + raise e if (e.code.nil? || e.code.to_i < 429) && !_connection_error(e.original) |
| 84 | + |
| 85 | + timeout = if e.retry_after.nil? || e.retry_after.empty? |
| 86 | + get_backoff_time(attempts) |
| 87 | + else |
| 88 | + (e.retry_after.to_f * 1000) + @jitter_interval * rand |
| 89 | + end |
| 90 | + |
| 91 | + message = 'The retriable error occurred during writing of data. '\ |
| 92 | + "Reason: '#{e.message}'. Retry in: #{timeout.to_f / 1000}s." |
| 93 | + |
| 94 | + @api_client.log(:warn, message) |
| 95 | + sleep timeout / 1000 |
| 96 | + retry |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + def _connection_error(error) |
| 101 | + InfluxError::HTTP_ERRORS.any? { |c| error.instance_of? c } |
| 102 | + end |
| 103 | + end |
| 104 | +end |
0 commit comments