Rate limiting prevents an API from being saturated with requests, ensuring that the service provided is not degraded. Rate limiting is a tactic commonly employed by an API but can also be employed by a client that consumes from an API. We might see the latter scenario for an application consuming from a third-party API that imposes request-rate restrictions with punitive measures for consumers routinely in violation of the given restrictions. Recently, I encountered such a scenario.

A visualization of a rate-limited API client
A visualization of a rate-limited API client
Figure 1: A visualization of a rate-limited API client

Precedence for a Rate-Limited API Client

Some time ago, I started work on a personal project: a web app that displays match statistics about highly ranked players in a competitive video game. The statistics displayed on the web app are produced by a .NET Core console app. The console app runs on an hourly interval, pulling the latest match data from the game’s public API, aggregating the data, and subsequently storing it for later use by the web app. The game’s API imposes request-rate restrictions with the typical limitations seen for a public API: a consumer should not exceed X requests over Y seconds. Applications that are routinely in violation of the rate limit are blocked from accessing the API. Given that my console app attempts to pull all of the latest match data every hour, as quickly as possible, I set out to implement a rate limiter for the app to prevent it from being penalized by the API. …


In the first article of this series, we implemented a rate limiter in .NET Core using the sliding window algorithm with the intention that it will be used by an application to self-limit its requests to an API. Before we can put our implementation into practice, we first need to validate that it functions as intended.

In this article, we will discuss how to validate our rate limiter with unit tests. We will first setup the test class and several helper methods that can be reused throughout our unit tests. We will then turn our attention towards the unit tests, examining each based on the scenario that they assess; this is done similarly to how we implemented the rate limiter in the previous article. …

Gopherchucks

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store