-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathredis.server.ts
More file actions
84 lines (75 loc) · 2.13 KB
/
Copy pathredis.server.ts
File metadata and controls
84 lines (75 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { Cluster, Redis, type ClusterNode, type ClusterOptions } from "ioredis";
import { defaultReconnectOnError } from "@internal/redis";
import { logger } from "./services/logger.server";
export type RedisWithClusterOptions = {
host?: string;
port?: number;
username?: string;
password?: string;
tlsDisabled?: boolean;
clusterMode?: boolean;
clusterOptions?: Omit<ClusterOptions, "redisOptions">;
keyPrefix?: string;
};
export type RedisClient = Redis | Cluster;
export function createRedisClient(
connectionName: string,
options: RedisWithClusterOptions
): Redis | Cluster {
let redis: Redis | Cluster;
if (options.clusterMode) {
const nodes: ClusterNode[] = [
{
host: options.host,
port: options.port,
},
];
logger.debug("Creating a redis cluster client", {
connectionName,
host: options.host,
port: options.port,
});
redis = new Redis.Cluster(nodes, {
...options.clusterOptions,
redisOptions: {
connectionName,
keyPrefix: options.keyPrefix,
username: options.username,
password: options.password,
enableAutoPipelining: true,
reconnectOnError: defaultReconnectOnError,
...(options.tlsDisabled
? {
checkServerIdentity: () => {
// disable TLS verification
return undefined;
},
}
: { tls: {} }),
},
dnsLookup: (address, callback) => callback(null, address),
slotsRefreshTimeout: 10000,
});
} else {
logger.debug("Creating a redis client", {
connectionName,
host: options.host,
port: options.port,
});
redis = new Redis({
connectionName,
host: options.host,
port: options.port,
username: options.username,
password: options.password,
enableAutoPipelining: true,
keyPrefix: options.keyPrefix,
reconnectOnError: defaultReconnectOnError,
...(options.tlsDisabled ? {} : { tls: {} }),
});
}
redis.on("error", (error) => {
logger.error("Redis client error", { connectionName, error });
});
return redis;
}