limiter/README.md

131 lines
2.3 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
2024-07-10 23:36:44 +00:00
### Add tasks
```typescript
import { Limiter, LimiterRetryError } from "@nesterow/limiter";
const task = ({ url }) => {
await fetch(url);
// ... write
};
const limiter = new Limiter({
limit: 20,
onError(error) {
// Logger.error(error)
},
});
limiter.process(task);
limiter.process(task);
limiter.process(task);
```
2024-07-10 23:14:10 +00:00
### 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:36:44 +00:00
await limiter.process(...Array.from({ length: 100 }, () => task()));
2024-07-10 23:14:10 +00:00
```
### Limit RPS
```typescript
2024-07-10 23:36:44 +00:00
import { Limiter } from "@nesterow/limiter";
2024-07-10 23:14:10 +00:00
const execEvery100ms = () => {
2024-07-10 23:36:44 +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:36:44 +00:00
limit: 20,
rps: 10,
});
2024-07-10 23:14:10 +00:00
2024-07-10 23:36:44 +00:00
await limiter.process(...Array.from({ length: 100 }, () => execEvery100ms()));
2024-07-10 23:14:10 +00:00
```
### Retry
```typescript
2024-07-10 23:36:44 +00:00
import { Limiter, LimiterRetryError } from "@nesterow/limiter";
2024-07-10 23:14:10 +00:00
const retry5times = () => {
2024-07-10 23:36:44 +00:00
await fetch("https://my.api.xyz");
throw new Error("Connection refused");
// ... write
};
2024-07-10 23:14:10 +00:00
const limiter = new Limiter({
2024-07-10 23:36:44 +00:00
limit: 20,
maxRetry: 5,
});
for (let i = 0; i < 100; i++) {
try {
await limiter.process(Array.from({ length: 100 }, () => retry5times()));
} catch (e) {
if (e instanceof LimiterRetryError) {
// Logger.log(e)
2024-07-10 23:14:10 +00:00
}
2024-07-10 23:36:44 +00:00
}
2024-07-10 23:14:10 +00:00
}
```
### Handle errors in background
```typescript
2024-07-10 23:36:44 +00:00
import { Limiter, LimiterRetryError } from "@nesterow/limiter";
2024-07-10 23:14:10 +00:00
const wontStopPooling = () => {
2024-07-10 23:36:44 +00:00
await fetch("https://my.api.xyz");
throw new Error("Connection refused");
// ... write
};
2024-07-10 23:14:10 +00:00
const limiter = new Limiter({
2024-07-10 23:36:44 +00:00
limit: 20,
maxRetry: 5,
onError(error) {
// Logger.error(error)
},
});
2024-07-10 23:14:10 +00:00
2024-07-10 23:36:44 +00:00
for (let i = 0; i < 100; i++) {
await limiter.process(Array.from({ length: 100 }, () => wontStopPooling()));
2024-07-10 23:14:10 +00:00
}
```