forked from hipache/hipache
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcache.js
191 lines (160 loc) · 5.21 KB
/
cache.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
'use strict';
/*
* This module handles all IO called on the cache (currently Redis)
*/
var url = require('url'),
factory = require('./drivers/factory'),
LruCache = require('./lru');
var legacyConfigSupport = function (config, log) {
if (config.redisHost || config.redisPort) {
log('WARNING: old redis syntax is deprecated! Please migrate to ' +
'{"driver": ["redis://host:port", "redis://masterhost:port"]}');
}
var driver = 'redis://';
if (config.redisPassword) {
driver += ':' + config.redisPassword + '@';
}
driver += config.redisHost || '';
if (config.redisPort) {
driver += ':' + config.redisPort;
}
if (config.redisDatabase) {
driver += '/' + config.redisDatabase;
}
config.driver = [driver];
if (config.redisMasterHost) {
driver = 'redis://';
if (config.redisMasterPassword) {
driver += ':' + config.redisMasterPassword + '@';
}
driver += config.redisMasterHost || '';
if (config.redisMasterPort) {
driver += ':' + config.redisMasterPort;
}
if (config.redisMasterDatabase) {
driver += '/' + config.redisMasterDatabase;
}
config.driver.push(driver);
}
};
function Cache(config, handlers) {
if (!(this instanceof Cache)) {
return new Cache(config, handlers);
}
var logHandler = handlers.logHandler || console.log,
debugHandler = handlers.debugHandler || console.log;
this.config = config;
this.log = function (msg) {
logHandler('Cache: ' + msg);
};
this.debug = function (msg) {
debugHandler('Cache: ' + msg);
};
if (!('driver' in config)) {
legacyConfigSupport(config, this.log);
}
this.client = factory.getDriver(config.driver);
this.client.on('error', function (err) {
this.log('DriverError ' + err);
}.bind(this));
// LRU cache for Redis lookups
this.lru = new LruCache();
this.lru.enabled = config.server.lruCache;
}
/*
* This method mark a dead backend in the cache by its backend id
*/
Cache.prototype.markDeadBackend = function (backendInfo) {
this.client.mark(
backendInfo.frontend,
backendInfo.backendId,
backendInfo.backendUrl,
backendInfo.backendLen,
this.config.server.deadBackendTTL,
function () {}
);
// A dead backend invalidates the LRU
this.lru.del(backendInfo.frontend);
};
/*
* This method is an helper to get the domain name (to a given depth for subdomains)
*/
Cache.prototype.getDomainsLookup = function (hostname) {
var parts = hostname.split('.');
var result = [parts.join('.')];
var n;
// Prevent abusive lookups
while (parts.length > 6) {
parts.shift();
}
while (parts.length > 1) {
parts.shift();
n = parts.join('.');
result.push('*.' + n);
}
result.push('*');
return result;
};
/*
* This method picks up a backend randomly and ignore dead ones.
* The parsed URL of the chosen backend is returned.
* The method also decides which HTTP error code to return according to the
* error.
*/
Cache.prototype.getBackendFromHostHeader = function (host, callback) {
if (host === undefined) {
return callback('no host header', 400);
}
if (host === 'ping') {
return callback('ok', 200);
}
var index = host.indexOf(':');
if (index > 0) {
host = host.slice(0, index).toLowerCase();
}
var readFromCache = function (hostKey, cb) {
// Let's try the LRU cache first
var rows = this.lru.get(hostKey);
if (rows) {
return cb(rows.slice(0));
}
// The entry is not in the LRU cache, let's do a request on Redis
this.client.read(this.getDomainsLookup(hostKey), function (err, rows) {
this.lru.set(hostKey, rows);
cb(rows.slice(0));
}.bind(this));
}.bind(this);
readFromCache(host, function (rows) {
var deads = rows.pop();
var backends = rows.shift();
while (rows.length && !backends.length) {
backends = rows.shift();
}
if (!backends.length) {
return callback('frontend not found', 400);
}
var virtualHost = backends[0];
backends = backends.slice(1); // copy to not modify the lru in place
var indexes = [];
backends.forEach(function (bcd, idx) {
if (deads.indexOf(idx) === -1) {
indexes.push(idx);
}
});
if (!indexes.length) {
return callback('Cannot find a valid backend', 502);
}
var index = indexes[Math.floor(Math.random() * indexes.length)];
var backend = url.parse(backends[index]);
backend.id = index; // Store the backend index
backend.frontend = host; // Store the associated frontend
backend.virtualHost = virtualHost; // Store the associated vhost
backend.len = backends.length;
if (!backend.hostname) {
return callback('backend is invalid', 502);
}
backend.port = backend.port ? parseInt(backend.port, 10) : 80;
callback(false, 0, backend);
}.bind(this));
};
module.exports = Cache;