Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

Commit

Permalink
rate limiting
Browse files Browse the repository at this point in the history
  • Loading branch information
noahfschr committed May 1, 2021
1 parent 5ae7a28 commit 3ded82b
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@

[Coin Price List](./coin-price-list/): Backendless GraphQL use case with Next.js and Redis.

[Serverless Histogram API](./histogram-api/): Histogram API with AWS Lambda and Redis.

[Redisson](./redisson/): Example code that uses Redisson as client.

15 changes: 15 additions & 0 deletions bull/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var Queue = require('bull');

var videoQueue = new Queue('video transcoding', 'REPLACE_REDIS_URL_HERE')
// var videoQueue = new Queue('video transcoding', 'redis://127.0.0.1:6379');


videoQueue.process(function(job, done){
console.log(job.data)
done();
}).catch(err => {
console.log(err)
});


videoQueue.add({video: 'http://example.com/video1.mov'});
6 changes: 6 additions & 0 deletions serverless-rate-limiting/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# package directories
node_modules
jspm_packages

# Serverless directories
.serverless
35 changes: 35 additions & 0 deletions serverless-rate-limiting/handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const RateLimiter = require('async-ratelimiter')
const Redis = require('ioredis')
const { getClientIp } = require('request-ip')

const rateLimiter = new RateLimiter({
db: new Redis("rediss://:ec0651dac90948de97cf09a57a74fd62@usw1-selected-termite-30690.upstash.io:30690"),
max: 1,
duration: 5_000
})

module.exports.hello = async (event) => {
const clientIp = getClientIp(event) || 'NA'
const limit = await rateLimiter.get({id: clientIp})
if (!limit.remaining) {
return {
statusCode: 429,
body: JSON.stringify(
{
message: 'Sorry, you are rate limited. Wait for 5 seconds',
client: clientIp
},
),
};
}

return {
statusCode: 200,
body: JSON.stringify(
{
message: 'hello!',
client: clientIp
},
),
};
};
7 changes: 7 additions & 0 deletions serverless-rate-limiting/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"dependencies": {
"async-ratelimiter": "^1.2.8",
"ioredis": "^4.27.1",
"request-ip": "^2.1.3"
}
}
14 changes: 14 additions & 0 deletions serverless-rate-limiting/serverless.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
service: serverless-rate-limiting
frameworkVersion: '2'
provider:
name: aws
runtime: nodejs12.x
lambdaHashingVersion: 20201221
region: us-west-1
functions:
hello:
handler: handler.hello
events:
- httpApi:
path: /hello
method: get

0 comments on commit 3ded82b

Please sign in to comment.