-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
216 lines (195 loc) · 5.26 KB
/
index.js
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
'use strict';
const redis = require('./build/Release/redis-fast-driver');
const EventEmitter = require('events').EventEmitter;
const os = require('os');
const defaultOptions = {
host: '127.0.0.1',
port: 6379,
db: 0,
auth: false,
maxRetries: -1,
tryToReconnect: true,
reconnectTimeout: 1000,
connectTimeout: 5000,
autoConnect: true,
doNotSetClientName: false,
doNotRunQuitOnEnd: false
};
class Redis extends EventEmitter {
constructor(opts) {
super();
this.opts = Object.assign({}, defaultOptions, opts);
this.init();
}
init() {
const {opts} = this;
this.name = opts.name || `redis-driver[${opts.host}:${opts.port}]`;
this.ready = false;
this.destroyed = false;
this.readyFirstTime = false;
this.connecting = false;
this.queue = [];
this.redis = new redis.RedisConnector();
this.connectTimeoutId = null;
this.reconnectTimeoutId = null;
this.reconnects = 0;
this.onDisconnect = this._onDisconnect.bind(this);
this.onConnect = this._onConnect.bind(this);
// When autoConnect is on, give the user a chance to bind event handlers
if (opts.autoConnect) setImmediate(this.connect.bind(this));
}
connect() {
if (this.destroyed) return;
const onError = (e) => {
this.emit('error', new Error(e));
this.reconnect();
};
try {
this.connecting = true;
this.redis.connect(this.opts.host, this.opts.port, this.onConnect, this.onDisconnect);
this.connectTimeoutId = setTimeout(() => {
this.redis.disconnect();
onError('Connection Timeout.');
}, this.opts.connectTimeout);
} catch(e) {
onError(e);
}
}
processQueue() {
if (this.queue.length > 0){
this.queue.forEach((cmd) => this.redis.redisCmd(cmd.args, cmd.cb));
this.queue = [];
}
}
reconnect() {
const {opts} = this;
if (opts.tryToReconnect === false || (opts.maxRetries > -1 && this.reconnects >= opts.maxRetries)) {
this.emit('error', new Error('Disconnected, exhausted retries.'));
this.end();
return;
}
this.reconnects++;
this.emit('reconnecting', this.reconnects);
if (this.reconnectTimeoutId) clearTimeout(this.reconnectTimeoutId);
this.reconnectTimeoutId = setTimeout(this.connect.bind(this), opts.reconnectTimeout);
}
selectDb(cb) {
const dbNum = this.opts.db;
if (dbNum > 0) {
this.redis.redisCmd(['SELECT', dbNum], (e) => {
if (e) {
this.emit('error', new Error(e));
this.reconnect();
return;
}
cb();
});
} else {
setImmediate(cb);
}
}
sendAuth(cb) {
const {auth} = this.opts;
if (auth) {
this.redis.redisCmd(['AUTH', auth], (e) => {
if(e) {
this.emit('error', new Error('Wrong password!'));
this.reconnect();
return;
}
cb();
});
} else {
setImmediate(cb);
}
}
_onConnect(e) {
if (e) {
this.emit('error', new Error(e));
this.reconnect();
return;
}
if (this.destroyed) {
// end() while we were connecting!
this.redis && this.redis.disconnect();
return;
}
this.ready = true;
this.connecting = false;
this.sendAuth(() => {
this.selectDb(() => {
if(!this.opts.doNotSetClientName) {
this.rawCall(['CLIENT', 'SETNAME', 'redis-fast-driver['+os.hostname()+':PID-'+process.pid+']']);
}
this.processQueue();
if (!this.readyFirstTime) {
this.readyFirstTime = true;
this.emit('ready');
}
this.reconnects = 0;
clearTimeout(this.connectTimeoutId);
this.emit('connect');
});
});
}
_onDisconnect(e) {
if (this.destroyed) return;
this.ready = false;
this.connecting = false;
clearTimeout(this.connectTimeoutId);
this.emit('disconnect');
if (e) {
this.emit('error', new Error(e));
}
this.reconnect();
}
rawCall(args, cb) {
if (!args || !Array.isArray(args)) {
throw new Error('first argument to rawCall() must be an Array');
}
if (this.destroyed) {
throw new Error('rawCall() cannot be called on a destroyed adapter.');
}
if (typeof cb === 'undefined') {
cb = (e) => {
if (e) this.emit('error', new Error(e));
};
}
// If not connected, push to queue
if (!this.ready) {
this.queue.push({args, cb});
return this;
}
// Send cmd
this.redis.redisCmd(args, cb);
return this;
}
rawCallAsync(args) {
return new Promise((resolve, reject) => {
this.rawCall(args, (err, resp) => {
if (err) return reject(err);
resolve(resp);
});
});
}
end() {
if(!this.opts.doNotRunQuitOnEnd) {
this.rawCall(['QUIT']);
}
this.ready = false;
this.destroyed = true;
this.queue = []; // prevents possible memleak
// Can still be present if connection started first time but not succeeded
clearTimeout(this.connectTimeoutId);
// If we were once connected, disconnect
if (this.redis && this.readyFirstTime) {
this.redis.disconnect();
setImmediate(() => this.emit('disconnect'));
}
if (!this.connecting) {
this.redis = null;
}
setImmediate(() => this.emit('end'));
}
}
module.exports = Redis;