fix: free unused journal.Reader instances (#158)

I'm a bit dumb and forgot to add code that frees journal.Reader instances from memory.

Co-authored-by: nhnn <nhnn@disroot.org>
Co-authored-by: Inex Code <inex.code@selfprivacy.org>
Reviewed-on: https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api/pulls/158
Reviewed-by: Inex Code <inex.code@selfprivacy.org>
Co-authored-by: nhnn <nhnn@noreply.git.selfprivacy.org>
Co-committed-by: nhnn <nhnn@noreply.git.selfprivacy.org>
This commit is contained in:
nhnn 2024-11-27 12:32:01 +02:00 committed by Inex Code
parent 848befe3f1
commit 5aa1a378ef
2 changed files with 22 additions and 8 deletions

View file

@ -21,11 +21,17 @@ async def log_stream() -> AsyncGenerator[LogEntry, None]:
asyncio.get_event_loop().add_reader(j, lambda: asyncio.ensure_future(callback()))
while True:
entry = await queue.get()
try:
yield LogEntry(entry)
except Exception:
asyncio.get_event_loop().remove_reader(j)
return
queue.task_done()
try:
while True:
entry = await queue.get()
try:
yield LogEntry(entry)
except Exception:
asyncio.get_event_loop().remove_reader(j)
j.close()
return
queue.task_done()
except asyncio.CancelledError:
asyncio.get_event_loop().remove_reader(j)
j.close()
return

View file

@ -42,6 +42,8 @@ def get_paginated_logs(
events = get_events_from_journal(j, limit, lambda j: j.get_previous())
events.reverse()
j.close()
return events
elif up_cursor is None and down_cursor is not None:
j.seek_cursor(down_cursor)
@ -50,6 +52,8 @@ def get_paginated_logs(
events = get_events_from_journal(j, limit, lambda j: j.get_previous())
events.reverse()
j.close()
return events
elif up_cursor is not None and down_cursor is None:
j.seek_cursor(up_cursor)
@ -57,8 +61,12 @@ def get_paginated_logs(
events = get_events_from_journal(j, limit, lambda j: j.get_next())
j.close()
return events
else:
j.close()
raise NotImplementedError(
"Pagination by both up_cursor and down_cursor is not implemented"
)