Many, if not most, web services have a rate limit for clients. Delicious says a client can make one request per second; Twitter has limits per end-point; I'm sure Facebook and Flickr and Foursquare have their own idea.
You can easily limit an iOS application to a single request at a time using an NSOperationQueue
.
But how do you limit an application to making, say, only one request per second?
I've looked at the sample code by Apple, AFNetworking, ASINetwork and a few others, and none seem to solve this problem. This seems odd to me. I'll concede that I could be missing something very obvious...
Some parameters:
NSOperationQueue
for network operations and the request is an NSOperation
(could also be a GCD queue I suppose, but this is what I've mostly been working with)Possible solutions:
sleep
statement in the NSOperation
(it's a queue/thread so this wouldn't block anything else)NSTimer
in the NSOperation
performSelector:
in the NSOperation
(I patched ASINetworking to use this approach, though I'm not using it and didn't push the change upstream)NSOperation
. This would be a task that the next network operation would be dependent uponThese all seem quite messy. Operations that sleep would likely prevent forms of "priority" queue. Starting/stopping the queue seems fragile. Ignoring the limit is rude.
To be clear, I have solved this problem. But the solution seems "messy" and somewhat fragile. I'd like to know if there's a better, cleaner option.
Ideas?
Web reverse proxies allow incoming requests to be limited based on a user-defined set of criteria and policy. Rate limiting achieves the following protections: Brute force attacks on sensitive information such as passwords or PINs.
Resolve a 403 error: Project rate limit exceededRaise the per-user quota in the Google Cloud project. For more information, request a quota increase. Batch requests to make fewer API calls. Use exponential backoff to retry the request.
A rate limit is the number of API calls an app or user can make within a given time period. If this limit is exceeded or if CPU or total time limits are exceeded, the app or user may be throttled.
New in iOS 13, this functionality is built in. Pass your communication trigger through the Combine framework's debounce
operator and you're all set.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With