Skip to content

Add request throttling support with a default implementation.#1

Merged
TimMikeladze merged 5 commits into
TimMikeladze:mainfrom
NyaStone:feature/request-queue-default-implementation
Oct 6, 2025
Merged

Add request throttling support with a default implementation.#1
TimMikeladze merged 5 commits into
TimMikeladze:mainfrom
NyaStone:feature/request-queue-default-implementation

Conversation

@NyaStone

@NyaStone NyaStone commented Oct 4, 2025

Copy link
Copy Markdown
Contributor

What:

Added support for fetch request throttling to simplify handling the YGOPRODeck API rate limit, including a default implementation.

Why:

Implementing throttling within the client is necessary to exclude cache hits from the throttling logic.

How:

TimeQueue interface

Introduced a TimeQueue interface to describe a queue/throttler.
The only required method is:

enqueue<T>(task: () => Promise<T>, signal: AbortSignal): Promise<T>

This method should delay the task if necessary, resolve with the task result, and be abortable via the provided signal.

  • Updated the YgoApi constructor options to allow passing a TimeQueue.
  • Updated the request method of YgoApi to enqueue fetch tasks and await them instead of running instantly.

Default implementation: SignalBasedQueue

TL;DR of the implementation:

  • When a task is enqueued, it stores an AbortController to signal when the task is ready to run. The task is then executed, and its resolution is returned.
  • A loop signals the next task at regular intervals.
    The interval is measured between fetch starts, not between one resolving and the next starting.
  • Provides an asynchronous method that resolves when the queue is available.

Added a test suite for the SignalBasedQueue implementation.

Checklist:

  • Documentation
  • Tests
  • Ready to be merged

@TimMikeladze

Copy link
Copy Markdown
Owner

@NyaStone Thank you for the PR! Could you please rebase and resolve conflicts? I'll merge and publish right away.

@NyaStone NyaStone force-pushed the feature/request-queue-default-implementation branch from 81b7ce1 to b13a3f1 Compare October 5, 2025 22:19
@NyaStone

NyaStone commented Oct 5, 2025

Copy link
Copy Markdown
Contributor Author

The PR has been rebased, and should be ready to be merged.

@TimMikeladze TimMikeladze merged commit 0f3edfe into TimMikeladze:main Oct 6, 2025
3 checks passed
@TimMikeladze

Copy link
Copy Markdown
Owner

Published as 1.2.0. Thanks!

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