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 38cf377 commit 2ec14c5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ A token bucket rate limiter for Deno.
## Features

- [x] Simple and easy to use.
- [x] Regulates access to functions based on defined limits.
- [x] Rate limiting based on token bucket algorithm.

## Install

Expand All @@ -33,7 +33,7 @@ import { createThrottle } from "@117/throttle";
const throttle = createThrottle({ limit: 5, interval: 1000 });

const work = () => {
if (throttle.canProceed()) {
if (throttle.check()) {
console.log("request allowed, processing work");
} else {
console.log("request blocked, please wait");
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.1",
"version": "1.0.2",
"exports": "./mod.ts",
"imports": {
"@/": "./",
Expand Down
26 changes: 23 additions & 3 deletions src/createThrottle.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
/**
* Represents a throttle mechanism.
* @property {function(): boolean} check - Checks if a token is available.
*/
export type Throttle = {
check: () => boolean;
};

/**
* Options for creating a throttle.
* @property {number} limit - The maximum number of tokens.
* @property {number} interval - The time interval for token refill in milliseconds.
*/
export type CreateThrottleOptions = {
limit: number;
interval: number;
};

export const createThrottle = ({ limit, interval }: CreateThrottleOptions) => {
/**
* Creates a throttle with the specified options.
* @param {CreateThrottleOptions} options - The options for the throttle.
* @returns {Throttle} The throttle instance.
*/
export const createThrottle = (
{ limit, interval }: CreateThrottleOptions,
): Throttle => {
let tokens = limit;
let lastRefill = Date.now();

Expand All @@ -16,7 +36,7 @@ export const createThrottle = ({ limit, interval }: CreateThrottleOptions) => {
lastRefill = now;
};

const canProceed = () => {
const check = () => {
refillTokens();

if (tokens > 0) {
Expand All @@ -27,5 +47,5 @@ export const createThrottle = ({ limit, interval }: CreateThrottleOptions) => {
return false;
};

return { canProceed };
return { check };
};
10 changes: 5 additions & 5 deletions src/createThrottle_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ Deno.test("should allow requests within limit", () => {
const throttle = createThrottle({ limit: 5, interval: 1000 });

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

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

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

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

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

0 comments on commit 2ec14c5

Please sign in to comment.