Skip to content

Conversation

CalvoM
Copy link
Owner

@CalvoM CalvoM commented Aug 1, 2025

We have better performance switching to aiohttp.

Summary by Sourcery

Migrate chess.com PGN fetching to asynchronous aiohttp calls with concurrency control and retry logic, update Celery task to run the async function, and DRY up Redis URI configuration for Celery.

New Features:

  • Convert get_chess_dot_com_games to an async function using aiohttp for non-blocking HTTP requests
  • Introduce fetch_archive coroutine with rate-limiting handling and a semaphore to limit concurrent connections

Enhancements:

  • Define a REDIS_URI constant and reuse it for Celery result backend configuration

We have better performance switching to aiohttp.
Copy link

sourcery-ai bot commented Aug 1, 2025

Reviewer's Guide

The PR replaces blocking requests-based PGN fetching with an asynchronous aiohttp-driven workflow, introducing a fetch_archive helper with rate-limit handling, concurrency limits via semaphores and a TCPConnector, adapts the Celery task to invoke the async function with asyncio.run, and centralizes Redis broker/backend configuration in a single URI.

Sequence diagram for asynchronous PGN fetching with aiohttp

sequenceDiagram
    participant CeleryTask as Celery Task
    participant Asyncio as asyncio
    participant ClientSession as aiohttp.ClientSession
    participant ChessComAPI as chess.com API
    CeleryTask->>Asyncio: asyncio.run(get_chess_dot_com_games(username))
    Asyncio->>ClientSession: Create session with TCPConnector(limit=15)
    loop For each archive
        Asyncio->>ClientSession: session.get(archive_url/pgn)
        alt 429 Rate Limited
            ClientSession-->>Asyncio: status 429, Retry-After header
            Asyncio->>Asyncio: await asyncio.sleep(Retry-After)
            Asyncio->>ClientSession: Retry session.get(archive_url/pgn)
        else 200 OK
            ClientSession-->>Asyncio: status 200, PGN text
        else Other error
            ClientSession-->>Asyncio: status != 200/429
        end
    end
    Asyncio-->>CeleryTask: Return concatenated PGNs
Loading

Class diagram for updated PGN fetching utilities

classDiagram
    class fetch_archive {
        +async fetch_archive(archive_url, session, semaphore)
    }
    class get_chess_dot_com_games {
        +async get_chess_dot_com_games(username)
    }
    fetch_archive <.. get_chess_dot_com_games : uses
Loading

Class diagram for Celery task update

classDiagram
    class pgn_get_chess_com_games_by_user {
        +pgn_get_chess_com_games_by_user(session_id, username)
    }
    class get_chess_dot_com_games {
        +async get_chess_dot_com_games(username)
    }
    pgn_get_chess_com_games_by_user ..> get_chess_dot_com_games : calls via asyncio.run
Loading

File-Level Changes

Change Details Files
Replace synchronous requests calls in PGN utils with an async aiohttp workflow
  • Extract fetch_archive helper handling 429 retries and backoff
  • Wrap calls in an asyncio.Semaphore and TCPConnector limit
  • Convert get_chess_dot_com_games to async, spawn fetch tasks, and gather results
  • Change all_pgns from str accumulation to list aggregation and join
style_predictor/apis/pgn/utils.py
Consolidate Redis connection string in settings
  • Add REDIS_URI environment-based constant
  • Point CELERY_RESULT_BACKEND to reuse REDIS_URI instead of duplicating the literal
my_chess_style/settings/base.py
Adapt Celery task to run the new async fetch function
  • Wrap get_chess_dot_com_games call in asyncio.run to execute coroutine
style_predictor/tasks.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@codecov-commenter
Copy link

codecov-commenter commented Aug 1, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 0% with 50 lines in your changes missing coverage. Please review.
✅ Project coverage is 36.19%. Comparing base (5cd1375) to head (615f456).

Files with missing lines Patch % Lines
style_predictor/apis/pgn/utils.py 0.00% 48 Missing ⚠️
style_predictor/tasks.py 0.00% 2 Missing ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #12      +/-   ##
==========================================
- Coverage   36.88%   36.19%   -0.70%     
==========================================
  Files          42       42              
  Lines        1041     1061      +20     
  Branches       99      100       +1     
==========================================
  Hits          384      384              
- Misses        641      661      +20     
  Partials       16       16              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @CalvoM - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments

### Comment 1
<location> `style_predictor/apis/pgn/utils.py:88` </location>
<code_context>


-def get_chess_dot_com_games(username: str) -> str:
+async def fetch_archive(
+    archive_url: str, session: aiohttp.ClientSession, semaphore: asyncio.Semaphore
+):
+    async with semaphore:
+        while True:
+            async with session.get(f"{archive_url}/pgn") as resp:
+                if resp.status == 429:
+                    retry_after = resp.headers.get("Retry-After")
+                    if retry_after:
+                        LOG.info(f"Rate limited. Retrying after {retry_after} seconds.")
+                        await asyncio.sleep(int(retry_after))
+                    else:
+                        # Implement exponential backoff here if no Retry-After
+                        await asyncio.sleep(45)  # Example fixed delay
+                elif resp.status == 200:
+                    return await resp.text()
+                else:
+                    LOG.error(f"Failed to fetch {archive_url}: {resp.status}")
</code_context>

<issue_to_address>
No timeout or error handling for network failures in fetch_archive.

Currently, network errors like timeouts or aiohttp.ClientError are not handled, which may cause hangs or crashes. Please add appropriate exception handling and consider implementing retries or returning an empty string on failure.
</issue_to_address>

### Comment 2
<location> `style_predictor/apis/pgn/utils.py:130` </location>
<code_context>
+            for archive in archives.get("archives", [])
+        ]
+        all_pgns = await asyncio.gather(*tasks)
+    return "\n\n".join(all_pgns)
</code_context>

<issue_to_address>
Empty strings from failed fetches will be included in the result.

Filter out empty strings from all_pgns before joining to avoid extra blank lines in the result.
</issue_to_address>

### Comment 3
<location> `style_predictor/tasks.py:314` </location>
<code_context>
 @shared_task(name=constants.GET_CHESS_COM_TASK)
 def pgn_get_chess_com_games_by_user(session_id: UUID, username: str):
     """Celery task to get chess games for user from chess.com."""
-    pgn_data: str = get_chess_dot_com_games(username)
+    pgn_data: str = asyncio.run(get_chess_dot_com_games(username))
     return save_file_and_queue_task(
         session_id, username, pgn_data, FileSource.CHESSDOTCOM
</code_context>

<issue_to_address>
Using asyncio.run in a Celery task may cause issues if an event loop is already running.

In environments where an event loop is already running, asyncio.run will fail. Consider alternatives like nest_asyncio or running the async code in a separate thread to ensure compatibility.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@CalvoM CalvoM force-pushed the fix_faster_requests branch from 5c173f4 to 615f456 Compare August 2, 2025 22:01
@CalvoM CalvoM merged commit 6efa186 into main Aug 2, 2025
2 checks passed
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