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

feat: add support for redis sentinel #276

Draft
wants to merge 1 commit into
base: next
Choose a base branch
from
Draft
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
11 changes: 11 additions & 0 deletions charts/app/templates/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ data:
{{- include "app.configValue" ( dict "Key" "APP_COOKIE_POLICY" "Value" .cookiePolicy ) }}
{{- end }}

# redis settings
{{- if .Values.redis.enabled }}
{{- if .Values.redis.sentinel.enabled }}
APP_REDIS_SENTINEL: "true"
{{- end }}
{{- else }}
{{- with .Values.app.redis }}
{{- include "app.configValue" ( dict "Key" "APP_REDIS_SENTINEL" "Value" .sentinel ) }}
{{- end }}
{{- end }}

# session settings
{{- with .Values.app.session }}
{{- include "app.configValue" ( dict "Key" "APP_SESSION_LIFETIME" "Value" .lifetime ) }}
Expand Down
7 changes: 6 additions & 1 deletion charts/app/templates/secrets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ type: Opaque
data:
# redis settings
{{- if .Values.redis.enabled }}
# provisioned redis settings
{{- if .Values.redis.sentinel.enabled }}
# provisioned redis settings for standalone
APP_REDIS_URI: {{ printf "redis://%s:26379" .Release.Name | b64enc }}
{{- else }}
# provisioned redis settings for standalone
APP_REDIS_URI: {{ printf "redis://%s-redis-master:6379" .Release.Name | b64enc }}
{{- end }}
{{- else }}
# custom redis settings
{{- with .Values.app.redis }}
Expand Down
12 changes: 10 additions & 2 deletions charts/app/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ app:
# redis settings
redis:
uri: ''
sentinel: ''

# database settings
database:
Expand Down Expand Up @@ -146,12 +147,19 @@ redis:
# should the redis chart be deployed
enabled: true

# architecture type
architecture: standalone
# use sentinel
sentinel:
enabled: true

# disable persistence on replica as we rely on sentinels
replica:
persistence:
enabled: false

auth:
# no password/authentication
enabled: false
sentinel: false

# MongoDB settings
# when using this sub chart
Expand Down
7 changes: 4 additions & 3 deletions docs/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ It's recommended to disable compression if you may delegate it to a reverse prox

## Redis configuration

| Name | Type | Default | Comment |
| ------------- | ------ | ---------------------- | ----------------------- |
| APP_REDIS_URI | String | redis://127.0.0.1:6379 | Redis connection string |
| Name | Type | Default | Comment |
| ------------------ | ------- | ---------------------- | ----------------------- |
| APP_REDIS_URI | String | redis://127.0.0.1:6379 | Redis connection string |
| APP_REDIS_SENTINEL | Boolean | false | Use sentinels |

## Session configuration

Expand Down
1 change: 1 addition & 0 deletions src/server/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const config = {

redis: {
uri: getString(getPrefix('REDIS_URI'), 'redis://127.0.0.1:6379'),
sentinel: getBoolean(getPrefix('REDIS_SENTINEL'), false),
},

session: {
Expand Down
7 changes: 3 additions & 4 deletions src/server/core/pubSub.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { EJSON, Document } from 'bson';
import { RedisPubSub } from 'graphql-redis-subscriptions';
import IORedis from 'ioredis';
import config from './config';
import { createRedisInstance } from './redis';

export const getPubSub = (): RedisPubSub => {
if (global.pubSub) {
return global.pubSub;
}

global.pubSub = new RedisPubSub({
publisher: new IORedis(config.redis.uri, { enableOfflineQueue: false }),
subscriber: new IORedis(config.redis.uri, { enableOfflineQueue: false }),
publisher: createRedisInstance(),
subscriber: createRedisInstance(),
serializer: (source: Document): string => JSON.stringify(EJSON.serialize(source)),
deserializer: (source: string): Document => EJSON.deserialize(JSON.parse(source)) as Document,
});
Expand Down
25 changes: 23 additions & 2 deletions src/server/core/redis.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
import IORedis, { Redis } from 'ioredis';
import IORedis, { Redis, RedisOptions } from 'ioredis';
import config from './config';

export const createRedisInstance = () => {
const details = new URL(config.redis.uri);

const options: RedisOptions = {
enableOfflineQueue: false,
};

if (config.redis.sentinel) {
options.sentinels = [{ host: details.hostname, port: parseInt(details.port, 10) }];
options.sentinelUsername = details.username || undefined;
options.sentinelPassword = details.password || undefined;
} else {
options.host = details.hostname;
options.port = parseInt(details.port, 10);
options.username = details.username || undefined;
options.password = details.password || undefined;
}

return new IORedis(options);
};

const getRedisInstance = (): Redis => {
if (global.redis) {
return global.redis;
}

global.redis = new IORedis(config.redis.uri, { enableOfflineQueue: false });
global.redis = createRedisInstance();

return global.redis;
};
Expand Down