All Projects → unrelentingtech → Ring Ratelimit

unrelentingtech / Ring Ratelimit

Licence: wtfpl
Rate limiting middleware for Clojure Ring

Programming Languages

clojure
4091 projects
ring
36 projects

Projects that are alternatives of or similar to Ring Ratelimit

aequitas
Fairness regulator and rate limiter
Stars: ✭ 49 (-37.18%)
Mutual labels:  rate-limiting
Hammer
An Elixir rate-limiter with pluggable backends
Stars: ✭ 366 (+369.23%)
Mutual labels:  rate-limiting
Dalli Rate limiter
Arbitrary Memcached-backed rate limiting for Ruby
Stars: ✭ 38 (-51.28%)
Mutual labels:  rate-limiting
course-spring-microservices
Code examples built for the purpose of video course: Microservices With Spring Boot And Spring Cloud
Stars: ✭ 74 (-5.13%)
Mutual labels:  rate-limiting
Ex rated
ExRated, the Elixir OTP GenServer with the naughty name that allows you to rate-limit calls to any service that requires it.
Stars: ✭ 328 (+320.51%)
Mutual labels:  rate-limiting
Gubernator
High Performance Rate Limiting MicroService and Library
Stars: ✭ 609 (+680.77%)
Mutual labels:  rate-limiting
yii2-ratelimiter-advanced
Advanced Rate Limiter is a Yii2 filter to enforce or monitor request rate limits.
Stars: ✭ 23 (-70.51%)
Mutual labels:  rate-limiting
Nekobin
Elegant and open-source pastebin service
Stars: ✭ 61 (-21.79%)
Mutual labels:  rate-limiting
Ratelimit
API Rate Limit Decorator
Stars: ✭ 365 (+367.95%)
Mutual labels:  rate-limiting
Express Security
nodejs + express security and performance boilerplate.
Stars: ✭ 37 (-52.56%)
Mutual labels:  rate-limiting
kong-scalable-rate-limiter
Kong plugin for Rate Limiting at high throughputs.
Stars: ✭ 19 (-75.64%)
Mutual labels:  rate-limiting
Annon.api
Configurable API gateway that acts as a reverse proxy with a plugin system.
Stars: ✭ 306 (+292.31%)
Mutual labels:  rate-limiting
Redis Ratelimit
A fixed window rate limiter based on Redis
Stars: ✭ 15 (-80.77%)
Mutual labels:  rate-limiting
caddy-ratelimit
HTTP rate limiting module for Caddy 2
Stars: ✭ 72 (-7.69%)
Mutual labels:  rate-limiting
Bucket4j
Java rate limiting library based on token/leaky-bucket algorithm.
Stars: ✭ 1,025 (+1214.1%)
Mutual labels:  rate-limiting
limio
A rate limiting library for Go centered around intuitive and idiomatic interfaces, and designed to limit silly window syndrome.
Stars: ✭ 51 (-34.62%)
Mutual labels:  rate-limiting
Requests Respectful
Minimalist Requests wrapper to work within rate limits of any amount of services simultaneously. Parallel processing friendly.
Stars: ✭ 417 (+434.62%)
Mutual labels:  rate-limiting
Webapithrottle
ASP.NET Web API rate limiter for IIS and Owin hosting
Stars: ✭ 1,180 (+1412.82%)
Mutual labels:  rate-limiting
Bottleneck
Job scheduler and rate limiter, supports Clustering
Stars: ✭ 1,113 (+1326.92%)
Mutual labels:  rate-limiting
Rate Limit
Meteor package to rate-limit a function by queuing up calls (instead of dropping them like throttle or debounce)
Stars: ✭ 15 (-80.77%)
Mutual labels:  rate-limiting

Current semantic version:

[ring-ratelimit "0.2.2"]

ring-ratelimit Build Status

Powerful Ring middleware for request rate limiting.

Usage

Basic usage:

(ns your.app
  (:use [ring.middleware ratelimit]))

(def app (-> your-routes-or-whatever
             (wrap-ratelimit {:limits [(ip-limit 100)]})))
; 100 per hour per IP address

