Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I rate limit my iOS network requests to one second

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:

  • Assume I have an 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)
  • The same rate limit is used for each request in the queue
  • I'm looking for a solution in iOS, but general ideas might be useful

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)
  • Start/stop the queue (using KVO?) to make sure the rate limit is not exceeded
  • Special "sleep" NSOperation. This would be a task that the next network operation would be dependent upon
  • Completely ignore the rate limit and just pause a bit when you get the "exceeded rate limit" error response

These 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?

like image 403
Stephen Darlington Avatar asked Nov 05 '12 13:11

Stephen Darlington


People also ask

What is proxy rate limit?

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.

How do I fix the API rate limit exceeded?

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.

What is Rate Limit reached?

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.


1 Answers

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.

like image 58
matt Avatar answered Oct 05 '22 10:10

matt