Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/duron/contrib/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from typing import TYPE_CHECKING, cast

if TYPE_CHECKING:
from collections.abc import AsyncGenerator
from collections.abc import AsyncGenerator, Generator
from io import IOBase

from duron.log import BaseEntry, Entry
Expand Down Expand Up @@ -73,10 +73,17 @@ def __init__(self, log_file: str | Path) -> None:
self._lock = asyncio.Lock()

async def stream(self) -> AsyncGenerator[tuple[int, BaseEntry], None]:
# The file read + yield lives in a synchronous generator so that
# GeneratorExit-driven cleanup of the `with` block is deterministic
# (an async generator abandoned mid-iteration may skip cleanup).
for item in self._read_entries():
yield item

def _read_entries(self) -> Generator[tuple[int, BaseEntry], None, None]:
if not self._log_file.exists():
return

with Path(self._log_file).open("rb") as f: # noqa: ASYNC230
with Path(self._log_file).open("rb") as f:
# Read existing lines from start offset
while True:
line_start_offset = f.tell()
Expand Down
Loading