If you're running your app on multiple JVM instances (eg. multiple Heroku dynos), the default backend (local atom) is not enough for you. ring-ratelimit supports Redis via the Carmine library. Don't forget to add it to your dependencies!

(ns your.app
  (:use [ring.middleware ratelimit]
        [ring.middleware.ratelimit redis]))
; ...

; ...wrapping thing skipped...
(wrap-ratelimit {:limits [(ip-limit 100)]
                 :backend (redis-backend pool spec)})

The redis-backend function, when called with no args, calls make-conn-pool and make-conn-spec with no args (ie. uses Redis on localhost) and uses ratelimits for Redis hash name. You can provide the Carmine configuration objects (pool, spec) and the hash name as args.

If you're running your app on Immutant, there's a backend for you too:

(ns your.app
  (:require [immutant.cache :as c])
  (:use [ring.middleware ratelimit]
        [ring.middleware.ratelimit immutant]))
; ...

; ...wrapping thing skipped...
(wrap-ratelimit {:limits [(ip-limit 100)]
                 :backend (immutant-backend (c/cache "ratelimit" :mode :replicated))})

Advanced usage

If you're using friend authentication, you can use user-limit and role-limit:

; ...

(def app (-> your-routes-or-whatever
             (wrap-ratelimit {:limits [(role-limit :admin 1000)
                                       (user-limit 500)
                                       (ip-limit 100)]})
             (friend/authenticate ; ...
             )))
; 1000 per hour per user for authenticated users with role :admin
; 500 per hour per user for all other authenticated users
; 100 per hour per IP address for anonymous users

Also, you can compose limits if you use the ring.middleware.ratelimit.limits namespace! It's like Ring middleware for limits. You can do really amazing (but not very useful) stuff:

(ns your.app
  (:use [ring.middleware ratelimit]
        [ring.middleware.ratelimit limits]))

(def app (-> your-routes-or-whatever
             (wrap-ratelimit {:limits [(-> 500 limit
                                           (wrap-limit-header "x-my-api-key")
                                           (wrap-limit-param "awesomeness")
                                           (wrap-limit-role :admin)
                                           wrap-limit-ip)
                                       (ip-limit 100)]})
             (friend/authenticate ; ...
             )))
; 500 per hour per user per IP per ?awesomeness= param per X-My-Api-Key HTTP header for users with role :admin
; 100 per hour per IP for anonymous users
; note: with composed limits changing one parameter = a different limit key

You can use a custom handler for the error (when the user has no requests left):

; ...

(defn err-handler [req]
  {:status 429
   :headers {"Content-Type" "text/html"}
   :body (your-error-template {:text "Too many requests"})})

; ...wrapping thing skipped...
(wrap-ratelimit {:limits [(ip-limit 100)]
                 :err-handler err-handler})

If you use a routing library like Compojure, you can limit different parts of your API differently:

(ns hard.core
  (:use [compojure core]
        [ring.middleware ratelimit params]
        [ring.middleware.ratelimit redis limits]))

(defn easy-handler [req]
  {:status 200
   :headers {"Content-Type" "text/plain"}
   :body "easy"})

(defn hard-handler [req]
  {:status 200
   :headers {"Content-Type" "text/plain"}
   :body "hard"})

(def default-limit
  {:limits [(user-limit 1000) (ip-limit 100)]
   :err-handler err-handler
   :backend (redis-backend)})

(defroutes awesome-routes
  (ANY "/easy-things" []
    (-> easy-handler
        (wrap-ratelimit default-limit)))
  (ANY "/hard-things" []
    (-> hard-handler
        (wrap-ratelimit
          (assoc default-limit :limits
            [(-> 30 limit
                 (wrap-limit-param "query")
                 wrap-limit-user)
             (-> 10 limit
                 (wrap-limit-param "query")
                 wrap-limit-ip)])))))

(def awesome-app
  (-> awesome-routes
      wrap-params))

License

Copyright © 2013 Greg V [email protected]
This work is free. You can redistribute it and/or modify it under the
terms of the Do What The Fuck You Want To Public License, Version 2,
as published by Sam Hocevar. See the COPYING file for more details.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].