Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Cap the number of concurrent requests #41

Merged
merged 5 commits into from
Apr 4, 2024

Conversation

iszulcdeepsense
Copy link
Collaborator

@iszulcdeepsense iszulcdeepsense commented Mar 26, 2024

Added

Maximum number of concurrent requests can be limited by jobtype_extra.max_concurrency field:
By default, concurrent requests are unlimited. Setting max_concurrency to 1 will make the job
process requests one by one. Overdue requests will be queued and processed in order.

Having such concurrency limits may cause some requests to wait in a queue.
If a throughput is higher than the job can handle, the queue will grow indefinitely.
To prevent that, you can also set jobtype_extra.max_concurrency_queue to limit the queue size.
When the queue is full, the job will return 429 Too Many Requests status code.

Example (1 request at a time, with up to 10 requests waiting in a queue):

jobtype_extra:
  max_concurrency: 1
  max_concurrency_queue: 10

Without this setting, FastAPI uses a threadpool of 40 threads internally to handle requests using non-async endpoints.
FastAPI uses Startlette's concurrency support to run requests in a threadpool. Starlette uses anyio, which in turn has 40 as the default

@iszulcdeepsense iszulcdeepsense linked an issue Mar 26, 2024 that may be closed by this pull request
@iszulcdeepsense iszulcdeepsense merged commit 90b3e82 into master Apr 4, 2024
1 check passed
@iszulcdeepsense iszulcdeepsense deleted the 40-cap-the-number-of-concurrent-requests branch April 4, 2024 08:19
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cap the number of concurrent requests
1 participant