Skip to content

Commit

Permalink
Make ExponentialBackoff and LinearBackoff require keyword arguments
Browse files Browse the repository at this point in the history
The `ExponentialBackoff` and `LinearBackoff` classes now require keyword
arguments for their constructor. This change was made to make the
classes easier to use and to avoid confusion with the order of the
arguments.

Signed-off-by: Leandro Lucarella <[email protected]>
  • Loading branch information
llucax committed Oct 3, 2024
1 parent e3ef0fb commit 09e8b16
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

- `GrpcStreamBroadcaster` now takes a `AsyncIterable` instead of a `AsyncIterator` as the `stream_method`. This is to match the type of streaming methods generated by `grpc`, so no conversion to an `AsyncIterator` is needed.
- gRPC URLs don't have a default port anymore, unless a default is set via `ChannelOptions`. If you want to set a default port for URLs, please pass custom `ChannelOptions` as `defaults` to `parse_grpc_uri` or as `channel_defaults` to `BaseApiClient`.
* The `ExponentialBackoff` and `LinearBackoff` classes now require keyword arguments for their constructor. This change was made to make the classes easier to use and to avoid confusion with the order of the arguments.

## New Features

Expand Down
5 changes: 3 additions & 2 deletions src/frequenz/client/base/retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class LinearBackoff(Strategy):

def __init__(
self,
*,
interval: float = DEFAULT_RETRY_INTERVAL,
jitter: float = DEFAULT_RETRY_JITTER,
limit: int | None = None,
Expand Down Expand Up @@ -124,9 +125,9 @@ class ExponentialBackoff(Strategy):
DEFAULT_MULTIPLIER = 2.0
"""Default multiplier for exponential increment."""

# pylint: disable=too-many-arguments
def __init__(
def __init__( # pylint: disable=too-many-arguments
self,
*,
initial_interval: float = DEFAULT_INTERVAL,
max_interval: float = DEFAULT_MAX_INTERVAL,
multiplier: float = DEFAULT_MULTIPLIER,
Expand Down
14 changes: 9 additions & 5 deletions tests/retry_strategy/test_retry_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_no_limit(self) -> None:

def test_iter(self) -> None:
"""Test iterator."""
assert list(retry.LinearBackoff(1, 0, 3)) == [1, 1, 1]
assert list(retry.LinearBackoff(interval=1, jitter=0, limit=3)) == [1, 1, 1]

def test_with_limit(self) -> None:
"""Test limit works."""
Expand Down Expand Up @@ -89,7 +89,7 @@ def test_with_jitter_with_limit(self) -> None:

def test_deep_copy(self) -> None:
"""Test if deep copies are really deep copies."""
strategy = retry.LinearBackoff(1.0, 0.0, 2)
strategy = retry.LinearBackoff(interval=1.0, jitter=0.0, limit=2)

copy1 = strategy.copy()
assert copy1.next_interval() == 1.0
Expand All @@ -108,7 +108,9 @@ class TestExponentialBackoff:

def test_no_limit(self) -> None:
"""Test base case."""
strategy = retry.ExponentialBackoff(3, 30, 2, 0.0)
strategy = retry.ExponentialBackoff(
initial_interval=3, max_interval=30, multiplier=2, jitter=0.0
)

assert strategy.next_interval() == 3.0
assert strategy.next_interval() == 6.0
Expand All @@ -119,7 +121,7 @@ def test_no_limit(self) -> None:

def test_with_limit(self) -> None:
"""Test limit works."""
strategy = retry.ExponentialBackoff(3, jitter=0.0, limit=3)
strategy = retry.ExponentialBackoff(initial_interval=3, jitter=0.0, limit=3)

assert strategy.next_interval() == 3.0
assert strategy.next_interval() == 6.0
Expand All @@ -128,7 +130,9 @@ def test_with_limit(self) -> None:

def test_deep_copy(self) -> None:
"""Test if deep copies are really deep copies."""
strategy = retry.ExponentialBackoff(3.0, 30.0, 2, 0.0, 2)
strategy = retry.ExponentialBackoff(
initial_interval=3.0, max_interval=30.0, multiplier=2, jitter=0.0, limit=2
)

copy1 = strategy.copy()
assert copy1.next_interval() == 3.0
Expand Down

0 comments on commit 09e8b16

Please sign in to comment.