# sz-throttler

> Throttler implementation to allow a maximum number of executions of some code block in a given time window across multiple instances

Latest version **1.0.0** (published 2017-07-03) · 0 weekly downloads

## Install

```sh
npm install sz-throttler
pnpm add sz-throttler
yarn add sz-throttler
bun add sz-throttler
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2017-07-03 |
| First published | 2017-07-03 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | 8.1.2 |
| Dependencies | 2 |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | szanata |
| Keywords | logs, request, node |

## Links

- npm: https://www.npmjs.com/package/sz-throttler
- Repository: https://github.com/madeinstefano/sz-throttler
- Homepage: https://github.com/madeinstefano/sz-throttler#readme
- Issues: https://github.com/madeinstefano/sz-throttler/issues
- npm.io page: https://npm.io/package/sz-throttler

## Dependencies (2)

- [uuid](https://npm.io/package/uuid.md) 3.1.0
- [redlock](https://npm.io/package/redlock.md) 3.0.0

## Alternatives

- [@expo/fingerprint](https://npm.io/package/@expo/fingerprint.md) — 6.2M weekly downloads
- [@azure/monitor-opentelemetry-exporter](https://npm.io/package/@azure/monitor-opentelemetry-exporter.md) — 850.0K weekly downloads
- [@azure/monitor-opentelemetry](https://npm.io/package/@azure/monitor-opentelemetry.md) — 624.0K weekly downloads
- [@posthog/ai](https://npm.io/package/@posthog/ai.md) — 423.3K weekly downloads
- [fakefilter](https://npm.io/package/fakefilter.md) — 63.9K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2017-07-03

## README

# SZ Throttler

Throttler implementation to allow a maximum number of executions of some code block in a given time window across multiple instances.

This library uses Redis, to synchronize the execution, so you must have the same instance running and accessible to all instances with throttled code.

When the maximum number executions is reached, any new executions will be queued in a pool, waiting for a chance to be executed. This is when the things here are unique, as instead of waiting for all time window to pass to execute the next block, this implementations always check how many executions happened since X seconds ago (x is the time window) and execute a new block if this number is less then the configured limit.

## Usage

### Install and import

Install the lib:
`npm install sz-throttler`

Import in the application:
```js
const SZThrottler = require( 'sz-throttler' );
```

### Create a new throttler

```js
const throttler = new SZThrottler( redisClient ).getThrottler( 'my_throttler', 10, 5, false );
```

Instantiate a new throttler using a valid Redis client instance.

Than call `.getThrottler` to create a new throttler function. It can receive 4 parameters:
- **name** *{String}* Name of the throttler. Unique to the block of code intended to be executed, but will be the same across all the instances
- **maxConcurrency** *{Number}* Number of execution in the time window
- **timeWindow** *{Number}* The time window
- **waitForNotification** *{Boolean}* If this is `true`, the executed blocks will still be accounted on subsequent time windows until the code explicit calls a callback function.

### Usage

```js
throttler(function () {
  // myCode...
});
```

or, with the fourth parameter `true`:

```js
throttler(function ( notifyExecutionDone ) {
  // do shit ...
  notifyExecutionDone();
});
```

### Need to stop ASAP

throttle._kill();

---
_Source: https://npm.io/package/sz-throttler · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
