mirror of
https://github.com/arcan1s/ahriman.git
synced 2026-02-24 21:59:48 +00:00
fix: handle class overrides for retry polices correctly
Previous implementation didn't work as intended because there was still override in init. Current implementation instead of playing with guessing separates default and instance setttings. Also update test cases to handle this scenario correctly
This commit is contained in:
@@ -22,7 +22,7 @@ import sys
|
|||||||
|
|
||||||
from functools import cached_property
|
from functools import cached_property
|
||||||
from requests.adapters import BaseAdapter, HTTPAdapter
|
from requests.adapters import BaseAdapter, HTTPAdapter
|
||||||
from typing import Any, IO, Literal
|
from typing import Any, ClassVar, IO, Literal
|
||||||
from urllib3.util.retry import Retry
|
from urllib3.util.retry import Retry
|
||||||
|
|
||||||
from ahriman import __version__
|
from ahriman import __version__
|
||||||
@@ -39,14 +39,18 @@ class SyncHttpClient(LazyLogging):
|
|||||||
wrapper around requests library to reduce boilerplate
|
wrapper around requests library to reduce boilerplate
|
||||||
|
|
||||||
Attributes:
|
Attributes:
|
||||||
|
DEFAULT_MAX_RETRIES(int): (class attribute) default maximum amount of retries
|
||||||
|
DEFAULT_RETRY_BACKOFF(float): (class attribute) default retry exponential backoff
|
||||||
|
DEFAULT_TIMEOUT(int | None): (class attribute) default HTTP request timeout in seconds
|
||||||
auth(tuple[str, str] | None): HTTP basic auth object if set
|
auth(tuple[str, str] | None): HTTP basic auth object if set
|
||||||
retry(Retry): retry policy of the HTTP client. Disabled by default
|
retry(Retry): retry policy of the HTTP client
|
||||||
suppress_errors(bool): suppress logging of request errors
|
suppress_errors(bool): suppress logging of request errors
|
||||||
timeout(int | None): HTTP request timeout in seconds
|
timeout(int | None): HTTP request timeout in seconds
|
||||||
"""
|
"""
|
||||||
|
|
||||||
retry: Retry = Retry()
|
DEFAULT_MAX_RETRIES: ClassVar[int] = 0
|
||||||
timeout: int | None = None
|
DEFAULT_RETRY_BACKOFF: ClassVar[float] = 0.0
|
||||||
|
DEFAULT_TIMEOUT: ClassVar[int | None] = 30
|
||||||
|
|
||||||
def __init__(self, configuration: Configuration | None = None, section: str | None = None, *,
|
def __init__(self, configuration: Configuration | None = None, section: str | None = None, *,
|
||||||
suppress_errors: bool = False) -> None:
|
suppress_errors: bool = False) -> None:
|
||||||
@@ -65,10 +69,10 @@ class SyncHttpClient(LazyLogging):
|
|||||||
|
|
||||||
self.suppress_errors = suppress_errors
|
self.suppress_errors = suppress_errors
|
||||||
|
|
||||||
self.timeout = configuration.getint(section, "timeout", fallback=30)
|
self.timeout = configuration.getint(section, "timeout", fallback=self.DEFAULT_TIMEOUT)
|
||||||
self.retry = SyncHttpClient.retry_policy(
|
self.retry = SyncHttpClient.retry_policy(
|
||||||
max_retries=configuration.getint(section, "max_retries", fallback=0),
|
max_retries=configuration.getint(section, "max_retries", fallback=self.DEFAULT_MAX_RETRIES),
|
||||||
retry_backoff=configuration.getfloat(section, "retry_backoff", fallback=0.0),
|
retry_backoff=configuration.getfloat(section, "retry_backoff", fallback=self.DEFAULT_RETRY_BACKOFF),
|
||||||
)
|
)
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
|
|||||||
@@ -88,11 +88,9 @@ class Repository(Executor, UpdateHandler):
|
|||||||
Args:
|
Args:
|
||||||
configuration(Configuration): configuration instance
|
configuration(Configuration): configuration instance
|
||||||
"""
|
"""
|
||||||
AUR.timeout = configuration.getint("aur", "timeout", fallback=30)
|
AUR.DEFAULT_MAX_RETRIES = configuration.getint("aur", "max_retries", fallback=0)
|
||||||
AUR.retry = AUR.retry_policy(
|
AUR.DEFAULT_RETRY_BACKOFF = configuration.getfloat("aur", "retry_backoff", fallback=0.0)
|
||||||
max_retries=configuration.getint("aur", "max_retries", fallback=0),
|
AUR.DEFAULT_TIMEOUT = configuration.getint("aur", "timeout", fallback=30)
|
||||||
retry_backoff=configuration.getfloat("aur", "retry_backoff", fallback=0.0),
|
|
||||||
)
|
|
||||||
|
|
||||||
def _set_context(self) -> None:
|
def _set_context(self) -> None:
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -49,14 +49,18 @@ def test_retry_policy() -> None:
|
|||||||
"""
|
"""
|
||||||
must set retry policy
|
must set retry policy
|
||||||
"""
|
"""
|
||||||
SyncHttpClient.retry = SyncHttpClient.retry_policy(1, 2.0)
|
SyncHttpClient.DEFAULT_MAX_RETRIES = 1
|
||||||
AUR.retry = AUR.retry_policy(3, 4.0)
|
SyncHttpClient.DEFAULT_RETRY_BACKOFF = 2.0
|
||||||
|
AUR.DEFAULT_MAX_RETRIES = 3
|
||||||
|
AUR.DEFAULT_RETRY_BACKOFF = 4.0
|
||||||
|
|
||||||
assert SyncHttpClient.retry.connect == 1
|
client = SyncHttpClient()
|
||||||
assert SyncHttpClient.retry.backoff_factor == 2.0
|
assert client.retry.connect == 1
|
||||||
|
assert client.retry.backoff_factor == 2.0
|
||||||
|
|
||||||
assert AUR.retry.connect == 3
|
aur = AUR()
|
||||||
assert AUR.retry.backoff_factor == 4.0
|
assert aur.retry.connect == 3
|
||||||
|
assert aur.retry.backoff_factor == 4.0
|
||||||
|
|
||||||
|
|
||||||
def test_exception_response_text() -> None:
|
def test_exception_response_text() -> None:
|
||||||
|
|||||||
@@ -31,8 +31,9 @@ def test_set_globals(configuration: Configuration) -> None:
|
|||||||
configuration.set_option("aur", "max_retries", "10")
|
configuration.set_option("aur", "max_retries", "10")
|
||||||
|
|
||||||
Repository._set_globals(configuration)
|
Repository._set_globals(configuration)
|
||||||
assert AUR.timeout == 42
|
aur = AUR()
|
||||||
assert AUR.retry.connect == 10
|
assert aur.timeout == 42
|
||||||
|
assert aur.retry.connect == 10
|
||||||
|
|
||||||
|
|
||||||
def test_set_context(configuration: Configuration, database: SQLite, mocker: MockerFixture) -> None:
|
def test_set_context(configuration: Configuration, database: SQLite, mocker: MockerFixture) -> None:
|
||||||
|
|||||||
Reference in New Issue
Block a user