Skip to content

Commit

Permalink
Add Redis Storage Add On
Browse files Browse the repository at this point in the history
  • Loading branch information
tichon29 committed Mar 8, 2024
1 parent de3961e commit 67d6349
Show file tree
Hide file tree
Showing 26 changed files with 1,813 additions and 126 deletions.
3 changes: 2 additions & 1 deletion docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ export default withMermaid({
link: '/guide/customization/addons',
collapsed: false,
items: [
{ text: 'Prometheus', link: '/guide/customization/addons/prometheus' }
{ text: 'Prometheus', link: '/guide/customization/addons/prometheus' },
{ text: 'Redis Storage', link: '/guide/customization/addons/redisStorage' }
]
}
]
Expand Down
2 changes: 1 addition & 1 deletion docs/src/guide/api/modules/breaker/sliding-time.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ await circuit.fn(myFunction5).execute();
| `slowCallDurationThreshold` | Specifies the duration (in ms) threshold above which calls are considered as slow | `60000` |
| `permittedNumberOfCallsInHalfOpenState` | Specifies the number of permitted calls when the circuit is half open | `2` |
| `halfOpenStateMaxDelay` | Specifies the maximum wait (in ms) in Half Open State, before switching back to open. 0 deactivates this | `0` |
| `slidingWindowSize` | Specifies the sliding duration (in ms) used to calculate failure and slow call rate percentages | `10` |
| `slidingWindowSize` | Specifies the sliding duration (in ms) used to calculate failure and slow call rate percentages | `60` |
| `minimumNumberOfCalls` | Specifies the minimum number of calls used to calculate failure and slow call rate percentages | `10` |
| `openStateDelay` | Specifies the time (in ms) the circuit stay opened before switching to half-open | `60000` |
| `onError` | Allows filtering of the error to report as a failure or not. | `None` |
Expand Down
106 changes: 106 additions & 0 deletions docs/src/guide/customization/addons/redisStorage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Redis Storage

The `Mollitia` [Redis Storage](https://redis.io/) addon adds redis storage for some modules of every circuit. The list of modules coming with redis support are RateLimit, SlidingCountBreaker and SlidingTimeBreaker.

``` bash

```

## Quick Start

``` bash
# Install mollitia
npm install mollitia --save
# Install the Redis storage addon
npm install @mollitia/redis-storage --save
```

``` typescript
// Then add the addon
import * as Mollitia from 'mollitia';
import { StorageAddon } from '@mollitia/redis-storage';
// Adds the Redis Storage addon to Mollitia
Mollitia.use(new StorageAddOn({ host: <Redis hostName>, port: <Redis Port>, password: <Redis Password> }));
```

Then, add `storage` options when creating modules. Storage is only available for RateLimit, SlindingCountBreaker or SlidingTimeBreaker module.

``` typescript
const rateLimitModule = new Mollitia.Ratelimit({
name: 'myRateLimitModule',
limitForPeriod: 2,
limitPeriod: 20000,
storage: {
// Setting storage.use to true indicates Redis Storage should be used
use: true
}
};
// Creates a circuit
const myCircuit = new Mollitia.Circuit({
// Initializes a circuit with a handler
func: yourFunction,
options: {
modules: [
rateLimit: rateLimitModule
]
}
});
// This will execute yourFunction('dummy')
await myCircuit.execute('dummy');

```
## API Reference
### Options
#### When Addon is created
| Name | Description | Default |
|:-----------------|:----------------------------------------------------------------------------|:-----------|
| `getMaxDelay` | Specifies the maximum time, in milliseconds,to get data from Redis storage | `500` |
| `setMaxDelay` | Specifies the maximum time, in milliseconds,to set data to Redis storage | `500` |
| `ttl` | Specifies the maximum duration, in milliseconds, the data stays in Redis | `0` |
#### At module level
| Name | Description | Default |
|:-----------------|:----------------------------------------------------------------------------|:-----------|
| `use` | Specifies if the redis storage is used for the module | `false` |
| `getMaxDelay` | Specifies the maximum time, in milliseconds,to get data from Redis storage | `500` |
| `setMaxDelay` | Specifies the maximum time, in milliseconds,to set data to Redis storage | `500` |
| `ttl` | Specifies the maximum duration, in milliseconds, the data stays in Redis | `0` |
#### Option priority
When an option is defined both at AddOn level and at module level, the option value is taken from module
Example:
``` typescript
Mollitia.use(new StorageAddOn({ host: <Redis hostName>, port: <Redis Port>, password: <Redis Password>, getMaxDelay: 1000, setMaxDelay: 1000 }));
const rateLimitModule = new Mollitia.Ratelimit({
name: 'myRateLimitModule',
limitForPeriod: 2,
limitPeriod: 20000,
storage: {
use: true,
getMaxDelay: 500
}
};
````
With such configuration, getMaxDelay is 500, setMaxDelay is 1000 and ttl is 0 (not set, so using default value)


#### Additional information related to the options

* getMaxDelay and setMaxDelay

These options are available to avoid blocking the operations for a long time when Redis is slow or unavailable.

* ttl

This option could be used to avoid keeping some keys in Redis storage for a long duration. Setting ttl to 0 deactivate the ttl.

Please note that this option is only applicable when the Redis Storage is used with SlindingCountBreaker module, as SlidingTimeBreker module and RateLimit module come with existing ttl (slidingWindowSize for SlidingCoundBreaker, limitPeriod for RateLimit).

This option is converted to a number of seconds, and rounded to the next integer.
129 changes: 126 additions & 3 deletions package-lock.json

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

9 changes: 9 additions & 0 deletions packages/@mollitia/redis-storage/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint-env node */
module.exports = {
root: true,
extends: ['mollitia/typescript'],
parserOptions: {
tsconfigRootDir: __dirname,
project: './tsconfig.eslint.json'
}
};
3 changes: 3 additions & 0 deletions packages/@mollitia/redis-storage/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/coverage
/dist
/node_modules
54 changes: 54 additions & 0 deletions packages/@mollitia/redis-storage/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Mollitia

<p align="center"><br/><img width="200" src="https://genesys.github.io/mollitia/favicon.svg" alt="Mollitia Icon"/><br/><br/></p>

> Mollitia - Redis Storage Addon
The `Mollitia` [Redis Storage](https://redis.io/) addon adds redis storage for some modules of every circuit. The list of modules coming with redis support are RateLimit, SlidingCountBreaker and SlidingTimeBreaker.

## 📄 Documentation

Please check out the official documentation to get started using **Mollitia**, visit [genesys.github.io/mollitia](https://genesys.github.io/mollitia).

## ⚙️ Installation

``` bash
npm install --save @mollitia/redis-storage
```

## 🚀 Usage

``` typescript
// Imports the library
import * as Mollitia from 'mollitia';
import { StorageAddon } from '@mollitia/redis-storage';
// Adds the Redis Storage addon to Mollitia
Mollitia.use(new StorageAddOn({ host: <Redis hostName>, port: <Redis Port>, password: <Redis Password> }));
// Creates the module that will be used in your circuit, using Redis Storage
// Redis Storage is only applicable for Modules:
// - RateLimit
// - SlidingCountBreaker
// - SlidingTimeBreaker
const rateLimit = new Mollitia.Ratelimit({
name: 'myRateLimit',
limitForPeriod: 2,
limitPeriod: 20000,
storage: {
// Setting storage.use to true indicates Redis Storage should be used
use: true
}
};
// Creates a circuit
const myCircuit = new Mollitia.Circuit({
// Initializes a circuit with a handler
func: yourFunction,
options: {
modules: [
rateLimit
]
}
});
// This will execute yourFunction('dummy')
await myCircuit.execute('dummy');

```
Loading

0 comments on commit 67d6349

Please sign in to comment.