Skip to content

Commit

Permalink
feat: Implement concurrency (#38)
Browse files Browse the repository at this point in the history
* feat: Add concurrency limit

* Refactor to clean up logic

* Fix type error

* Remove @types/debug
  • Loading branch information
nzakas authored Oct 31, 2024
1 parent 77ab8fa commit 330552f
Show file tree
Hide file tree
Showing 4 changed files with 551 additions and 100 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,41 @@ const text = await retrier.retry(() => fs.readFile("README.md", "utf8"));

The `retry()` method will either pass through the result on success or wait and retry on failure. Any error that isn't caught by the retrier is automatically rejected so the end result is a transparent passing through of both success and failure.

### Setting a Timeout

You can control how long a task will attempt to retry before giving up by passing the `timeout` option to the `Retrier` constructor. By default, the timeout is one minute.

```js
import fs from "fs/promises";

const retrier = new Retrier(error => {
return error.code === "ENFILE" || error.code === "EMFILE";
}, { timeout: 100_000 });

const text = await retrier.retry(() => fs.readFile("README.md", "utf8"));
```

When a call times out, it rejects the first error that was received from calling the function.

### Setting a Concurrency Limit

When processing a large number of function calls, you can limit the number of concurrent function calls by passing the `concurrency` option to the `Retrier` constructor. By default, `concurrency` is 1000.

```js
import fs from "fs/promises";

const retrier = new Retrier(error => {
return error.code === "ENFILE" || error.code === "EMFILE";
}, { concurrency: 100 });

const filenames = getFilenames();
const contents = await Promise.all(
filenames.map(filename => retrier.retry(() => fs.readFile(filename, "utf8"))
);
```
### Aborting with `AbortSignal`
You can also pass an `AbortSignal` to cancel a retry:
```js
Expand Down
60 changes: 50 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 330552f

Please sign in to comment.