Skip to content

feat: migrate database access to async with aiosqlite#10

Merged
victor1234 merged 5 commits intomasterfrom
feature/add-async
Aug 26, 2025
Merged

feat: migrate database access to async with aiosqlite#10
victor1234 merged 5 commits intomasterfrom
feature/add-async

Conversation

@victor1234
Copy link
Owner

@victor1234 victor1234 commented Aug 26, 2025

What

Added async database support using aiosqlite and converted the API, services, and DB access layer to fully asynchronous.

Why

Synchronous SQLite access was blocking the FastAPI event loop and limiting concurrency. Async queries allow the server to handle more requests efficiently without blocking I/O.

How

  • Add aiosqlite dependency in pyproject.toml and poetry.lock.
  • Replace sqlite3 with aiosqlite and introduced an asynccontextmanager for DB connections.
  • Convert DB functions (get_book_title, get_book_file_path, get_cover_path, get_authors, etc.) to async.
  • Update service functions (generate_*_feed, search_books, etc.) to async and awaited DB calls.
  • Make API endpoints (download_book, get_cover, search, root_by_*, etc.) async.
  • Update /ready probe to use async DB check.

@victor1234 victor1234 requested a review from Copilot August 26, 2025 01:39
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR converts the OPDS server from synchronous to asynchronous operations by replacing sqlite3 with aiosqlite and making all database-related functions async. The migration enables better concurrent handling of multiple requests and improved performance.

Key changes:

  • Migrated from sqlite3 to aiosqlite for async database operations
  • Converted all database access functions to async/await pattern
  • Updated API endpoints to use async handlers

Reviewed Changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/opds_server/db/access.py Complete migration from sqlite3 to aiosqlite with async context managers and cursor handling
src/opds_server/services/opds.py Converted feed generation functions to async and added await calls to database operations
src/opds_server/api/catalog.py Updated API endpoint handlers to async and added await calls to service functions
src/opds_server/main.py Updated health check endpoint to use async database connection
pyproject.toml Added aiosqlite dependency

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +133 to +134
) as cursor:
async for book_id, author_id, name in cursor:
Copy link

Copilot AI Aug 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using async for to iterate over cursor results may not be supported by aiosqlite. Consider using await cursor.fetchall() and then iterating over the results with a regular for loop.

Suggested change
) as cursor:
async for book_id, author_id, name in cursor:
rows = await cursor.fetchall()
for book_id, author_id, name in rows:

Copilot uses AI. Check for mistakes.
Comment on lines +164 to +165
) as cursor:
async for book_id, file_format, filename in cursor:
Copy link

Copilot AI Aug 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using async for to iterate over cursor results may not be supported by aiosqlite. Consider using await cursor.fetchall() and then iterating over the results with a regular for loop.

Suggested change
) as cursor:
async for book_id, file_format, filename in cursor:
rows = await cursor.fetchall()
for book_id, file_format, filename in rows:

Copilot uses AI. Check for mistakes.
@victor1234 victor1234 changed the title feat: convert to async feat: migrate database access to async with aiosqlite Aug 26, 2025
@victor1234 victor1234 merged commit 5d1bef8 into master Aug 26, 2025
1 check passed
@victor1234 victor1234 deleted the feature/add-async branch August 26, 2025 12:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants