Mocking a rate limiting endpoint 429
Ever have some client code that needs to call another endpoint which rate limits you? Join the club. I recently did, and honestly I didn’t care at all about the ONE particular rest client that needed to backoff appropriately when the 429s were being barfed back; instead what I did care about was implementing a generic rate limiting engine that could function in front that one specific client, and be used generically for many different clients/use-cases and throttle threads before they even got to the client code in anticipation of a rate limit being reached.
In the process of writing that gating code (which ended up using a distributed semaphore of sorts that spanned across many nodes in a cluster) I ended up writing this simple mock endpoint who could be configured to return 429′s and a Retry-After header (i.e. RFC 3261) after some threshold of requests per timeframe was reached. I had to use this in order to properly be able to test the distributed gating code which could limit the total number of threads operating across clients spanning many nodes. Its simple and completely customizable. Maybe will be of use to someone else.
Check it out here: https://github.com/bitsofinfo/mock-rate-limiting-endpoint
Originally published at bitsofinfo.wordpress.com on December 21, 2018.