Skip to content

Commit

Permalink
acme modules: also support 503 for retries (#513)
Browse files Browse the repository at this point in the history
* Also support 503 for retries.

* Forgot to adjust status code comparison.

* Also support 408.
  • Loading branch information
felixfontein authored Sep 21, 2022
1 parent e656570 commit c24e5c6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/513-acme-503.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- "acme* modules - also support the HTTP 503 Service Unavailable and 408 Request Timeout response status for automatic retries (https://github.com/ansible-collections/community.crypto/pull/513)."
8 changes: 6 additions & 2 deletions plugins/module_utils/acme/acme.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,22 @@
IPADDRESS_IMPORT_ERROR = None


RETRY_STATUS_CODES = (408, 429, 503)


def _decode_retry(module, response, info, retry_count):
if info['status'] != 429:
if info['status'] not in RETRY_STATUS_CODES:
return False

if retry_count >= 5:
raise ACMEProtocolException(module, msg='Giving up after 5 retries', info=info, response=response)

# 429 and 503 should have a Retry-After header (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After)
try:
retry_after = min(max(1, int(info.get('retry-after'))), 60)
except (TypeError, ValueError) as dummy:
retry_after = 10
module.log('Retrieved a 429 Too Many Requests on %s, retrying in %s seconds' % (info['url'], retry_after))
module.log('Retrieved a %d HTTP status on %s, retrying in %s seconds' % (info['status'], info['url'], retry_after))

time.sleep(retry_after)
return True
Expand Down

0 comments on commit c24e5c6

Please sign in to comment.