Skip to content

Instantly share code, notes, and snippets.

@animir
Created December 12, 2020 06:39
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save animir/d06ca92931677f330d3f2d4c6c3108e4 to your computer and use it in GitHub Desktop.
Save animir/d06ca92931677f330d3f2d4c6c3108e4 to your computer and use it in GitHub Desktop.
Tiny rate-limiter-flexible example compatible with Deno
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