Doing request rate limiting with NSURLSession
Doing request rate limiting with NSURLSession
- Subject: Doing request rate limiting with NSURLSession
- From: Sebastien Boisvert <email@hidden>
- Date: Tue, 09 Oct 2018 19:01:06 +0000 (UTC)
I have a need to limit the rate at which requests are made to a service to a
specific limit, let's say max 32/sec, evenly distributed, so a minimum delay of
1/32 sec between requests.
I have tried to adding an appropriate pause between initiating
NSURLSessionTasks created for a NSURLSession (pausing before calling -resume on
the task), but I suspect that the system will schedule the actual execution of
the tasks' request whenever it deems appropriate, which might be shorter than
the 1/32 sec delay I want.
I was wondering what might be the best way to achieve this. My thinking is
using a custom NSURLProtocol where I'd use (yet another) NSURLSession to
control either the number of active tasks (though that probably still wouldn't
allow exact control of the delay), and/or use a delegate call (such as the -...
didReceiveChallenge:... one) to add the delay before calling the completion
handler to delay the completion of the task (but again I'm not really sure that
would give the fine-grained control I want).
Might there be a better/simpler way?
(Note that the idea of using a NSURLProtocol would be to add it to the
configuration of a specific NSURLSessionConfiguration (via -protocolClasses) in
specific instances where I want to have this rate limiting, instead of
implementing this rate limiting globally - basically I'd like to have a
'bolt-on' solution I could use where I need to rate-limit requests on a
session).
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden