-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit a673859
Showing
7 changed files
with
150 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
name: deploy | ||
on: | ||
push: | ||
branches: | ||
- main | ||
jobs: | ||
publish: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: read | ||
id-token: write | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: set up Deno | ||
uses: denoland/setup-deno@v1 | ||
with: | ||
deno-version: v1.x | ||
- name: run Deno tests | ||
run: deno test | ||
- name: publish to JSR | ||
run: npx jsr publish |
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,49 @@ | ||
# throttle | ||
|
||
![version](https://img.shields.io/jsr/v/%40117/throttle?style=flat-square&color=%23ff51bc&label=version) | ||
![status](https://img.shields.io/github/actions/workflow/status/117/throttle/deploy.yml?style=flat-square) | ||
|
||
A throttle function implementation for Deno that manages request rates. | ||
|
||
## Contents | ||
|
||
- [Features](#features) | ||
- [Install](#install) | ||
- [Example](#example) | ||
- [Contributing](#contributing) | ||
|
||
## Features | ||
|
||
- [x] Simple and easy to use. | ||
- [x] Regulates access to functions based on defined limits. | ||
|
||
## Install | ||
|
||
For Deno: | ||
|
||
```sh | ||
$ deno add @117/throttle | ||
``` | ||
|
||
## Example | ||
|
||
```ts | ||
import { createThrottle } from "@117/throttle"; | ||
|
||
const throttle = createThrottle({ limit: 5, interval: 1000 }); | ||
|
||
const work = () => { | ||
if (throttle.canProceed()) { | ||
console.log("request allowed, processing work"); | ||
} else { | ||
console.log("request blocked, please wait"); | ||
} | ||
}; | ||
|
||
// Simulate requests | ||
setInterval(work, 200); | ||
``` | ||
|
||
## Contributing | ||
|
||
Feel free to contribute and PR to your 💖's content. |
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,10 @@ | ||
{ | ||
"name": "@117/throttle", | ||
"description": "A token bucket rate limiter for Deno.", | ||
"version": "1.0.0", | ||
"exports": "./mod.ts", | ||
"imports": { | ||
"@/": "./", | ||
"assert": "https://deno.land/std@0.224.0/assert/mod.ts" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,4 @@ | ||
export { | ||
createThrottle, | ||
type CreateThrottleOptions, | ||
} from "@/src/createThrottle.ts"; |
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,31 @@ | ||
export type CreateThrottleOptions = { | ||
limit: number; | ||
interval: number; | ||
}; | ||
|
||
export const createThrottle = ({ limit, interval }: CreateThrottleOptions) => { | ||
let tokens = limit; | ||
let lastRefill = Date.now(); | ||
|
||
const refillTokens = () => { | ||
const now = Date.now(); | ||
const elapsed = now - lastRefill; | ||
const newTokens = Math.floor(elapsed / interval) * limit; | ||
|
||
tokens = Math.min(limit, tokens + newTokens); | ||
lastRefill = now; | ||
}; | ||
|
||
const canProceed = () => { | ||
refillTokens(); | ||
|
||
if (tokens > 0) { | ||
tokens--; | ||
return true; | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
return { canProceed }; | ||
}; |
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,26 @@ | ||
import { assert } from "https://deno.land/std@0.177.0/testing/asserts.ts"; | ||
import { createThrottle } from "./createThrottle.ts"; | ||
|
||
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`); | ||
} | ||
}); | ||
|
||
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"); | ||
|
||
// Wait for the interval to pass | ||
await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
|
||
assert( | ||
throttle.canProceed(), | ||
"fourth request should be allowed after interval", | ||
); | ||
}); |