|
| 1 | +# Sliding Window Counter Rate Limiter |
| 2 | +Python sliding window counter API rate limiter using Redis. |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +[](https://shields.io/) |
| 9 | +[](https://github.com/ellerbrock/open-source-badges/) |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +<a name=""></a> |
| 14 | +# Table of contents |
| 15 | + |
| 16 | +- [Why Sliding window counter](#why-sliding-window-counter) |
| 17 | +- [Requirements](#requirements) |
| 18 | +- [Rate limit strategies](#rate-limit-strategies) |
| 19 | +- [Rate limit parameters](#rate-limit-parameters) |
| 20 | +- [Usage](#usage) |
| 21 | +- [Rate limiter supported methods](#rate-limiter-supported-methods) |
| 22 | +- [Optional parameters](#optional-parameters) |
| 23 | +- [License](#license) |
| 24 | +- [Contributing](#contributing) |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +# Why Sliding window counter |
| 30 | + |
| 31 | +Sliding window counter is used for two main reasons which are |
| 32 | + |
| 33 | +- Avoid spikes (smooth out bursts) |
| 34 | +- Memory efficient |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +# Requirements |
| 42 | + |
| 43 | +- [Python](https://www.python.org/downloads/) |
| 44 | +- [Redis](https://redis.io/download) |
| 45 | + |
| 46 | + |
| 47 | +# Rate limit strategies |
| 48 | + |
| 49 | +These strategies are used in rate limiter. |
| 50 | + |
| 51 | +- N requests per second |
| 52 | +- N requests per minute |
| 53 | +- N requests per hour |
| 54 | + |
| 55 | + |
| 56 | +# Rate limit parameters |
| 57 | + |
| 58 | +These parameter must be passed when initialize rate limiter class. |
| 59 | + |
| 60 | +- clientid |
| 61 | +- redispipeline |
| 62 | +- rate |
| 63 | +- time_window_unit |
| 64 | + |
| 65 | +This **clientid** parameter is used for how many requests serverd for particular user and how many request are left. |
| 66 | + |
| 67 | +This **redispipeline** parameter is redis pipeline which is used for perform opeartions in single shot. |
| 68 | + |
| 69 | + |
| 70 | +This **rate** parameter is used for how many request is allowed for particular user. |
| 71 | + |
| 72 | +```python |
| 73 | +rate = 100 # 100 request are allowed |
| 74 | +``` |
| 75 | + |
| 76 | +This **time_window_unit** parameter is used for time window. |
| 77 | + |
| 78 | +```python |
| 79 | +# singular time unit allowed. For example |
| 80 | + |
| 81 | +time_window_unit = 'hour' |
| 82 | +time_window_unit = 'minute' |
| 83 | +time_window_unit = 'second' |
| 84 | +``` |
| 85 | + |
| 86 | +Both **rate** and **time_window_unit** parameters are used for rate limit. Set rate limit |
| 87 | + |
| 88 | +**1400 requests per hour.** |
| 89 | + |
| 90 | +```python |
| 91 | +rate = 1400 |
| 92 | +time_window_unit = 'hour' |
| 93 | +``` |
| 94 | + |
| 95 | + |
| 96 | +**150 requests per minute.** |
| 97 | + |
| 98 | + |
| 99 | +```python |
| 100 | +rate = 150 |
| 101 | +time_window_unit = 'minute' |
| 102 | +``` |
| 103 | + |
| 104 | +**12 requests per second.** |
| 105 | + |
| 106 | + |
| 107 | +```python |
| 108 | +rate = 12 |
| 109 | +time_window_unit = 'second' |
| 110 | +``` |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | +# Usage |
| 115 | + |
| 116 | +Import redis and sliding window counter class |
| 117 | + |
| 118 | +```python |
| 119 | +import redis |
| 120 | +from ratelimiter import SlidingWindowCounterRateLimiter |
| 121 | +``` |
| 122 | + |
| 123 | +Create redis pipeline |
| 124 | +```python |
| 125 | +r = redis.Redis(host='localhost', port=6379, db=1) |
| 126 | +pipeline = r.pipeline() |
| 127 | +``` |
| 128 | + |
| 129 | +We need hourly rate limit for example 300 requests per hour.So initialize rate limiter |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | +```python |
| 134 | + |
| 135 | +rate = 300 # 300 requests allowed |
| 136 | +time_window_unit = 'hour' # per hour |
| 137 | +client_id = 'user-100C' # client id |
| 138 | + |
| 139 | +# pass these argument into rate limiter |
| 140 | +ratelimiter = SlidingWindowCounterRateLimiter(clientid=client_id,redispipeline=pipeline,rate = rate,time_window_unit=time_window_unit) |
| 141 | +``` |
| 142 | + |
| 143 | + |
| 144 | +After that check request is allowed or not. |
| 145 | + |
| 146 | +```python |
| 147 | +if ratelimiter.isRequestAllowed(): # Return true if request allowed |
| 148 | + print('200') |
| 149 | + |
| 150 | +else: # return false if request not allowed |
| 151 | + print('429') |
| 152 | +``` |
| 153 | + |
| 154 | +# Rate limiter supported methods |
| 155 | + |
| 156 | +Get rate limit HTTP headers.These HTTP headers are returns as a dictionary. |
| 157 | + |
| 158 | +- X-RateLimit-Limit |
| 159 | +- X_RateLimit_Remaining |
| 160 | +- **X-RateLimit-Reset** : This header used extra space so if this header set true then it will be part of rate limit http headers otherwise not. |
| 161 | +- Retry-after |
| 162 | + |
| 163 | + |
| 164 | +```python |
| 165 | +ratelimiter.getHttpResponseHeaders() # return HTTP response headers as a dictionary. |
| 166 | +``` |
| 167 | + |
| 168 | + |
| 169 | + |
| 170 | + |
| 171 | + |
| 172 | +Get **Retry-after** time. |
| 173 | + |
| 174 | +```python |
| 175 | +ratelimiter.get_retry_after() |
| 176 | +``` |
| 177 | + |
| 178 | +Get **X_RateLimit_Remaining**. |
| 179 | + |
| 180 | +```python |
| 181 | +ratelimiter.get_x_ratelimit_remaining() |
| 182 | +``` |
| 183 | + |
| 184 | +Get total request served |
| 185 | + |
| 186 | +```python |
| 187 | +ratelimiter.getTotalRequestServedInSlidingWindow() |
| 188 | +``` |
| 189 | + |
| 190 | +Get rate limit (max request allowed) |
| 191 | + |
| 192 | +```python |
| 193 | +ratelimiter.getMaxRequestsAllowed() |
| 194 | +``` |
| 195 | + |
| 196 | +# Optional parameters |
| 197 | + |
| 198 | +Optional parameters can be passed to rate limiter when first time rate limiter initialized. These parameters are used for performance and reduce memory usage. |
| 199 | + |
| 200 | + |
| 201 | + |
| 202 | +This **is_ratelimit_reset_header_allowed** parameter is used for X-RateLimit-Reset header which used extra space. If not required then set to false when initialize ratelimiter. |
| 203 | + |
| 204 | +```python |
| 205 | +is_ratelimit_reset_header_allowed = True # By default set to true |
| 206 | +``` |
| 207 | + |
| 208 | +This **is_2nd_RTT_allowed** parameter is used for reduce memory usage and but increase latency. |
| 209 | +Hhen set to true on each request it will delete any expired **Fixed time window** which saved memory but increase latency. |
| 210 | + |
| 211 | +```python |
| 212 | +is_2nd_RTT_allowed = False # RTT = Round Trip Time |
| 213 | +``` |
| 214 | + |
| 215 | +This **max_no_time_window_for_deletion** parameter is used for both performance and reduce memory usage. When expired **Fixed time window** reached/euqal to this parameter then |
| 216 | +it will delete all expired **Fixed time windows** and it will ignore is_2nd_RTT_allowed parameter if set to true of false. |
| 217 | + |
| 218 | +```python |
| 219 | + |
| 220 | +max_no_time_window_for_deletion = 5 # By default set to 5 |
| 221 | + |
| 222 | +``` |
| 223 | + |
| 224 | + |
| 225 | + |
| 226 | + |
| 227 | + |
| 228 | + |
| 229 | + |
| 230 | +<a name="license"></a> |
| 231 | +# License |
| 232 | + |
| 233 | +[](https://shields.io/) |
| 234 | + |
| 235 | +License under a [MIT License](https://choosealicense.com/licenses/mit/). |
| 236 | + |
| 237 | + |
| 238 | +<a name="contributing"></a> |
| 239 | +# Contributing |
| 240 | + |
| 241 | +- Fork, clone or make a pull requset to this repository. |
| 242 | +- Ask here [https://github.com/faizanfareed/python-sliding-window-counter-rate-limiter/issues](https://github.com/faizanfareed/python-sliding-window-counter-rate-limiter/issues) |
0 commit comments