Module: Backup::Backblaze::Retry
- Defined in:
- lib/backup/backblaze/retry.rb
Defined Under Namespace
Classes: RetrySequence, TooManyRetries
Constant Summary collapse
- MAX_RETRIES =
5
Class Method Summary collapse
Class Method Details
.call(retries, backoff, api_call_name, &blk) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/backup/backblaze/retry.rb', line 31 module_function def call retries, backoff, api_call_name, &blk raise TooManyRetries, "max tries is #{MAX_RETRIES}" unless retries < MAX_RETRIES # default exponential backoff for retries > 0 backoff ||= retries ** 2 # minor avoidance of unnecessary work in sleep if there's no backoff needed. if backoff > 0 ::Backup::Logger.info "calling #{api_call_name} retry #{retries} after sleep #{backoff}" sleep backoff else ::Backup::Logger.info "calling #{api_call_name}" end # Finally! Do the call. blk.call rescue Excon::Errors::HTTPStatusError => ex Backup::Logger.info ex. # backoff can end up nil, if Retry-After isn't specified. backoff = ex.response.headers['Retry-After']&.to_i ::Backup::Logger.info "server specified Retry-After of #{backoff.inspect}" raise "Retry-After #{backoff} > 60 is too long" if backoff && backoff > 60 # need to get code from body body_wrap = HashWrap.from_json ex.response.body # figure out which retry sequence to use recovery_sequence = RetryLookup.retry_sequence api_call_name, ex.response.status, body_wrap.code # There's a sequence of retries, and we don't know how to hook the # return values and parameters together. So make that someone else's # problem. # # TODO possibly just execute the retry sequence here? # That's quite hard cos it will have to have access to the calling self if recovery_sequence.any? ::Backup::Logger.info "recovery sequence of #{recovery_sequence.inspect}" raise RetrySequence.new(recovery_sequence, backoff) else raise end rescue Excon::Errors::Error => ex Backup::Logger.info ex. # Socket errors etc therefore no http status code and no response body. # So just retry with default exponential backoff. call retries + 1, nil, api_call_name, &blk end |