limiter/README.md

116 lines
1.9 KiB
Markdown
Raw Normal View History

2024-07-10 23:14:10 +00:00
# Limiter
2024-07-10 23:14:38 +00:00
A promise pool with RPS limiter.
2024-07-10 23:14:10 +00:00
Features:
- [x] TypeScript first
- [x] Limits parrallel promises execution
- [x] Limits RPS (requests per second), evenly distributes the requests over time
- [x] Able to retry
- [x] Simple API
- [x] Simple async/await flow
- [x] Allows to handle errors silently using onError callback
- [x] Works with any runtime (Bun/Deno/Node)
## Install
```bash
bun add github:nesterow/limiter # or pnpm
```
## Usage
### Limit number of requests
```typescript
2024-07-10 23:20:10 +00:00
import { Limiter } from "@nesterow/limiter";
2024-07-10 23:14:10 +00:00
const task = () => {
2024-07-10 23:20:10 +00:00
await fetch("https://my.api.xyz");
// ... write
};
2024-07-10 23:14:10 +00:00
const limiter = new Limiter({
2024-07-10 23:20:10 +00:00
limit: 10,
});
2024-07-10 23:14:10 +00:00
2024-07-10 23:20:10 +00:00
for (let i = 0; i < 100; i++) {
await limiter.process(task);
2024-07-10 23:14:10 +00:00
}
```
### Limit RPS
```typescript
import {Limiter} from '@nesterow/limiter'
const execEvery100ms = () => {
await fetch('https://my.api.xyz')
// ... write
}
const limiter = new Limiter({
limit: 20
rps: 10
})
for (let i=0; i < 100; i++) {
await limiter.process(execEvery100ms)
}
```
### Retry
```typescript
import {Limiter, LimiterRetryError} from '@nesterow/limiter'
const retry5times = () => {
await fetch('https://my.api.xyz')
throw new Error("Connection refused")
// ... write
}
const limiter = new Limiter({
limit: 20
maxRetry: 5
})
for (let i=0; i < 100; i++) {
try {
await limiter.process(retry5times)
} catch(e) {
if (e instanceof LimiterRetryError) {
// Logger.log(e)
}
}
}
```
### Handle errors in background
```typescript
import {Limiter, LimiterRetryError} from '@nesterow/limiter'
const wontStopPooling = () => {
await fetch('https://my.api.xyz')
throw new Error("Connection refused")
// ... write
}
const limiter = new Limiter({
limit: 20
maxRetry: 5,
onError(error) {
// Logger.error(error)
}
})
for (let i=0; i < 100; i++) {
await limiter.process(wontStopPooling)
}
```