Skip to content

Commit

Permalink
refactor; bump
Browse files Browse the repository at this point in the history
  • Loading branch information
117 committed Sep 22, 2024
1 parent 7e406f3 commit 6b615c1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 28 deletions.
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,11 @@ import { createThrottle } from "@117/throttle";

const throttle = createThrottle({ limit: 5, interval: 1000 });

const work = () => {
if (throttle.check()) {
console.log("request allowed, processing work");
} else {
console.log("request blocked, please wait");
}
const work = async () => {
await throttle.wait();
console.log("request allowed, processing work");
};

// Simulate requests
setInterval(work, 200);
```

Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@117/throttle",
"description": "A token bucket rate limiter for Deno.",
"version": "1.0.3",
"version": "1.0.4",
"exports": "./mod.ts",
"imports": {
"@/": "./",
Expand Down
16 changes: 8 additions & 8 deletions src/createThrottle.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* Represents a throttle mechanism.
* @property {function(): boolean} check - Checks if a token is available.
* @property {function(): Promise<void>} wait - Waits for a token to be available.
*/
export type Throttle = {
check: () => boolean;
wait: () => Promise<void>;
};

/**
Expand Down Expand Up @@ -36,16 +36,16 @@ export const createThrottle = (
lastRefill = now;
};

const check = () => {
const wait = async () => {
refill();

if (tokens > 0) {
tokens--;
return true;
while (tokens <= 0) {
await new Promise((resolve) => setTimeout(resolve, 100));
refill();
}

return false;
tokens--;
};

return { check };
return { wait };
};
32 changes: 20 additions & 12 deletions src/createThrottle_test.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
import { assert } from "https://deno.land/std@0.177.0/testing/asserts.ts";
import { createThrottle } from "./createThrottle.ts";
import { assert } from "assert";

Deno.test("should allow requests within limit", () => {
import { createThrottle } from "@/src/createThrottle.ts";

Deno.test("should allow requests within limit", async () => {
const throttle = createThrottle({ limit: 5, interval: 1000 });

for (let i = 0; i < 5; i++) {
assert(throttle.check(), `request ${i + 1} should be allowed`);
await throttle.wait();
assert(true, `request ${i + 1} should be allowed`);
}
});

Deno.test("should block requests exceeding limit", async () => {
const throttle = createThrottle({ limit: 2, interval: 1000 });

assert(throttle.check(), "first request should be allowed");
assert(throttle.check(), "second request should be allowed");
assert(!throttle.check(), "third request should be blocked");
await throttle.wait();
await throttle.wait();

let blocked = false;

try {
await throttle.wait();
} catch {
blocked = true;
}

assert(blocked, "third request should be blocked");

// Wait for the interval to pass
await new Promise((resolve) => setTimeout(resolve, 1000));
await throttle.wait();

assert(
throttle.check(),
"fourth request should be allowed after interval",
);
assert(true, "fourth request should be allowed after interval");
});

0 comments on commit 6b615c1

Please sign in to comment.