{"id":16660443,"url":"https://github.com/killme2008/clj-rate-limiter","last_synced_at":"2025-03-21T17:31:25.424Z","repository":{"id":33540716,"uuid":"37186847","full_name":"killme2008/clj-rate-limiter","owner":"killme2008","description":"Rate limiter for clojure that supports a rolling window, either in-memory or backed by redis","archived":false,"fork":false,"pushed_at":"2019-04-15T03:06:11.000Z","size":24,"stargazers_count":22,"open_issues_count":1,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-04-23T20:53:40.375Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Clojure","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"epl-1.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/killme2008.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-06-10T09:08:23.000Z","updated_at":"2023-11-17T21:27:50.000Z","dependencies_parsed_at":"2022-08-03T15:18:41.109Z","dependency_job_id":null,"html_url":"https://github.com/killme2008/clj-rate-limiter","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/killme2008%2Fclj-rate-limiter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/killme2008%2Fclj-rate-limiter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/killme2008%2Fclj-rate-limiter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/killme2008%2Fclj-rate-limiter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/killme2008","download_url":"https://codeload.github.com/killme2008/clj-rate-limiter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244838208,"owners_count":20518804,"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":[],"created_at":"2024-10-12T10:29:18.630Z","updated_at":"2025-03-21T17:31:25.035Z","avatar_url":"https://github.com/killme2008.png","language":"Clojure","funding_links":[],"categories":[],"sub_categories":[],"readme":"# clj-rate-limiter\n\nRate limiter in clojure supports a rolling window,either in-memory or backed by redis.\n\nIt's transformed from [rolling-rate-limiter](https://github.com/classdojo/rolling-rate-limiter) in node.js, but does some performance tuning for redis-based implementation and adds a mechanism to prevent request flooding.\n\n## Usage\n\nLeiningen:\n\n```clj\n[clj-rate-limiter \"0.1.5\"]\n```\n\n### Basic\n\nCreate an in-memory rate limiter with maximum 100 requsts in 1 seconds:\n\n```clj\n(require '[clj-rate-limiter.core :as r])\n(def limiter (r/create\n\t           (r/rate-limiter-factory :memory\n\t                                   :interval 1000\n\t                                   :max-in-interval 100)))\n\n(println (r/allow? limiter \"key1\"))\n(println (r/allow? limiter \"key2\"))\n(count (filter true? (repeatedly 1000 #(r/allow? limiter \"key3\")))) ;;should be 100\n```\n\nThe `:interval` sets the time window unit in millseconds, and `:max-in-interval` sets the maximum requests in a time window, and `:memory` make the rate limiter store requests in memory.\nAfter the limiter is created, you can use `(allow? limiter key)` to test if the request can be passed.The string key is present the type of the request.A group requests of the same key are rate limited by the limiter.\n\nIn a cluster, you may want to created a redis-backed limiter:\n\n```clj\n;;redis spec and pool as decribed in\n;;https://github.com/ptaoussanis/carmine\n(def redis {:spec {:host \"localhost\" :port 6379 :timeout 5000}\n            :pool {:max-active (* 3 (.availableProcessors (Runtime/getRuntime)))\n                   :min-idle (.availableProcessors (Runtime/getRuntime))\n                   :max-wait 5000}})\n\n(def limiter (r/create\n\t           (r/rate-limiter-factory :redis\n\t                                   :redis redis\n\t                                   :namespace \"APIs\"\n\t                                   :interval 1000\n\t                                   :max-in-interval 100)))\n```\n\nYou have to provide a redis spec and pool, and you can set the namespace prefix of the request keys.\n\n\n### min-difference\n\nYou can set the minimum time in millseconds between requests by `:min-difference` option,default is zero.\n\n```clj\n(def limiter (r/create\n\t           (r/rate-limiter-factory :redis\n\t                                   :redis redis\n\t                                   :namespace \"APIs\"\n\t                                   :interval 1000\n\t                                   :min-difference 1\n\t                                   :max-in-interval 100)))\n```\n\n## Protection for flooding requests\n\nThe rate limiter will still add the request to backend storage even if the requests are too many. It may consume too much memory in backend storage,so i provide a option `:flood-threshold` for such situation.\n\nIf you set `:flood-threshold` value(a long value),when current requests number in storage is greater or equal to `(* flood-threshold max-in-interval)`, then the limiter will not add the new request to backend storage until next time window.\n\n```clj\n(def limiter (r/create\n\t           (r/rate-limiter-factory :redis\n\t                                   :redis redis\n\t                                   :namespace \"APIs\"\n\t                                   :interval 1000\n\t                                   :flood-threshold 5\n\t                                   :max-in-interval 100)))\n```\n\n\n## License\n\nCopyright © 2015 dennis zhuang\n\nDistributed under the Eclipse Public License either version 1.0 or (at\nyour option) any later version.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkillme2008%2Fclj-rate-limiter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkillme2008%2Fclj-rate-limiter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkillme2008%2Fclj-rate-limiter/lists"}