Skip to content

Commit 0f7e2d2

Browse files
authored
Merge pull request #35 from alexist/feat/rate-limiter
feat(rate-limiter): New command RATE_LIMITER to manage sliding window rate limiter
2 parents 6bc7f29 + 59ad196 commit 0f7e2d2

23 files changed

+981
-132
lines changed

README.md

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ The protocol expose commands to interact with the distributed storage :
7878
- TOUCH : update the ttl of a key
7979
- DELETE : delete a key from the storage
8080
- INCR : increment the value for a key
81+
- RATE_LIMITER : consume a token in the sliding window rate limiter identified by the key
8182

8283
In a clustered deployment (2 or more instances), a client need to connect to only one instance to see all the storage.
8384
The goal is to provide a near storage associated with a nginx instance.
@@ -136,42 +137,41 @@ named hazelcast.xml.
136137
This is an example of this file :
137138

138139
```xml
139-
140-
<hazelcast xsi:schemaLocation="http://www.hazelcast.com/schema/config hazelcast-config-3.9.xsd"
140+
<?xml version="1.0" encoding="UTF-8"?>
141+
<hazelcast xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
141142
xmlns="http://www.hazelcast.com/schema/config"
142-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
143-
<group>
144-
<name>ngx-dshm</name>
145-
<password>FIXME</password>
146-
</group>
143+
xsi:schemaLocation="http://www.hazelcast.com/schema/config
144+
http://www.hazelcast.com/schema/config/hazelcast-config-5.0.xsd">
145+
146+
<cluster-name>ngx-dshm</cluster-name>
147+
147148
<network>
148149
<port auto-increment="false">5701</port>
149150
<join>
150151
<multicast enabled="false"/>
151152
<tcp-ip enabled="true">
152-
<interface>10.0.x.y</interface>
153+
<interface>127.0.0.1</interface>
153154
<member-list>
154-
<member>10.0.x.y:5701</member>
155-
<member>10.0.x.z:5701</member>
155+
<member>127.0.0.1</member>
156156
</member-list>
157157
</tcp-ip>
158158
<aws enabled="false"/>
159159
</join>
160160
</network>
161+
161162
<map name="default">
162163
<in-memory-format>BINARY</in-memory-format>
163164
<backup-count>1</backup-count>
164165
<async-backup-count>0</async-backup-count>
165166
<time-to-live-seconds>0</time-to-live-seconds>
166167
<max-idle-seconds>0</max-idle-seconds>
167-
<eviction-policy>NONE</eviction-policy>
168-
<max-size policy="PER_NODE">0</max-size>
169-
<eviction-percentage>25</eviction-percentage>
170-
<min-eviction-check-millis>100</min-eviction-check-millis>
171-
<merge-policy>com.hazelcast.map.merge.PutIfAbsentMapMergePolicy</merge-policy>
168+
<eviction eviction-policy="NONE"/>
169+
<merge-policy>PutIfAbsentMergePolicy</merge-policy>
172170
<cache-deserialized-values>INDEX-ONLY</cache-deserialized-values>
173171
</map>
172+
174173
</hazelcast>
174+
175175
```
176176

177177
The reference documentation for this configuration is
@@ -363,6 +363,27 @@ INCR key -1 0\r\n
363363
INCR key -1 0 60\r\n
364364
```
365365

366+
**_RATE_LIMITER \<key\> \<capacity\> \<duration\>_**
367+
368+
**with data:** _no_
369+
370+
Consumes a token in a sliding window rate limiter with the key `key`. The sliding window duration is configured with `duration` seconds. The
371+
rate limiter is created automatically
372+
373+
The command attempts to consume a token and return the number of remaining available tokens. If there were no more tokens available, the
374+
command returns -1, otherwise
375+
the command return the number of tokens available between 0 and `capacity`
376+
377+
note : GET command with this key return the available tokens
378+
379+
This operation is atomic.
380+
381+
Example : consumes a token in rate limiter `key` with capacity 1000 tokens every 10 seconds
382+
383+
```
384+
RATE_LIMITER key 1000 10\r\n
385+
```
386+
366387
**_FLUSHALL [region]_**
367388

368389
**with data:** _no_
@@ -460,10 +481,10 @@ The session_storage parameter control the storage module to be used.
460481
- An official docker image build is available at quay.io or directly in the GitHub registry :
461482

462483
```shell
463-
docker pull quay.io/grrolland/ngx-distributed-shm
484+
docker pull quay.io/grrolland/ngx-distributed-shm
464485
```
465486
```shell
466-
docker pull ghcr.io/grrolland/ngx-distributed-shm
487+
docker pull ghcr.io/grrolland/ngx-distributed-shm
467488
```
468489

469490
## Kubernetes

0 commit comments

Comments
 (0)