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

feature/cache-failure-config #39

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ Edit the values of your parameter in SSM Parameter Store or hosted configuration
* `diskSpace` is size in MB of the file created in tmp when `failureMode` is set to `diskspace`.
* `denylist` is an array of regular expressions, if a connection is made to a host matching one of the regular expressions it will be blocked.

### Caching
To enable caching, add an environment variable to your Lambda function with the key FAILURE_CACHE_TTL_SECONDS and the value set to the value to the desired cache time. The library uses node-cache for its implementation and consequently, FAILURE_CACHE_TTL_SECONDS = 0 can be used for an unlimited cache time.

## Example

In the subfolder `example` is a sample application which will install an AWS Lambda function, an Amazon DynamoDB table, and a parameter in SSM Parameter Store. You can install it using AWS SAM, AWS CDK, or Serverless Framework.
Expand Down Expand Up @@ -105,6 +108,10 @@ Inspired by Yan Cui's articles on latency injection for AWS Lambda (https://hack

## Changelog

### 2022-02-14 v0.4.5

* Adds optional caching to the SSM configuration

### 2022-02-14 v0.4.4

* Switch to node-fetch@2.
Expand Down
20 changes: 19 additions & 1 deletion lib/failure.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const ssm = new aws.SSM();
const fetch = require("node-fetch");
const childProcess = require("child_process");
const Mitm = require("mitm");
const nodeCache = require("node-cache");
const cache = new nodeCache.NodeCache();

var mitm = null;

Expand Down Expand Up @@ -56,10 +58,26 @@ async function getConfig() {
return defaults;
}
}

async function getCachedConfig() {
if (process.env.FAILURE_CACHE_TTL_SECONDS) {
const key = "failure-config";
let config = globalCache.get(key);
if (config === undefined) {
config = await getConfig();
const ttl = parseInt(process.env.FAILURE_CACHE_TTL_SECONDS);
globalCache.set(key, config, ttl);
}
return config;
} else {
return await getConfig();
}
}

var injectFailure = function (fn) {
return async function () {
try {
let config = await getConfig();
let config = await getCachedConfig();

if (config.isEnabled === false || config.failureMode != "denylist") {
clearMitm();
Expand Down
Loading