Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
94939a6
chore: Scaffold node redis store package.
kinyoklion Jun 5, 2023
f9c2bcd
Add note on store organization.
kinyoklion Jun 5, 2023
a6a3918
Better text.
kinyoklion Jun 5, 2023
e57b38e
new
kinyoklion Jun 5, 2023
c3263d8
Update disable cache example.
kinyoklion Jun 5, 2023
73ac3bd
Add LICENSE.
kinyoklion Jun 5, 2023
e721f0c
chore: Scaffold node redis store package. (#136)
kinyoklion Jun 5, 2023
df18981
feat: Implement redis persistent store.
kinyoklion Jun 5, 2023
e444fbe
First draft of redis implementation.
kinyoklion Jun 5, 2023
01bdc2c
Start adding tests.
kinyoklion Jun 6, 2023
317f3e8
Work on tests
kinyoklion Jun 6, 2023
b958550
Recursive deps for build order.
kinyoklion Jun 6, 2023
bf07269
Fix update queue.
kinyoklion Jun 6, 2023
0743f4f
Add remainder of feature store tests.
kinyoklion Jun 6, 2023
a44e72b
Add redis core tests.
kinyoklion Jun 6, 2023
25eb6f9
Ignore redis files.
kinyoklion Jun 6, 2023
33146ec
Lint
kinyoklion Jun 6, 2023
708ed53
Add architecture diagram.
kinyoklion Jun 6, 2023
69e3868
Add initial big segments store.
kinyoklion Jun 7, 2023
b8b6f1d
Add big segment store to diagram.
kinyoklion Jun 7, 2023
3a1f9d2
Add description for BigSegmentStore.
kinyoklion Jun 7, 2023
77b5974
Add big segments tests. Extend store tests to use multiple prefixes.
kinyoklion Jun 7, 2023
f6d0ab6
Linting
kinyoklion Jun 7, 2023
1ab1ee9
Update CI configuration.
kinyoklion Jun 7, 2023
0aca35b
merge feature branch
kinyoklion Jun 7, 2023
fa2aa4b
Enable workflow for branch for testing.
kinyoklion Jun 7, 2023
a5a3129
Add sudo commands.
kinyoklion Jun 7, 2023
1903d66
Add dev dependency changes to CI build.
kinyoklion Jun 7, 2023
58f2415
Remove running on branch.
kinyoklion Jun 7, 2023
307c171
Correct typo in readme.
kinyoklion Jun 7, 2023
aac9142
Fix name of big segement store factory. Update index.
kinyoklion Jun 9, 2023
d5f989a
Merge branch 'rlamb/implement-redis-store' of github.com:launchdarkly…
kinyoklion Jun 9, 2023
628a1ef
Change job name
kinyoklion Jun 9, 2023
5c319ce
Rename exports for greater backward compatibility.
kinyoklion Jun 13, 2023
68506c9
Merge branch 'main' into rlamb/implement-redis-store
kinyoklion Jun 13, 2023
3445518
Update node server dep.
kinyoklion Jun 13, 2023
b9d8782
Catch on quit.
kinyoklion Jun 13, 2023
57edf27
Fix big segment included/excluded keys.
kinyoklion Jun 13, 2023
127b604
Add doc script.
kinyoklion Jun 13, 2023
a472f98
Merge branch 'main' into rlamb/feat/add-redis-persistent-store
kinyoklion Jun 13, 2023
e955907
Merge branch 'rlamb/feat/add-redis-persistent-store' into rlamb/imple…
kinyoklion Jun 13, 2023
a61bf36
Update node server package.
kinyoklion Jun 13, 2023
b466af8
Update packages/store/node-server-sdk-redis/src/RedisClientState.ts
kinyoklion Jun 15, 2023
e7c5492
Update packages/store/node-server-sdk-redis/src/RedisClientState.ts
kinyoklion Jun 15, 2023
4b241c1
Update packages/store/node-server-sdk-redis/src/RedisCore.ts
kinyoklion Jun 15, 2023
1c81d9e
Log ioredis quit errors.
kinyoklion Jun 15, 2023
9e4fdbe
Merge branch 'main' into rlamb/implement-redis-store
kinyoklion Jun 15, 2023
6b4c5fc
Merge branch 'main' into rlamb/feat/add-redis-persistent-store
kinyoklion Jun 15, 2023
05cd22a
Merge branch 'rlamb/feat/add-redis-persistent-store' into rlamb/imple…
kinyoklion Jun 15, 2023
8e5dacb
Merge branch 'rlamb/implement-redis-store' of github.com:launchdarkly…
kinyoklion Jun 15, 2023
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
name: '@launchdarkly/node-server-sdk-redis Bug Report'
about: Create a report to help us improve
title: ''
labels: 'package: store/node-server-sdk-redis, bug'
assignees: ''

---

**Is this a support request?**
This issue tracker is maintained by LaunchDarkly SDK developers and is intended for feedback on the code in this library. If you're not sure whether the problem you are having is specifically related to this library, or to the LaunchDarkly service overall, it may be more appropriate to contact the LaunchDarkly support team; they can help to investigate the problem and will consult the SDK team if necessary. You can submit a support request by going [here](https://support.launchdarkly.com/) and clicking "submit a request", or by emailing support@launchdarkly.com.

Note that issues filed on this issue tracker are publicly accessible. Do not provide any private account information on your issues. If your problem is specific to your account, you should submit a support request as described above.

**Describe the bug**
A clear and concise description of what the bug is.

**To reproduce**
Steps to reproduce the behavior.

**Expected behavior**
A clear and concise description of what you expected to happen.

**Logs**
If applicable, add any log output related to your problem.

**SDK version**
The version of this SDK that you are using.

**Language version, developer tools**
For instance, Go 1.11 or Ruby 2.5.3. If you are using a language that requires a separate compiler, such as C, please include the name and version of the compiler too.

**OS/platform**
For instance, Ubuntu 16.04, Windows 10, or Android 4.0.3. If your code is running in a browser, please also include the browser type and version.

**Additional context**
Add any other context about the problem here.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
name: '@launchdarkly/node-server-sdk-redis Feature Request'
about: Suggest an idea for this project
title: ''
labels: 'package: store/node-server-sdk-redis, feature'
assignees: ''

---

**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I would love to see the SDK [...does something new...]

**Describe the solution you'd like**
A clear and concise description of what you want to happen.

**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.

**Additional context**
Add any other context about the feature request here.
33 changes: 33 additions & 0 deletions .github/workflows/node-redis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: store/node-server-sdk-redis

on:
push:
branches: [main]
paths-ignore:
- '**.md' #Do not need to run CI for markdown changes.
pull_request:
branches: [main]
paths-ignore:
- '**.md'

jobs:
build-test-node-redis:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
registry-url: 'https://registry.npmjs.org'
# We may want to consider moving this build to a docker container instead of installing it
# in the image.
- run: |
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test suite needs a redis instance.

sudo apt-get update
sudo apt-get install redis-server
sudo service redis-server start
- id: shared
name: Shared CI Steps
uses: ./actions/ci
with:
workspace_name: '@launchdarkly/node-server-sdk-redis'
workspace_path: packages/store/node-server-sdk-redis
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ docs/
!.yarn/versions
yarn-error.log
.DS_Store
.vscode
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is in another PR, and will not be in the diff after it is merged and I update the base.

dump.rdb
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ This includes shared libraries, used by SDKs and other tools, as well as SDKs.
| [@launchdarkly/js-server-sdk-common](packages/shared/sdk-server/README.md) | [![NPM][js-server-sdk-common-npm-badge]][js-server-sdk-common-npm-link] | [Common Server][package-shared-sdk-server-issues] | [![Actions Status][shared-sdk-server-ci-badge]][shared-sdk-server-ci] |
| [@launchdarkly/js-server-sdk-common-edge](packages/shared/sdk-server-edge/README.md) | [![NPM][js-server-sdk-common-edge-badge]][js-server-sdk-common-edge-link] | [Common Server Edge][package-shared-sdk-server-edge-issues] | [![Actions Status][shared-sdk-server-edge-ci-badge]][shared-sdk-server-edge-ci] |

| Store Packages | npm | issues | tests |
| ------------------------------------------------------------------------------------- | --------------------------------------------------- | ------------------------------- | ------------------------------------------------------- |
| [@launchdarkly/node-server-sdk-redis](packages/store/node-server-sdk-redis/README.md) | [![NPM][node-redis-npm-badge]][node-redis-npm-link] | [Node Redis][node-redis-issues] | [![Actions Status][node-redis-ci-badge]][node-redis-ci] |

## Organization

`packages` Top level directory containing package implementations.
Expand All @@ -27,6 +31,8 @@ This includes shared libraries, used by SDKs and other tools, as well as SDKs.

`packages/sdk` SDK packages intended for use by application developers.

`packages/store` Persistent store packages for use with SDKs in this repository.

## LaunchDarkly overview

[LaunchDarkly](https://www.launchdarkly.com) is a feature management platform that serves trillions of feature flags daily to help teams build better software, faster. [Get started](https://docs.launchdarkly.com/home/getting-started) using LaunchDarkly today!
Expand Down Expand Up @@ -119,3 +125,9 @@ We encourage pull requests and other contributions from the community. Check out
[sdk-akamai-edgekv-dm-badge]: https://img.shields.io/npm/dm/@launchdarkly/akamai-server-edgekv-sdk.svg?style=flat-square
[sdk-akamai-edgekv-dt-badge]: https://img.shields.io/npm/dt/@launchdarkly/akamai-server-edgekv-sdk.svg?style=flat-square
[package-sdk-akamai-edgekv-issues]: https://github.com/launchdarkly/js-core/issues?q=is%3Aissue+is%3Aopen+label%3A%22package%3A+sdk%2Fakamai-edgekv%22+
[//]: # 'store/node-server-sdk-redis'
[node-redis-ci-badge]: https://github.com/launchdarkly/js-core/actions/workflows/node-redis.yml/badge.svg
[node-redis-ci]: https://github.com/launchdarkly/js-core/actions/workflows/node-redis.yml
[node-redis-npm-badge]: https://img.shields.io/npm/v/@launchdarkly/node-server-sdk-redis.svg?style=flat-square
[node-redis-npm-link]: https://www.npmjs.com/package/@launchdarkly/node-server-sdk-redis
[node-redis-issues]: https://github.com/launchdarkly/js-core/issues?q=is%3Aissue+is%3Aopen+label%3A%22package%3A+store%2Fnode-server-sdk-redis%22+
2 changes: 1 addition & 1 deletion actions/ci/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ runs:
- name: Build
shell: bash
# This will build the package and its dependencies.
run: yarn workspaces foreach -ptR --from '${{ inputs.workspace_name }}' run build
run: yarn workspaces foreach -pR --topological-dev --from '${{ inputs.workspace_name }}' run build

- name: Lint
shell: bash
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"packages/sdk/akamai-base",
"packages/sdk/akamai-base/example",
"packages/sdk/akamai-edgekv",
"packages/sdk/akamai-edgekv/example"
"packages/sdk/akamai-edgekv/example",
"packages/store/node-server-sdk-redis"
],
"private": true,
"scripts": {
Expand Down
Empty file.
13 changes: 13 additions & 0 deletions packages/store/node-server-sdk-redis/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright 2023 Catamorphic, Co.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
89 changes: 89 additions & 0 deletions packages/store/node-server-sdk-redis/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# LaunchDarkly Server-Side SDK for Node.js

[![NPM][node-redis-npm-badge]][node-redis-npm-link]
[![Actions Status][node-redis-ci-badge]][node-redis-ci]
[![Documentation](https://img.shields.io/static/v1?label=GitHub+Pages&message=API+reference&color=00add8)](https://launchdarkly.github.io/js-core/packages/store/node-server-sdk-redis/docs/)

This library provides a Redis-backed persistence mechanism (feature store) for the [LaunchDarkly Node.js SDK](https://github.com/launchdarkly/js-core/packages/sdk/server-node), replacing the default in-memory feature store. The underlying Redis client implementation is [ioredis](https://github.com/luin/ioredis).

The minimum version of the LaunchDarkly Server-Side SDK for Node for use with this library is 8.0.0.

This SDK is a beta version and should not be considered ready for production use while this message is visible.

## LaunchDarkly overview

[LaunchDarkly](https://www.launchdarkly.com) is a feature management platform that serves over 100 billion feature flags daily to help teams build better software, faster. [Get started](https://docs.launchdarkly.com/home/getting-started) using LaunchDarkly today!

[![Twitter Follow](https://img.shields.io/twitter/follow/launchdarkly.svg?style=social&label=Follow&maxAge=2592000)](https://twitter.com/intent/follow?screen_name=launchdarkly)

## Supported Node versions

This package is compatible with Node.js versions 14 and above.

## Getting started

Refer to [Using Redis as a persistent feature store](https://docs.launchdarkly.com/sdk/features/storing-data/redis#nodejs-server-side).

## Quick setup

This assumes that you have already installed the LaunchDarkly Node.js SDK.

1. Install this package with `npm` or `yarn`:

```shell
npm install @launchdarkly/node-server-sdk-redis --save
```

2. If your application does not already have its own dependency on the `ioredis` package, add `ioredis` as well:

```shell
npm install ioredis --save
```

3. Import the package:

```typescript
import { RedisFeatureStoreFactory } = from '@launchdarkly/node-server-sdk-redis';
```

4. When configuring your SDK client, add the Redis feature store:

```typescript
const storeFactory = RedisFeatureStoreFactory();
const config = { featureStore: storeFactory };
const client = LaunchDarkly.init('YOUR SDK KEY', config);
```

By default, the store will try to connect to a local Redis instance on port 6379. You may specify an alternate configuration as described in the API documentation for `RedisFeatureStoreFactory`.

## Caching behavior

To reduce traffic to Redis, there is an optional in-memory cache that retains the last known data for a configurable amount of time. This is on by default; to turn it off (and guarantee that the latest feature flag data will always be retrieved from Redis for every flag evaluation), configure the store as follows:

```typescript
const factory = RedisFeatureStoreFactory({ cacheTTL: 0 });
```

## Contributing

We encourage pull requests and other contributions from the community. Check out our [contributing guidelines](CONTRIBUTING.md) for instructions on how to contribute to this SDK.

## About LaunchDarkly

- LaunchDarkly is a continuous delivery platform that provides feature flags as a service and allows developers to iterate quickly and safely. We allow you to easily flag your features and manage them from the LaunchDarkly dashboard. With LaunchDarkly, you can:
- Roll out a new feature to a subset of your users (like a group of users who opt-in to a beta tester group), gathering feedback and bug reports from real-world use cases.
- Gradually roll out a feature to an increasing percentage of users, and track the effect that the feature has on key metrics (for instance, how likely is a user to complete a purchase if they have feature A versus feature B?).
- Turn off a feature that you realize is causing performance problems in production, without needing to re-deploy, or even restart the application with a changed configuration file.
- Grant access to certain features based on user attributes, like payment plan (eg: users on the ‘gold’ plan get access to more features than users in the ‘silver’ plan). Disable parts of your application to facilitate maintenance, without taking everything offline.
- LaunchDarkly provides feature flag SDKs for a wide variety of languages and technologies. Check out [our documentation](https://docs.launchdarkly.com/sdk) for a complete list.
- Explore LaunchDarkly
- [launchdarkly.com](https://www.launchdarkly.com/ 'LaunchDarkly Main Website') for more information
- [docs.launchdarkly.com](https://docs.launchdarkly.com/ 'LaunchDarkly Documentation') for our documentation and SDK reference guides
- [apidocs.launchdarkly.com](https://apidocs.launchdarkly.com/ 'LaunchDarkly API Documentation') for our API documentation
- [blog.launchdarkly.com](https://blog.launchdarkly.com/ 'LaunchDarkly Blog Documentation') for the latest product updates

[node-redis-ci-badge]: https://github.com/launchdarkly/js-core/actions/workflows/node-redis.yml/badge.svg
[node-redis-ci]: https://github.com/launchdarkly/js-core/actions/workflows/node-redis.yml

[node-redis-npm-badge]: https://img.shields.io/npm/v/@launchdarkly/node-server-sdk-redis.svg?style=flat-square
[node-redis-npm-link]: https://www.npmjs.com/package/@launchdarkly/node-server-sdk-redis
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import Redis from 'ioredis';
import { interfaces } from '@launchdarkly/node-server-sdk';
import RedisBigSegmentStore, {
KEY_LAST_SYNCHRONIZED,
KEY_USER_INCLUDE,
KEY_USER_EXCLUDE,
} from '../src/RedisBigSegmentStore';
import clearPrefix from './clearPrefix';

const FAKE_HASH = 'userhash';

async function setMetadata(
prefix: string,
metadata: interfaces.BigSegmentStoreMetadata
): Promise<void> {
const client = new Redis();
await client.set(
`${prefix}:${KEY_LAST_SYNCHRONIZED}`,
metadata.lastUpToDate ? metadata.lastUpToDate.toString() : ''
);
await client.quit();
}

async function setSegments(
prefix: string,
userHashKey: string,
included: string[],
excluded: string[]
): Promise<void> {
const client = new Redis();

// Generators and await in a loop, both of which eslint doesn't like. This is a test, and this
// is simpler.
/* eslint-disable */
for (const ref of included) {
await client.sadd(`${prefix}:${KEY_USER_INCLUDE}:${userHashKey}`, ref);
}
for (const ref of excluded) {
await client.sadd(`${prefix}:${KEY_USER_EXCLUDE}:${userHashKey}`, ref);
}
/* eslint-enable */
await client.quit();
}

describe.each([undefined, 'app1'])('given a redis big segment store', (prefixParam) => {
let store: RedisBigSegmentStore;
const prefix = prefixParam || 'launchdarkly';

beforeEach(async () => {
await clearPrefix(prefix);
// Use param directly to test undefined.
store = new RedisBigSegmentStore({ prefix: prefixParam });
});

afterEach(async () => {
store.close();
});

it('can get populated meta data', async () => {
const expected = { lastUpToDate: 1234567890 };
await setMetadata(prefix, expected);
const meta = await store.getMetadata();
expect(meta).toEqual(expected);
});

it('can get metadata when not populated', async () => {
const meta = await store.getMetadata();
expect(meta?.lastUpToDate).toBeUndefined();
});

it('can get user membership for a user which has no membership', async () => {
const membership = await store.getUserMembership(FAKE_HASH);
expect(membership).toBeUndefined();
});

it('can get membership for a user that is only included', async () => {
await setSegments(prefix, FAKE_HASH, ['key1', 'key2'], []);

const membership = await store.getUserMembership(FAKE_HASH);
expect(membership).toEqual({ key1: true, key2: true });
});

it('can get membership for a user that is only excluded', async () => {
await setSegments(prefix, FAKE_HASH, [], ['key1', 'key2']);

const membership = await store.getUserMembership(FAKE_HASH);
expect(membership).toEqual({ key1: false, key2: false });
});

it('can get membership for a user that is included and excluded', async () => {
await setSegments(prefix, FAKE_HASH, ['key1', 'key2'], ['key2', 'key3']);

const membership = await store.getUserMembership(FAKE_HASH);
expect(membership).toEqual({ key1: true, key2: true, key3: false }); // include of key2 overrides exclude
});
});
Loading