Skip to content

Limit concurrent HTTP requests #922

New issue

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

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

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Ralith
Copy link
Contributor

@Ralith Ralith commented Apr 27, 2025

When building a non-trivial project with third-party dependencies fetched via http_archive or similar, e.g. from reindeer in non-vendored mode, buck2 can generate extremely large numbers of outgoing HTTP requests. Hyper does not enforce any limits itself, happily expanding its connection pool with every additional concurrent request. This can cause the remote HTTP server to reject requests, and even lead to buck2 itself failing with "too many open files" errors.

Larger numbers of concurrent requests have rapidly diminishing returns, so while there's no obviously correct limit, any smallish number should improve behavior in most cases. It may even improve total throughput by reducing contention for network bandwidth.

See also discussion in facebookincubator/reindeer#46.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 27, 2025
@facebook-github-bot
Copy link
Contributor

@facebook-github-bot has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. (Because this pull request was imported automatically, there will not be any future comments.)

@Ralith
Copy link
Contributor Author

Ralith commented Apr 27, 2025

cc @cormacrelf

@Ralith Ralith force-pushed the push-vrzyztmsxkup branch 5 times, most recently from d9909c9 to 57a8fac Compare April 27, 2025 21:07
@Ralith
Copy link
Contributor Author

Ralith commented Apr 27, 2025

Reworked this to reduce the boilerplate and ensure the semaphore permit is held until the response is fully consumed. Hopefully that doesn't confuse the import?

@Ralith Ralith force-pushed the push-vrzyztmsxkup branch 3 times, most recently from 03cf79a to fcba6c4 Compare April 27, 2025 22:03
@Ralith Ralith force-pushed the push-vrzyztmsxkup branch from fcba6c4 to 9786a5c Compare April 27, 2025 22:35
Comment on lines +144 to +147
.inspect(move |_| {
// Ensure we keep a concurrent request permit alive until the stream is consumed
let _guard = &semaphore_guard;
})
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a little cheeky and might be clearer as a stream transformer struct, but that would probably take about 4x as much code. Let me know if that'd be preferred.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants