Skip to content

Commit b89dd37

Browse files
committed
simplify batcher and remove pydantic requirement
1 parent 080a8b4 commit b89dd37

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

localstack-core/localstack/utils/batching.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
import time
55
from typing import Generic, Protocol, TypeVar, overload
66

7-
from pydantic import Field
8-
from pydantic.dataclasses import dataclass
9-
107
LOG = logging.getLogger(__name__)
118

129
T = TypeVar("T")
@@ -16,7 +13,6 @@
1613

1714

1815
# TODO: Add batching on bytes as well.
19-
@dataclass
2016
class Batcher(Generic[T]):
2117
"""
2218
A utility for collecting items into batches and flushing them when one or more batch policy conditions are met.
@@ -51,14 +47,33 @@ class Batcher(Generic[T]):
5147
assert batcher.flush() == ["item1", "item2", "item3", "item4"]
5248
"""
5349

54-
max_count: int | None = Field(default=None, description="Maximum number of items", ge=0)
55-
max_window: float | None = Field(
56-
default=None, description="Maximum time window in seconds", ge=0
57-
)
50+
max_count: int | None
51+
"""
52+
Maximum number of items, must be None or positive.
53+
"""
54+
55+
max_window: float | None
56+
"""
57+
Maximum time window in seconds, must be None or positive.
58+
"""
59+
60+
_triggered: bool
61+
_last_batch_time: float
62+
_batch: list[T]
5863

59-
_triggered: bool = Field(default=False, init=False)
60-
_last_batch_time: float = Field(default_factory=time.monotonic, init=False)
61-
_batch: list[T] = Field(default_factory=list, init=False)
64+
def __init__(self, max_count: int | None = None, max_window: float | None = None):
65+
"""
66+
Initialize a new Batcher instance.
67+
68+
:param max_count: Maximum number of items that be None or positive.
69+
:param max_window: Maximum time window in seconds that must be None or positive.
70+
"""
71+
self.max_count = max_count
72+
self.max_window = max_window
73+
74+
self._triggered = False
75+
self._last_batch_time = time.monotonic()
76+
self._batch = []
6277

6378
@property
6479
def period(self) -> float:

0 commit comments

Comments
 (0)