Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[FrameworkBundle][HttpClient] AddThrottlingHttpClient to limit requests within a timeframe#53550

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
fabpot merged 1 commit intosymfony:7.1fromHypeMC:add-throttling-httpclient
Feb 3, 2024

Conversation

@HypeMC
Copy link
Member

@HypeMCHypeMC commentedJan 16, 2024
edited
Loading

QA
Branch?7.1
Bug fix?no
New feature?yes
Deprecations?no
Issues-
LicenseMIT

This PR adds a simpleThrottlingHttpClient to help with limiting the number of requests within a certain period.

Simple example, don't send more than 10 requests in 5 second:

framework:http_client:scoped_clients:ping.client:base_uri:'http://localhost:8080'rate_limiter:http_throttlingrate_limiter:http_throttling:policy:'token_bucket'limit:10rate:{ interval: '5 seconds', amount: 10 }
#[AsCommand('app:ping')]class PingCommandextends Command{publicfunction__construct(        #[Target('ping.client')]privateHttpClientInterface$httpClient,    ) {parent::__construct();    }protectedfunctionexecute(InputInterface$input,OutputInterface$output):int    {$requests = [];for ($i =0;$i <100;$i++) {$requests[] =$this->httpClient->request('GET','/ping');        }foreach ($requestsas$request) {$output->writeln($request->getContent());        }return Command::SUCCESS;    }}

Receiving controller:

#[AsController]class PingController{    #[Route('/ping', name:'app_ping')]publicfunction__invoke():Response    {returnnewResponse((new \DateTime())->format('Y-m-d\TH:i:s.u'));    }}

Output:

$ bin/console app:ping2024-01-16T11:52:54.9225972024-01-16T11:52:54.9648512024-01-16T11:52:55.0095042024-01-16T11:52:55.0539862024-01-16T11:52:55.0982672024-01-16T11:52:55.1396212024-01-16T11:52:55.1827692024-01-16T11:52:55.2644332024-01-16T11:52:55.3047752024-01-16T11:52:55.2238052024-01-16T11:52:59.8778582024-01-16T11:52:59.9147242024-01-16T11:52:59.9506102024-01-16T11:52:59.9868182024-01-16T11:53:00.0256382024-01-16T11:53:00.0636872024-01-16T11:53:00.8798502024-01-16T11:53:00.9196512024-01-16T11:53:00.9592732024-01-16T11:53:00.9993812024-01-16T11:53:04.8901262024-01-16T11:53:04.9651162024-01-16T11:53:04.9999162024-01-16T11:53:05.0377802024-01-16T11:53:05.0739992024-01-16T11:53:05.1102412024-01-16T11:53:05.9170452024-01-16T11:53:05.9716722024-01-16T11:53:06.0160252024-01-16T11:53:06.0592202024-01-16T11:53:09.9200922024-01-16T11:53:09.9747572024-01-16T11:53:10.0104062024-01-16T11:53:10.0457902024-01-16T11:53:10.0817492024-01-16T11:53:10.1181002024-01-16T11:53:10.9244852024-01-16T11:53:10.9810942024-01-16T11:53:11.0239352024-01-16T11:53:11.067210...

mdeboer, 7-zete-7, qdequippe, and ivanelez reacted with heart emoji
@carsonbotcarsonbot added this to the7.1 milestoneJan 16, 2024
@HypeMCHypeMCforce-pushed theadd-throttling-httpclient branch 2 times, most recently from8c8ad49 toe70c356CompareJanuary 16, 2024 12:02
@HypeMCHypeMCforce-pushed theadd-throttling-httpclient branch 3 times, most recently from5b676f0 toe554083CompareJanuary 16, 2024 18:23
@HypeMCHypeMCforce-pushed theadd-throttling-httpclient branch 4 times, most recently from27cfc2c to8d43eb7CompareJanuary 22, 2024 11:40
@nicolas-grekas
Copy link
Member

Can you please help me get the steps that achieve the throttling? Which part is sleeping especially? The lock is, right?
Do we need to play with the pause handler then? I think this can be checked by comparing how curl vs native http clients behave with the current code.

@HypeMC
Copy link
MemberAuthor

@nicolas-grekas Actually no,$this->rateLimiter()->reserve()->getWaitDuration() doesn't sleep, it just returns the expected delay between the requests. The pause handler is what actually sleeps. This behavior is consistent across both the curl and native http clients. Maybe I'm wrong, but if the lock was what slept, then that would mean that the creation of the response objects was delayed, but not the actual requests which are triggered afterwards.

Copy link
Member

@nicolas-grekasnicolas-grekas 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 the insights, makes sense now.

@HypeMCHypeMCforce-pushed theadd-throttling-httpclient branch 2 times, most recently from84c8927 to1a8b068CompareJanuary 23, 2024 09:55
@fabpotfabpotforce-pushed theadd-throttling-httpclient branch from1a8b068 to5bb5474CompareFebruary 3, 2024 13:13
@fabpot
Copy link
Member

Thank you@HypeMC.

@fabpotfabpot merged commitf0f3040 intosymfony:7.1Feb 3, 2024
@HypeMCHypeMC deleted the add-throttling-httpclient branchFebruary 3, 2024 14:10
@fabpotfabpot mentioned this pull requestMay 2, 2024
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@OskarStarkOskarStarkOskarStark left review comments

@stofstofstof requested changes

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

Assignees

No one assigned

Projects

None yet

Milestone

7.1

Development

Successfully merging this pull request may close these issues.

6 participants

@HypeMC@nicolas-grekas@fabpot@stof@OskarStark@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp