mirror of
https://github.com/ansible-collections/hetzner.hcloud
synced 2024-12-12 05:22:31 +00:00
refactor: reuse exponential_backoff_function
from hcloud-python (#535)
Replace the local function with the exponential_backoff_function from hcloud-python
This commit is contained in:
parent
b9a1509378
commit
c665629f7e
3 changed files with 7 additions and 40 deletions
|
@ -3,7 +3,6 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from contextlib import contextmanager
|
||||
from random import random
|
||||
|
||||
from ansible.module_utils.basic import missing_required_lib
|
||||
|
||||
|
@ -107,22 +106,3 @@ class Client(ClientBase):
|
|||
yield
|
||||
finally:
|
||||
self._requests_session = requests.Session()
|
||||
|
||||
|
||||
def exponential_backoff_poll_interval(*, base: float, multiplier: int, cap: float, jitter: float):
|
||||
"""
|
||||
Return a poll interval function, implementing a truncated exponential backoff with jitter.
|
||||
|
||||
:param base: Base for the exponential backoff algorithm.
|
||||
:param multiplier: Multiplier for the exponential backoff algorithm.
|
||||
:param cap: Value at which the interval is truncated.
|
||||
:param jitter: Proportion of the interval to add as random jitter.
|
||||
"""
|
||||
|
||||
def func(retries: int) -> float:
|
||||
interval = base * multiplier**retries # Exponential backoff
|
||||
interval = min(cap, interval) # Cap backoff
|
||||
interval += random() * interval * jitter # Add jitter
|
||||
return interval
|
||||
|
||||
return func
|
||||
|
|
|
@ -15,13 +15,13 @@ from ansible.module_utils.common.validation import (
|
|||
check_required_one_of,
|
||||
)
|
||||
|
||||
from .client import (
|
||||
ClientException,
|
||||
client_check_required_lib,
|
||||
client_get_by_name_or_id,
|
||||
exponential_backoff_poll_interval,
|
||||
from .client import ClientException, client_check_required_lib, client_get_by_name_or_id
|
||||
from .vendor.hcloud import (
|
||||
APIException,
|
||||
Client,
|
||||
HCloudException,
|
||||
exponential_backoff_function,
|
||||
)
|
||||
from .vendor.hcloud import APIException, Client, HCloudException
|
||||
from .vendor.hcloud.actions import ActionException
|
||||
from .version import version
|
||||
|
||||
|
@ -87,7 +87,7 @@ class AnsibleHCloud:
|
|||
application_name="ansible-module",
|
||||
application_version=version,
|
||||
# Total waiting time before timeout is > 117.0
|
||||
poll_interval=exponential_backoff_poll_interval(base=1.0, multiplier=2, cap=5.0, jitter=0.5),
|
||||
poll_interval=exponential_backoff_function(base=1.0, multiplier=2, cap=5.0),
|
||||
poll_max_retries=25,
|
||||
)
|
||||
|
||||
|
|
|
@ -1,14 +1 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from ansible_collections.hetzner.hcloud.plugins.module_utils.client import (
|
||||
exponential_backoff_poll_interval,
|
||||
)
|
||||
|
||||
|
||||
def test_exponential_backoff_poll_interval():
|
||||
poll_interval = exponential_backoff_poll_interval(base=1.0, multiplier=2, cap=5.0, jitter=0.0)
|
||||
poll_max_retries = 25
|
||||
|
||||
results = [poll_interval(i) for i in range(poll_max_retries)]
|
||||
assert sum(results) == 117.0
|
||||
assert results[:6] == [1.0, 2.0, 4.0, 5.0, 5.0, 5.0]
|
||||
|
|
Loading…
Reference in a new issue