-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
178 lines (138 loc) · 4.18 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
'use strict';
var redis = require('redis');
var crypto = require('crypto');
var address = require('network-address');
var noop = function () {};
var sha1 = function (val) {
return crypto.createHash('sha1').update(val).digest('hex');
};
var store;
var parseConnectionObject = function (config) {
if (!config.host) throw new Error('Invalid host.');
var opts = {
redis: {}
};
opts.namespace = config.namespace || 'rr';
opts.redis.port = config.port || 6379;
opts.redis.host = config.host;
opts.redis.options = config.options || {};
return opts;
};
module.exports = function (opts) {
opts = parseConnectionObject(opts);
if (!store) {
store = redis.createClient(opts.redis.port, opts.redis.host, opts.redis.options);
}
store.on('error', function (error) {
console.log(error);
});
var destroyed = false;
var ns = (opts.namespace || '').replace(/^\//, '').replace(/([^/])$/, '$1/');
var prefix = function (key) {
return 'services/' + ns + key;
};
var that = {};
var services = [];
var normalize = function (key) {
return key.replace(/[^a-zA-Z0-9-]/g, '-');
};
that.join = function (name, service, cb) {
if (typeof service === 'function') return that.join(name, null, service);
if (typeof service === 'number') service = {port: service};
if (!service) service = {};
if (!cb) cb = noop;
service.name = name;
service.hostname = service.hostname || address(process.env.REGISTRY_INTERFACE || '');
service.host = service.host || (service.port ? service.hostname + ':' + service.port : service.hostname);
service.url = service.url || (service.protocol || 'http') + '://' + service.host;
var key = prefix(normalize(name) + '/' + sha1(name + '-' + service.url));
var value = JSON.stringify(service);
var entry = {name: name, key: key, destroyed: false, timeout: null};
var update = function (cb) {
const multi = store.multi();
multi.set(key, value);
multi.expire(key, 10);
multi.exec(cb);
};
var loop = function () {
update(function (err) {
if (entry.destroyed) return;
entry.timeout = setTimeout(loop, err ? 15000 : 5000);
});
};
var onerror = function (err) {
leave([entry], function () {
cb(err);
});
};
services.push(entry);
update(function (err) {
if (err) return onerror(err);
if (destroyed) return onerror(new Error('registry destroyed'));
entry.timeout = setTimeout(loop, 5000);
cb(null, service);
});
};
that.lookup = function (name, cb) {
if (typeof name === 'function') return that.lookup(null, name);
that.list(name, function (err, list) {
if (err) return cb(err);
if (!list.length) return cb(null, null);
cb(null, list[(Math.random() * list.length) | 0]);
});
};
that.list = function (name, cb) {
if (typeof name === 'function') return that.list(null, name);
if (name) name = normalize(name);
store.keys(`${prefix(name || '')}*`, function (err, reply) {
if (err) {
return cb(err);
}
if (!reply || reply.length === 0) {
return cb(null, []);
}
store.mget(reply, function (err, replies) {
if (err) {
return cb(err);
}
if (!replies || replies.length === 0) {
return cb(null, []);
}
var list = replies
.map(function (node) {
try {
return JSON.parse(node);
} catch (err) {
return null;
}
})
.filter(function (val) {
return val;
});
cb(null, list);
});
});
};
var leave = function (list, cb) {
var loop = function () {
var next = list.shift();
if (!next) return cb();
clearTimeout(next.timeout);
next.destroyed = true;
var i = services.indexOf(next);
if (i > -1) services.splice(i, 1);
store.del(next.key, loop);
};
loop();
};
that.leave = function (name, cb) {
var list = services.filter(function (entry) {
return entry.name === name;
});
leave(list, cb || noop);
};
that.quit = function () {
store.quit();
};
return that;
};