Skip to content

Commit

Permalink
The full implementation of Ria
Browse files Browse the repository at this point in the history
  • Loading branch information
ShindouMihou committed Apr 24, 2022
0 parents commit 8529e3a
Show file tree
Hide file tree
Showing 11 changed files with 1,290 additions and 0 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
21 changes: 21 additions & 0 deletions .env.format
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.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.env
node_modules
14 changes: 14 additions & 0 deletions Dockerfile
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"]
50 changes: 50 additions & 0 deletions README.md
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.")
}
}

}
```
22 changes: 22 additions & 0 deletions docker-compose.yml
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
Loading

0 comments on commit 8529e3a

Please sign in to comment.