{"id":16377752,"url":"https://github.com/walshydev/spark-ratelimiter","last_synced_at":"2025-04-19T23:54:40.479Z","repository":{"id":130663522,"uuid":"248051764","full_name":"WalshyDev/Spark-RateLimiter","owner":"WalshyDev","description":"Easy rate-limit implementation for SparkJava. ","archived":false,"fork":false,"pushed_at":"2020-03-17T19:21:49.000Z","size":3,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-19T23:54:36.360Z","etag":null,"topics":["java","rate-limiter","rate-limiting","ratelimit","spark","sparkjava"],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/WalshyDev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-03-17T19:05:30.000Z","updated_at":"2023-12-02T08:15:28.000Z","dependencies_parsed_at":null,"dependency_job_id":"cb977a1a-2f3e-4bdc-8981-80e32392a71b","html_url":"https://github.com/WalshyDev/Spark-RateLimiter","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WalshyDev%2FSpark-RateLimiter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WalshyDev%2FSpark-RateLimiter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WalshyDev%2FSpark-RateLimiter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WalshyDev%2FSpark-RateLimiter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/WalshyDev","download_url":"https://codeload.github.com/WalshyDev/Spark-RateLimiter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249830852,"owners_count":21331357,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["java","rate-limiter","rate-limiting","ratelimit","spark","sparkjava"],"created_at":"2024-10-11T03:43:54.383Z","updated_at":"2025-04-19T23:54:40.461Z","avatar_url":"https://github.com/WalshyDev.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Spark-RateLimiter\n\n## Description\nThis is a tiny library which allows you to set up rate-limiting using SparkJava. If you run an API and need to rate-limit requests then this is the lib for you!\n\n## Usage\nThis rate-limiter will add 3 headers when a request comes in, it will then test if the max amount has been hit. If it does get it then this sends an empty `429` status code.\n\n#### Headers\nClients just need to look for and respect these headers if they do not wish to be rate-limited.\n```\nX-RateLimit-Limit: 30\nX-RateLimit-Remaining: 29\nX-RateLimit-Reset: 49999\n```\n\n### Basic\nHere's a basic usage which allows for 30 requests in a minute span using the requester IP address as the defining key.\nThis will map to all /api requests.\n```java\nprivate final RateLimiter rateLimiter = new RateLimiter(30, 1, TimeUnit.MINUTES);\n    \npublic void setup() {\n    rateLimiter.map(\"/api/*\");\n    \n    Spark.path(\"/api\", () -\u003e Spark.get(\"/\", (req, res) -\u003e \"{\\\"Hello\\\", \\\"World!\\\"}\"));\n}\n```\n\n### Defining the key\nDefining which key to use as the rate-limiter is easy. Just add a new parameter to the `RateLimiter` class constructor.\n```java\nprivate final RateLimiter rateLimiter = new RateLimiter(30, 1, TimeUnit.MINUTES, Request::queryString);\n    \npublic void setup() {\n    rateLimiter.map(\"/api/*\");\n    \n    Spark.path(\"/api\", () -\u003e Spark.get(\"/\", (req, res) -\u003e \"{\\\"Hello\\\", \\\"World!\\\"}\"));\n}\n```\n\n### Map to a specific endpoint\nTo map to a specific path rather than a group you can just use that endpoint. For example:\n```java\nprivate final RateLimiter rateLimiter = new RateLimiter(30, 1, TimeUnit.MINUTES);\n    \npublic void setup() {\n    rateLimiter.map(\"/api/some/endpoint\");\n    \n    // Since we only map to `/api/some/endpoint` this GET will not be rate-limited.\n    Spark.path(\"/api\", () -\u003e Spark.get(\"/\", (req, res) -\u003e \"{\\\"Hello\\\", \\\"World!\\\"}\"));\n}\n```\n\n## Maven/Gradle\n### Maven\n```xml\n\u003crepository\u003e\n    \u003cid\u003ejitpack.io\u003c/id\u003e\n    \u003curl\u003ehttps://jitpack.io\u003c/url\u003e\n\u003c/repository\u003e\n\n...\n\n\u003cdependency\u003e\n\t\u003cgroupId\u003ecom.github.WalshyDev\u003c/groupId\u003e\n\t\u003cartifactId\u003eSpark-RateLimiter\u003c/artifactId\u003e\n    \u003cversion\u003eVERSION\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n### Gradle\n```groovy\nrepositories {\n    maven { url 'https://jitpack.io' }\n}\n\n...\n\ndependencies {\n    implementation 'com.github.WalshyDev:Spark-RateLimiter:VERSION'\n}\n```\n\n## Cleanup\nThe `RateLimiter` uses a `ScheduledExecutorService` internally. You should stop this executor on program shutdown. To do this just call `RateLimiter#stop`","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwalshydev%2Fspark-ratelimiter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwalshydev%2Fspark-ratelimiter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwalshydev%2Fspark-ratelimiter/lists"}