-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathClusterOptions.ts
217 lines (195 loc) · 5.01 KB
/
ClusterOptions.ts
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import { SrvRecord, resolveSrv, lookup } from "dns";
import { RedisOptions } from "../redis/RedisOptions";
import { CommanderOptions } from "../utils/Commander";
import { NodeRole } from "./util";
export type DNSResolveSrvFunction = (
hostname: string,
callback: (
err: NodeJS.ErrnoException | null | undefined,
records?: SrvRecord[]
) => void
) => void;
export type DNSLookupFunction = (
hostname: string,
callback: (
err: NodeJS.ErrnoException | null | undefined,
address: string,
family?: number
) => void
) => void;
export interface NatMap {
[key: string]: { host: string; port: number };
}
/**
* Options for Cluster constructor
*/
export interface ClusterOptions extends CommanderOptions {
/**
* See "Quick Start" section.
*
* @default (times) => Math.min(100 + times * 2, 2000)
*/
clusterRetryStrategy?: (
times: number,
reason?: Error
) => number | void | null;
/**
* See Redis class.
*
* @default true
*/
enableOfflineQueue?: boolean;
/**
* When enabled, ioredis only emits "ready" event when `CLUSTER INFO`
* command reporting the cluster is ready for handling commands.
*
* @default true
*/
enableReadyCheck?: boolean;
/**
* Scale reads to the node with the specified role.
*
* @default "master"
*/
scaleReads?: NodeRole | Function;
/**
* When a MOVED or ASK error is received, client will redirect the
* command to another node.
* This option limits the max redirections allowed to send a command.
*
* @default 16
*/
maxRedirections?: number;
/**
* When an error is received when sending a command (e.g.
* "Connection is closed." when the target Redis node is down), client will retry
* if `retryDelayOnFailover` is valid delay time (in ms).
*
* @default 100
*/
retryDelayOnFailover?: number;
/**
* When a CLUSTERDOWN error is received, client will retry
* if `retryDelayOnClusterDown` is valid delay time (in ms).
*
* @default 100
*/
retryDelayOnClusterDown?: number;
/**
* When a TRYAGAIN error is received, client will retry
* if `retryDelayOnTryAgain` is valid delay time (in ms).
*
* @default 100
*/
retryDelayOnTryAgain?: number;
/**
* By default, this value is 0, which means when a `MOVED` error is received,
* the client will resend the command instantly to the node returned together with
* the `MOVED` error. However, sometimes it takes time for a cluster to become
* state stabilized after a failover, so adding a delay before resending can
* prevent a ping pong effect.
*
* @default 0
*/
retryDelayOnMoved?: number;
/**
* The milliseconds before a timeout occurs while refreshing
* slots from the cluster.
*
* @default 1000
*/
slotsRefreshTimeout?: number;
/**
* The milliseconds between every automatic slots refresh.
*
* @default 5000
*/
slotsRefreshInterval?: number;
/**
* Passed to the constructor of `Redis`
*
* @default null
*/
redisOptions?: Omit<
RedisOptions,
| "port"
| "host"
| "path"
| "sentinels"
| "retryStrategy"
| "enableOfflineQueue"
| "readOnly"
>;
/**
* By default, When a new Cluster instance is created,
* it will connect to the Redis cluster automatically.
* If you want to keep the instance disconnected until the first command is called,
* set this option to `true`.
*
* @default false
*/
lazyConnect?: boolean;
/**
* Discover nodes using SRV records
*
* @default false
*/
useSRVRecords?: boolean;
/**
* SRV records will be resolved via this function.
*
* You may provide a custom `resolveSrv` function when you want to customize
* the cache behavior of the default function.
*
* @default require('dns').resolveSrv
*/
resolveSrv?: DNSResolveSrvFunction;
/**
* Hostnames will be resolved to IP addresses via this function.
* This is needed when the addresses of startup nodes are hostnames instead
* of IPs.
*
* You may provide a custom `lookup` function when you want to customize
* the cache behavior of the default function.
*
* @default require('dns').lookup
*/
dnsLookup?: DNSLookupFunction;
natMap?: NatMap;
/**
* See Redis class.
*
* @default false
*/
enableAutoPipelining?: boolean;
/**
* See Redis class.
*
* @default []
*/
autoPipeliningIgnoredCommands?: string[];
/**
* Custom LUA commands
*/
scripts?: Record<
string,
{ lua: string; numberOfKeys?: number; readOnly?: boolean }
>;
}
export const DEFAULT_CLUSTER_OPTIONS: ClusterOptions = {
clusterRetryStrategy: (times) => Math.min(100 + times * 2, 2000),
enableOfflineQueue: true,
enableReadyCheck: true,
scaleReads: "master",
maxRedirections: 16,
retryDelayOnMoved: 0,
retryDelayOnFailover: 100,
retryDelayOnClusterDown: 100,
retryDelayOnTryAgain: 100,
slotsRefreshTimeout: 1000,
useSRVRecords: false,
resolveSrv: resolveSrv,
dnsLookup: lookup,
enableAutoPipelining: false,
autoPipeliningIgnoredCommands: [],
};