-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8529e3a
Showing
11 changed files
with
1,290 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#--------------- | ||
# Your Redis Configuration | ||
# This is where one can configure their Redis Pubsub channel, etc. | ||
#----------------- | ||
# REDIS_CHANNEL= | ||
REDIS_URI=redis://172.17.0.1:6379 | ||
|
||
#---------------- | ||
# Your rate-limit specifications | ||
# This is where one can configure the specifications for their ratelimit. | ||
#----------------- | ||
# POINTS_PER_DURATION=45 | ||
# SECONDS_DURATION=1 | ||
|
||
# For Discord bots, a recommended points per duration for global rate-limiter is at 45 per second | ||
# while a recommended points per duration for identify is at 1 per 6 seconds. | ||
# It's recommended to run two instances of this client for Discord bots as this run single-threaded. | ||
# Refer to the docker-compose.yml which will do all the mess for you. | ||
|
||
# All the commented fields are ones that are configured automatically for Discord bots in the | ||
# docker-compose.yml. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.env | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
FROM node:16 | ||
WORKDIR /usr/src/app | ||
|
||
# Copy all the package.json first. | ||
COPY package*.json ./ | ||
|
||
# Build the production image. | ||
RUN npm install | ||
|
||
COPY . . | ||
|
||
RUN npx tsc -p . | ||
|
||
CMD [ "node", "./src/index.js"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Ria | ||
A simple Redis Pubsub-based rate-limiter for Discord bots (mainly from Javacord). The functionality of this rate-limiter is simple and that is to handle the `requestQuota` function of Javacord. | ||
|
||
## :package: Usage | ||
You can use Ria by first configuring the `.env` file, we highly recommend not modifying anything that is commented since those fields are pre-filled with the `docker-compose.yml` unless you are planning on using custom values for your rate-limit. | ||
```shell | ||
cp .env.format .env | ||
``` | ||
|
||
After configuring the `.env` file then you can simply build the container: | ||
```shell | ||
docker build -t ria . | ||
``` | ||
|
||
If you want to automatically boot two containers for the global and identify rate-limits then simply use the docker-compose file to get you up and running ASAP: | ||
```shell | ||
docker-compose up -d | ||
``` | ||
|
||
### Requesting a quota | ||
You can easily request a quota by simply sending a message to the `<#channel>.requests` (default: `global.ratelimits.requests` or `identify.ratelimits.requests`) with a callback field that you can use to identify later. | ||
```json | ||
{"callback":"hello world"} | ||
``` | ||
|
||
Ria should send you a quota within milliseconds (unless there is no quota) with a response on the consumer channel `<#channel>.consumer` (default: `global.ratelimits.consumer` or `identify.ratelimits.consumer`) such as this: | ||
```json | ||
{ | ||
"callback": "hello world", | ||
"message": { | ||
"approved": true | ||
} | ||
} | ||
``` | ||
|
||
Ria will not send a response until a quota is freed up which should allow you to perform blocking operations accurately until the response of Ria is received. A sample of how we wrote the receiver on Mana is: | ||
```kotlin | ||
class RedisRatelimiter(val channel: String): Ratelimiter { | ||
|
||
override fun requestQuota() { | ||
try { | ||
RedisPubsub.send(channel, null).join() | ||
} catch (exception: Exception) { | ||
exception.printStackTrace() | ||
Mana.getLogger().fromSLF4J().error("The rate-limiter failed to respond within the designated one-minute window, permit granted.") | ||
} | ||
} | ||
|
||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
version: '3.1' | ||
|
||
services: | ||
global: | ||
build: . | ||
restart: unless-stopped | ||
environment: | ||
- POINTS_PER_DURATION=45 | ||
- SECONDS_DURATION=1 | ||
- REDIS_CHANNEL=global.ratelimits | ||
env_file: | ||
- .env | ||
|
||
identify: | ||
build: . | ||
restart: unless-stopped | ||
environment: | ||
- POINTS_PER_DURATION=1 | ||
- SECONDS_DURATION=6 | ||
- REDIS_CHANNEL=identify.ratelimits | ||
env_file: | ||
- .env |
Oops, something went wrong.