-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
236 lines (215 loc) · 7.87 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
'use strict';
const ip = require('ip');
const is = require('is_js');
const pino = require('pino');
/**
* @description Redis based service registry & service discovery
* @class ServiceRegistry
*/
class ServiceRegistry {
/**
*Creates an instance of ServiceRegistry.
* @param {Object} redis redis instance
* @param {Object} options registry options
* @memberof ServiceRegistry
*/
constructor(redis, options) {
if (is.not.undefined(options) && is.not.object(options))
throw new Error('invalid options');
if (is.not.undefined(options) && is.not.object(options))
throw new Error('invalid options');
this._options = Object.assign({ prefix: 'clerq' }, options || {});
this._cache = {};
this._redis = redis;
this._logger = pino(Object.assign({ level: 'error' }, is.object(this._options.pino) ? this._options.pino : {}));
}
/**
* @description adds a new service instance
* @param {String} service name
* @param {String | Number} target address
* @returns Promise
* @memberof ServiceRegistry
*/
up(service, target) {
return new Promise((resolve, reject) => {
if (is.not.string(service) || is.empty(service)) throw new Error('INVALID_SERVICE');
const address = this._address(target), key = this._key(service);
this._redis.sadd(key, address, (e, d) => {
if (this._options.expire) this._redis.expire(key, this._options.expire);
if (is.error(e)) {
this._logger.error(e, 'clerq.up');
reject(e);
} else {
this._logger.info({ service, address, d }, 'clerq.up');
resolve(d);
}
});
});
}
/**
* @description removes an existing service instance
* @param {String} service name
* @param {String | Number} target address
* @returns Promise
* @memberof ServiceRegistry
*/
down(service, target) {
return new Promise((resolve, reject) => {
if (is.not.string(service) || is.empty(service)) throw new Error('INVALID_SERVICE');
const address = this._address(target), key = this._key(service);
this._redis.srem(key, address, (e, d) => {
if (this._options.expire) this._redis.expire(key, this._options.expire);
if (is.error(e)) {
this._logger.error(e, 'clerq.down');
reject(e);
} else {
this._logger.info({ service, address, d }, 'clerq.down');
resolve(d);
}
});
});
}
/**
* @description removes an existing service completely
* @param {String} service name
* @returns Promise
* @memberof ServiceRegistry
*/
destroy(service) {
return new Promise((resolve, reject) => {
if (is.not.string(service) || is.empty(service)) throw new Error('INVALID_SERVICE');
const key = this._key(service);
this._redis.expire(key, 1, e => {
if (is.error(e)) {
this._logger.error(e, 'clerq.destroy');
reject(e);
} else {
this._logger.info({ service }, 'clerq.destroy');
resolve(true);
}
});
});
}
/**
* @description returns a random instance by service
* @param {String} service name
* @returns Promise
* @memberof ServiceRegistry
*/
get(service) {
return new Promise((resolve, reject) => {
if (is.not.string(service) || is.empty(service)) throw new Error('INVALID_SERVICE');
else if (this.isCached(service)) return resolve(this._cache[service].d);
const key = this._key(service);
this._redis.srandmember(key, (e, d) => {
if (this._options.expire) this._redis.expire(key, this._options.expire);
if (is.error(e)) {
this._logger.error(e, 'clerq.get');
reject(e);
} else {
if (is.number(this._options.cache) && this._options.cache > 0)
if (d) this._cache[service] = { d, t: Date.now() };
this._logger.info({ service, d }, 'clerq.get');
resolve(d);
}
});
});
}
/**
* @description returns all instances by service
* @param {String} service name
* @returns Promise
* @memberof ServiceRegistry
*/
all(service) {
return new Promise((resolve, reject) => {
if (is.not.string(service) || is.empty(service)) throw new Error('INVALID_SERVICE');
const key = this._key(service);
this._redis.smembers(key, (e, d) => {
if (this._options.expire) this._redis.expire(key, this._options.expire);
if (is.error(e)) {
this._logger.error(e, 'clerq.all');
reject(e);
} else {
if (is.number(this._options.cache) && this._options.cache > 0)
if (is.array(d) && is.not.empty(d)) {
const address = d[ Math.floor(Math.random() * d.length) ];
this._cache[service] = { d: address, t: Date.now() };
}
this._logger.info({ service, d }, 'clerq.all');
resolve(d);
}
});
});
}
/**
* @description returns list of all services
* @returns Promise
* @memberof ServiceRegistry
*/
services() {
return new Promise((resolve, reject) => {
this._redis.keys(`${ this._options.prefix }*`, (e, d) => {
if (is.error(e)) {
this._logger.error(e, 'clerq.services');
reject(e);
} else {
const services = [];
if (is.array(d))
for (let service of d)
services.push(service.replace(this._key(), ''));
this._logger.info(services, 'clerq.services');
resolve(services);
}
});
});
}
/**
* @description checks if service instance cached properly
* @param {String} service
* @returns {Boolean}
* @memberof ServiceRegistry
*/
isCached(service) {
if (is.number(this._options.cache) && this._options.cache > 0 && is.existy(this._cache[service])) {
const cached = this._cache[service];
if (is.object(cached)) return Math.abs(Date.now() - cached.t) < this._options.cache;
}
return false;
}
/**
* @description stops service registry
* @memberof ServiceRegistry
*/
stop() {
this._redis.quit();
this._logger.info('service registry is down');
}
/**
* @description builds address up
* @private
* @returns String | undefined
* @memberof ServiceRegistry
*/
_address(target) {
if (is.number(target)) return `${ ip.address(this._options.iface) }:${ Math.abs(target) }`;
else if (is.string(target)) {
if (!target.includes(':')) {
const port = parseInt(target);
if (is.number(port)) return `${ ip.address(this._options.iface) }:${ Math.abs(port) }`;
} else return target;
}
}
/**
* @description builds key up
* @private
* @returns String
* @memberof ServiceRegistry
*/
_key(service) {
const key = `${ this._options.prefix }${ this._options.delimiter || '::' }`;
if (!service) return key;
else return `${ key }${ service }`;
}
}
module.exports = ServiceRegistry;