Skip to content
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

sdk: Allow to limit the number of concurrent network requests #3625

Conversation

gnunicorn
Copy link
Contributor

@gnunicorn gnunicorn commented Jun 28, 2024

Add a new max_concurrent_requests parameter in the RequestConfig limits the number of http(s) requests the internal sdk client issues concurrently (if > 0). The default behavior is the same as before: there is no limit on concurrent requests issued.

This is especially useful for resource constrained platforms (e.g. mobile platforms), and if your pattern might lead to issuing many requests at the same time (like downloading and caching all avatars at startup).

  • Public API changes documented in changelogs (optional)

Signed-off-by: Benjamin Kampmann [email protected]

@gnunicorn gnunicorn requested a review from a team as a code owner June 28, 2024 16:34
@gnunicorn gnunicorn requested review from poljar and removed request for a team June 28, 2024 16:34
Copy link

codecov bot commented Jun 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 84.21%. Comparing base (6464d21) to head (52c5387).
Report is 83 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3625      +/-   ##
==========================================
- Coverage   84.22%   84.21%   -0.02%     
==========================================
  Files         256      256              
  Lines       26551    26565      +14     
==========================================
+ Hits        22363    22372       +9     
- Misses       4188     4193       +5     

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

Copy link
Member

@bnjbvr bnjbvr left a comment

Choose a reason for hiding this comment

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

I like the implementation; just a few nits here and there. Thanks!

crates/matrix-sdk/src/config/request.rs Outdated Show resolved Hide resolved
crates/matrix-sdk/src/http_client/mod.rs Outdated Show resolved Hide resolved
@bnjbvr
Copy link
Member

bnjbvr commented Jul 1, 2024

(Also would be nice to test, but likely it's a lot of bother for little value, so not mandatory. For this one time :))

@bnjbvr bnjbvr removed the request for review from poljar July 1, 2024 11:16
@gnunicorn gnunicorn requested a review from bnjbvr July 1, 2024 12:33
Copy link
Member

@bnjbvr bnjbvr left a comment

Choose a reason for hiding this comment

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

Thanks for adding a test, this is on the right track! Might need a tweak to the test, a bit of Clippy and cargo xtask fixup style, and that should be good to go.

async fn acquire(&self) -> MaybeSemaphorePermit<'_> {
match self.0.as_ref() {
Some(inner) => {
// ignoring errors as we never close this
Copy link
Member

Choose a reason for hiding this comment

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

What kind of errors do you mean here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If the semaphore has been closed, this returns an AcquireError.

This only ever happens when there the Semphore was explicitly closed, which this MaybeSemaphore doesn't ever do. So the error can never occur and can be ignored.

Copy link
Member

Choose a reason for hiding this comment

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

Can you please update the comment to make this clear?

crates/matrix-sdk/src/http_client/mod.rs Outdated Show resolved Hide resolved
crates/matrix-sdk/src/http_client/mod.rs Show resolved Hide resolved
crates/matrix-sdk/src/http_client/mod.rs Outdated Show resolved Hide resolved
crates/matrix-sdk/src/http_client/mod.rs Outdated Show resolved Hide resolved
@gnunicorn gnunicorn requested a review from bnjbvr July 2, 2024 11:00
Copy link
Member

@bnjbvr bnjbvr left a comment

Choose a reason for hiding this comment

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

New test failure when the test is running in the super-slow code coverage run: https://github.com/matrix-org/matrix-rust-sdk/actions/runs/9760610496/job/26939845059?pr=3625#step:9:1663

Hywan
Hywan previously requested changes Jul 3, 2024
async fn acquire(&self) -> MaybeSemaphorePermit<'_> {
match self.0.as_ref() {
Some(inner) => {
// ignoring errors as we never close this
Copy link
Member

Choose a reason for hiding this comment

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

Can you please update the comment to make this clear?

crates/matrix-sdk/src/http_client/mod.rs Outdated Show resolved Hide resolved
@gnunicorn
Copy link
Contributor Author

@bnjbvr yeah, looks like that 300ms wasn't long enough. I suppose the only way to fix it is to increase it to 1s again for that second test :( .

 assertion `left == right` failed: Not all requests passed through
  left: 163
 right: 254

@gnunicorn gnunicorn requested review from bnjbvr and Hywan July 4, 2024 13:03
Copy link
Member

@bnjbvr bnjbvr left a comment

Choose a reason for hiding this comment

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

Sweet, thanks!

@bnjbvr bnjbvr changed the title Allow to limit the number of concurrent requests made by the sdk sdk: Allow to limit the number of concurrent network requests Jul 4, 2024
@bnjbvr bnjbvr enabled auto-merge (squash) July 4, 2024 13:59
@bnjbvr bnjbvr removed the request for review from Hywan July 4, 2024 14:01
@bnjbvr bnjbvr dismissed Hywan’s stale review July 4, 2024 14:01

i reviewed

@bnjbvr bnjbvr merged commit d49cb54 into matrix-org:main Jul 4, 2024
40 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.

3 participants