Created
December 12, 2020 06:39
-
-
Save animir/d06ca92931677f330d3f2d4c6c3108e4 to your computer and use it in GitHub Desktop.
Tiny rate-limiter-flexible example compatible with Deno
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
import { serve } from "https://deno.land/std/http/server.ts"; | |
import * as RateLimiterFlexible from "https://dev.jspm.io/rate-limiter-flexible"; | |
const rateLimiter = new RateLimiterFlexible.default.RateLimiterMemory({ | |
points: 2, | |
duration: 5, | |
}); | |
const s = serve({ port: 8000 }); | |
console.log("http://localhost:8000/"); | |
for await (const req of s) { | |
rateLimiter.consume('userId', 1) | |
.then((rateLimiterRes) => { | |
req.respond({ body: "Hello World\n" }); | |
}) | |
.catch((rej) => { | |
req.respond({status: 429, body: 'Too many ' + rej.consumedPoints}) | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment