Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Redis Storage Add On #16

Merged
merged 9 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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', link: '/guide/customization/addons/redis' }
]
}
]
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
108 changes: 108 additions & 0 deletions docs/src/guide/customization/addons/redis.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# Redis

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

## Quick Start

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

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

Then, add `redis` options when creating modules. Redis is only available for Ratelimit, SlidingCountBreaker or SlidingTimeBreaker module.

``` typescript
const rateLimitModule = new Mollitia.Ratelimit({
name: 'myRateLimitModule',
limitForPeriod: 2,
limitPeriod: 20000,
redis: {
// Setting redis.use to true indicates Redis 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 | `500` |
| `setMaxDelay` | Specifies the maximum time, in milliseconds,to set data to Redis | `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 is used for the module | `false` |
| `getMaxDelay` | Specifies the maximum time, in milliseconds,to get data from Redis | `500` |
| `setMaxDelay` | Specifies the maximum time, in milliseconds,to set data to Redis | `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 RedisAddon({ host: <Redis hostName>, port: <Redis Port>, password: <Redis Password>, getMaxDelay: 1000, setMaxDelay: 1000 }));
const rateLimitModule = new Mollitia.Ratelimit({
name: 'myRateLimitModule',
limitForPeriod: 2,
limitPeriod: 20000,
redis: {
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 for a long duration. Setting ttl to 0 deactivate the ttl.

Please note that this option is only applicable when Redis is used with SlidingCountBreaker module, as SlidingTimeBreaker module and Ratelimit module come with existing ttl (slidingWindowSize for SlidingCountBreaker, 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.

2 changes: 1 addition & 1 deletion packages/@mollitia/prometheus/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { defineLibConfig } from '../../../shared/vite/index.js';
import { version } from './package.json';

export default defineLibConfig({
name: 'Mollitia',
name: 'MollitiaPrometheus',
cadgerfeast marked this conversation as resolved.
Show resolved Hide resolved
base: './src',
entry: ['./index.ts'],
version
Expand Down
9 changes: 9 additions & 0 deletions packages/@mollitia/redis/.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/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/coverage
/dist
/node_modules
Loading
Loading