This repository has been archived by the owner on Nov 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
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
Showing
6 changed files
with
81 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
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,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'}); |
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,6 @@ | ||
# package directories | ||
node_modules | ||
jspm_packages | ||
|
||
# Serverless directories | ||
.serverless |
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,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 | ||
}, | ||
), | ||
}; | ||
}; |
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,7 @@ | ||
{ | ||
"dependencies": { | ||
"async-ratelimiter": "^1.2.8", | ||
"ioredis": "^4.27.1", | ||
"request-ip": "^2.1.3" | ||
} | ||
} |
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 @@ | ||
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 |