Connecting to Rate Limited Services in ColdFusion

I often need connect to APIs. These APIs often have usage limitations. Specifically, they have rate limits. That is to say limits on the rate (frequency) with which you can use their APIs.

This means that any code that calls an API needs to know what do in the event that it is at risk of violating that Rate Limit - ideally before (and instead of) doing so.

To solve that problem, I wrote RateLimiter.cfc. It runs on top of MrECache, but has no other external dependencies.

Instantiating RateLimiter is simple:

Variables.RateLimiter = new com.sebtools.RateLimiter();

To use it, you first need to write a CFC method that needs to be rate limited. When dealing with an external API, I write a CFC method that calls the external API. Then you call it through RateLimiter instead of calling it directly.

Then call the "method" method with the following arguments:

  • id (string, required): A unique identifier for that which is being rate-limited.
  • component (required): A reference to the CFC holding the method to call.
  • MethodName (string, required): The name of the method to call.
  • Args (struct, optional): A structure of arguments to pass to the argument.
  • default (optional): If provided, the value to return if the method is not callable due to the rate limite
  • waitlimit (numeric, default=100): Maximum number of milliseconds to wait.
  • waitstep (numeric, default=20): Milliseconds to wait between checks.

That's the basics of it. There are also a few utility methods to get information about an id. All of them take a single "id" argument:

  • calling: Indicates if the given method is currently being called.
  • isCallable: Indicates if the given method can be called right now.

That's about it.

RateLimiter is part of my com.sebtools package. It is a free ColdFusion package and you can read more about it in my com.sebtools blog series. Check it out!

If you need any help with com.sebtools, or ColdFusion in general, let me know.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.