[fix] readme

This commit is contained in:
Anton Nesterov 2024-07-11 01:36:44 +02:00
parent 9ca50199f9
commit 2550f7e165
4 changed files with 62 additions and 47 deletions

103
README.md
View file

@ -21,6 +21,28 @@ bun add github:nesterow/limiter # or pnpm
## Usage ## Usage
### 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);
```
### Limit number of requests ### Limit number of requests
```typescript ```typescript
@ -35,81 +57,74 @@ const limiter = new Limiter({
limit: 10, limit: 10,
}); });
for (let i = 0; i < 100; i++) { await limiter.process(...Array.from({ length: 100 }, () => task()));
await limiter.process(task);
}
``` ```
### Limit RPS ### Limit RPS
```typescript ```typescript
import {Limiter} from '@nesterow/limiter' import { Limiter } from "@nesterow/limiter";
const execEvery100ms = () => { const execEvery100ms = () => {
await fetch('https://my.api.xyz') await fetch("https://my.api.xyz");
// ... write // ... write
} };
const limiter = new Limiter({ const limiter = new Limiter({
limit: 20 limit: 20,
rps: 10 rps: 10,
}) });
for (let i=0; i < 100; i++) {
await limiter.process(execEvery100ms)
}
await limiter.process(...Array.from({ length: 100 }, () => execEvery100ms()));
``` ```
### Retry ### Retry
```typescript ```typescript
import {Limiter, LimiterRetryError} from '@nesterow/limiter' import { Limiter, LimiterRetryError } from "@nesterow/limiter";
const retry5times = () => { const retry5times = () => {
await fetch('https://my.api.xyz') await fetch("https://my.api.xyz");
throw new Error("Connection refused") throw new Error("Connection refused");
// ... write // ... write
} };
const limiter = new Limiter({ const limiter = new Limiter({
limit: 20 limit: 20,
maxRetry: 5 maxRetry: 5,
}) });
for (let i=0; i < 100; i++) { for (let i = 0; i < 100; i++) {
try { try {
await limiter.process(retry5times) await limiter.process(Array.from({ length: 100 }, () => retry5times()));
} catch(e) { } catch (e) {
if (e instanceof LimiterRetryError) { if (e instanceof LimiterRetryError) {
// Logger.log(e) // Logger.log(e)
}
} }
}
} }
``` ```
### Handle errors in background ### Handle errors in background
```typescript ```typescript
import {Limiter, LimiterRetryError} from '@nesterow/limiter' import { Limiter, LimiterRetryError } from "@nesterow/limiter";
const wontStopPooling = () => { const wontStopPooling = () => {
await fetch('https://my.api.xyz') await fetch("https://my.api.xyz");
throw new Error("Connection refused") throw new Error("Connection refused");
// ... write // ... write
} };
const limiter = new Limiter({ const limiter = new Limiter({
limit: 20 limit: 20,
maxRetry: 5, maxRetry: 5,
onError(error) { onError(error) {
// Logger.error(error) // Logger.error(error)
} },
}) });
for (let i=0; i < 100; i++) { for (let i = 0; i < 100; i++) {
await limiter.process(wontStopPooling) await limiter.process(Array.from({ length: 100 }, () => wontStopPooling()));
} }
``` ```

View file

@ -1,5 +1,5 @@
{ {
"name": "@nesterow/limiter", "name": "@nesterow/limiter",
"version": "0.1.0", "version": "0.1.1",
"exports": "./limiter.ts" "exports": "./limiter.ts"
} }

View file

@ -1,5 +1,5 @@
{ {
"version": "0.1.0", "version": "0.1.1",
"name": "@nesterow/limiter", "name": "@nesterow/limiter",
"module": "limiter.ts", "module": "limiter.ts",
"type": "module", "type": "module",