1
1
import { EventEmitter } from "events" ;
2
2
import { createConnection , TcpNetConnectOpts } from "net" ;
3
- import { INatMap } from "../../cluster/ClusterOptions" ;
3
+ import { NatMap } from "../../cluster/ClusterOptions" ;
4
4
import {
5
5
CONNECTION_CLOSED_ERROR_MSG ,
6
6
packObject ,
9
9
} from "../../utils" ;
10
10
import { connect as createTLSConnection , ConnectionOptions } from "tls" ;
11
11
import SentinelIterator from "./SentinelIterator" ;
12
- import { IRedisClient , ISentinelAddress , ISentinel } from "./types" ;
12
+ import { RedisClient , SentinelAddress , Sentinel } from "./types" ;
13
13
import AbstractConnector , { ErrorEmitter } from "../AbstractConnector" ;
14
14
import { NetStream } from "../../types" ;
15
15
import Redis from "../../Redis" ;
@@ -18,26 +18,26 @@ import { FailoverDetector } from "./FailoverDetector";
18
18
19
19
const debug = Debug ( "SentinelConnector" ) ;
20
20
21
- interface IAddressFromResponse {
21
+ interface AddressFromResponse {
22
22
port : string ;
23
23
ip : string ;
24
24
flags ?: string ;
25
25
}
26
26
27
27
type PreferredSlaves =
28
- | ( ( slaves : IAddressFromResponse [ ] ) => IAddressFromResponse | null )
28
+ | ( ( slaves : AddressFromResponse [ ] ) => AddressFromResponse | null )
29
29
| Array < { port : string ; ip : string ; prio ?: number } >
30
30
| { port : string ; ip : string ; prio ?: number } ;
31
31
32
- export { ISentinelAddress , SentinelIterator } ;
32
+ export { SentinelAddress , SentinelIterator } ;
33
33
34
34
export interface SentinelConnectionOptions {
35
35
name ?: string ;
36
36
role ?: "master" | "slave" ;
37
37
tls ?: ConnectionOptions ;
38
38
sentinelUsername ?: string ;
39
39
sentinelPassword ?: string ;
40
- sentinels ?: Array < Partial < ISentinelAddress > > ;
40
+ sentinels ?: Array < Partial < SentinelAddress > > ;
41
41
sentinelRetryStrategy ?: ( retryAttempts : number ) => number | void | null ;
42
42
sentinelReconnectStrategy ?: ( retryAttempts : number ) => number | void | null ;
43
43
preferredSlaves ?: PreferredSlaves ;
@@ -46,7 +46,7 @@ export interface SentinelConnectionOptions {
46
46
sentinelCommandTimeout ?: number ;
47
47
enableTLSForSentinelMode ?: boolean ;
48
48
sentinelTLS ?: ConnectionOptions ;
49
- natMap ?: INatMap ;
49
+ natMap ?: NatMap ;
50
50
updateSentinels ?: boolean ;
51
51
sentinelMaxConnections ?: number ;
52
52
failoverDetector ?: boolean ;
@@ -196,7 +196,7 @@ export default class SentinelConnector extends AbstractConnector {
196
196
return connectToNext ( ) ;
197
197
}
198
198
199
- private async updateSentinels ( client : IRedisClient ) : Promise < void > {
199
+ private async updateSentinels ( client : RedisClient ) : Promise < void > {
200
200
if ( ! this . options . updateSentinels ) {
201
201
return ;
202
202
}
@@ -208,8 +208,8 @@ export default class SentinelConnector extends AbstractConnector {
208
208
}
209
209
210
210
result
211
- . map < IAddressFromResponse > (
212
- packObject as ( value : any ) => IAddressFromResponse
211
+ . map < AddressFromResponse > (
212
+ packObject as ( value : any ) => AddressFromResponse
213
213
)
214
214
. forEach ( ( sentinel ) => {
215
215
const flags = sentinel . flags ? sentinel . flags . split ( "," ) : [ ] ;
@@ -230,7 +230,7 @@ export default class SentinelConnector extends AbstractConnector {
230
230
}
231
231
232
232
private async resolveMaster (
233
- client : IRedisClient
233
+ client : RedisClient
234
234
) : Promise < TcpNetConnectOpts | null > {
235
235
const result = await client . sentinel (
236
236
"get-master-addr-by-name" ,
@@ -247,7 +247,7 @@ export default class SentinelConnector extends AbstractConnector {
247
247
}
248
248
249
249
private async resolveSlave (
250
- client : IRedisClient
250
+ client : RedisClient
251
251
) : Promise < TcpNetConnectOpts | null > {
252
252
const result = await client . sentinel ( "slaves" , this . options . name ) ;
253
253
@@ -256,8 +256,8 @@ export default class SentinelConnector extends AbstractConnector {
256
256
}
257
257
258
258
const availableSlaves = result
259
- . map < IAddressFromResponse > (
260
- packObject as ( value : any ) => IAddressFromResponse
259
+ . map < AddressFromResponse > (
260
+ packObject as ( value : any ) => AddressFromResponse
261
261
)
262
262
. filter (
263
263
( slave ) =>
@@ -269,16 +269,16 @@ export default class SentinelConnector extends AbstractConnector {
269
269
) ;
270
270
}
271
271
272
- sentinelNatResolve ( item : ISentinelAddress | null ) {
272
+ sentinelNatResolve ( item : SentinelAddress | null ) {
273
273
if ( ! item || ! this . options . natMap ) return item ;
274
274
275
275
return this . options . natMap [ `${ item . host } :${ item . port } ` ] || item ;
276
276
}
277
277
278
278
private connectToSentinel (
279
- endpoint : Partial < ISentinelAddress > ,
279
+ endpoint : Partial < SentinelAddress > ,
280
280
options ?: Partial < RedisOptions >
281
- ) : IRedisClient {
281
+ ) : RedisClient {
282
282
const redis = new Redis ( {
283
283
port : endpoint . port || 26379 ,
284
284
host : endpoint . host ,
@@ -304,7 +304,7 @@ export default class SentinelConnector extends AbstractConnector {
304
304
}
305
305
306
306
private async resolve (
307
- endpoint : Partial < ISentinelAddress >
307
+ endpoint : Partial < SentinelAddress >
308
308
) : Promise < TcpNetConnectOpts | null > {
309
309
const client = this . connectToSentinel ( endpoint ) ;
310
310
@@ -329,7 +329,7 @@ export default class SentinelConnector extends AbstractConnector {
329
329
// Move the current sentinel to the first position
330
330
this . sentinelIterator . reset ( true ) ;
331
331
332
- const sentinels : ISentinel [ ] = [ ] ;
332
+ const sentinels : Sentinel [ ] = [ ] ;
333
333
334
334
// In case of a large amount of sentinels, limit the number of concurrent connections
335
335
while ( sentinels . length < this . options . sentinelMaxConnections ) {
@@ -368,14 +368,14 @@ export default class SentinelConnector extends AbstractConnector {
368
368
}
369
369
370
370
function selectPreferredSentinel (
371
- availableSlaves : IAddressFromResponse [ ] ,
371
+ availableSlaves : AddressFromResponse [ ] ,
372
372
preferredSlaves ?: PreferredSlaves
373
- ) : ISentinelAddress | null {
373
+ ) : SentinelAddress | null {
374
374
if ( availableSlaves . length === 0 ) {
375
375
return null ;
376
376
}
377
377
378
- let selectedSlave : IAddressFromResponse ;
378
+ let selectedSlave : AddressFromResponse ;
379
379
if ( typeof preferredSlaves === "function" ) {
380
380
selectedSlave = preferredSlaves ( availableSlaves ) ;
381
381
} else if ( preferredSlaves !== null && typeof preferredSlaves === "object" ) {
@@ -427,9 +427,7 @@ function selectPreferredSentinel(
427
427
return addressResponseToAddress ( selectedSlave ) ;
428
428
}
429
429
430
- function addressResponseToAddress (
431
- input : IAddressFromResponse
432
- ) : ISentinelAddress {
430
+ function addressResponseToAddress ( input : AddressFromResponse ) : SentinelAddress {
433
431
return { host : input . ip , port : Number ( input . port ) } ;
434
432
}
435
433
0 commit comments