-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsoupcache.js
More file actions
228 lines (202 loc) · 8.22 KB
/
soupcache.js
File metadata and controls
228 lines (202 loc) · 8.22 KB
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
var url = require('url'),
AuthCheck = require('./authCheck.js'),
http = require('http'),
fs = require('fs'),
async = require('async'),
util = require('util'),
AverageRing = require('./averageRing.js'),
server = null,
onRequest = null,
Cache = require('./mongocache.js'),
Logger = require('./logger.js'),
assetRequest = require('./assetRequest.js'),
nonAssetRequest = require('./nonAssetRequest.js'),
loginRequest = require('./loginRequest.js'),
statusRequest = require('./statusRequest.js'),
parasoupRequest = require('./parasoupRequest.js'),
maintenanceRequest = require('./maintenanceRequest.js'),
assetLoader = require('./assetLoader.js'),
statPrinter = require('./statPrinter.js'),
events = new require('events'),
options = JSON.parse(fs.readFileSync('./config.json'));
function requestDispatcher(request, response) {
if (onRequest) {
onRequest(request, response);
} else {
response.writeHead(503);
response.end('Parasoup starting up');
}
}
function startupComponents(options) {
options.assetLoader = new assetLoader(options);
options.stats = {
dataCount: {},
redirects: 0,
parasoups: 0,
parasoupAssetCache: 0,
assetCount: 0,
requests: 0,
soupErrors: 0,
assetsServed: 0,
responseTime: 0,
cacheHits: 0
};
options.eventBus = new events.EventEmitter();
options.domain = options.domainPrefix;
var parasoupRequestHandler = new parasoupRequest(options);
var authCheck = new AuthCheck(options);
onRequest = function(request, response) {
options.stats.requests++;
var assetRegex = new RegExp('asset-.\\.' + options.domain),
statusRegex = new RegExp('status\\.' + options.domain),
parasoupRegex = new RegExp('^' + options.domain + '$'),
assetRequestHandler = new assetRequest(options),
nonAssetRequestHandler = new nonAssetRequest(options),
loginRequestHandler = new loginRequest(options),
statusRequestHandler = new statusRequest(options);
maintenanceRequestHandler = new maintenanceRequest(options);
if (!options.stats.dataCount[request.connection.remoteAddress]) {
options.stats.dataCount[request.connection.remoteAddress] = 0;
}
var maint = fs.existsSync(options.maintenanceTriggerFile);
if (maint) {
new maintenanceRequestHandler(request, response);
} else {
if (request.headers.host && request.headers.host.match(assetRegex) && request.url.indexOf('?') === -1) {
new assetRequestHandler(request, response);
} else {
if (!authCheck.checkAuth(request, response)) {
return;
}
if (request.headers.host && request.headers.host.match(statusRegex)) {
new statusRequestHandler(request, response);
} else if (request.headers.host && request.headers.host.match(parasoupRegex)) {
new parasoupRequestHandler(request, response);
} else {
new nonAssetRequestHandler(request, response);
}
}
}
};
var statusProvider = [];
statusProvider.push(function() {
var start = new Date();
var requestRing = new AverageRing(120 * options.statsPerSecond);
return function() {
var s = 'redirects: ' + ( options.stats.redirects || 0 ) + '\n';
var reqs = ( options.stats.requests || 0 );
requestRing.add(reqs);
var rpm = requestRing.getAveragePerTime(2, 1000);
s += 'requests: ' + reqs + ' ' + rpm + '/s';
return s;
};
} ());
statusProvider.push(function() {
var start = new Date();
var parasoupRing = new AverageRing(120 * options.statsPerSecond);
var byteSumRing = new AverageRing(120 * options.statsPerSecond);
var cacheHitRing = new AverageRing(120 * options.statsPerSecond);
var responseTimeRing = new AverageRing(120 * options.statsPerSecond);
return function() {
var maxLines = 10;
var convertToHumanReadable = function(bytes) {
var factors = [[1, 'B'], [1024, 'KB'], [1048576, 'MB'], [1073741824, 'GB']];
var ret = '';
for (var i = 0; i < factors.length; i++) {
if (i + 1 >= factors.length || (bytes > factors[i][0] && bytes < factors[i + 1][0]) || bytes === 0) {
ret = Math.floor((bytes / (factors[i][0])) * 100 ) / 100 + factors[i][1];
break;
}
}
return ret;
};
var sumBytes = 0;
var dataArray = options.stats.dataCount;
for (var i in options.stats.dataCount) {
if (!(i === '' || typeof i == 'undefined' || isNaN(dataArray[i]))) {
sumBytes += dataArray[i];
}
}
var served = options.stats.parasoups;
parasoupRing.add(served);
byteSumRing.add(sumBytes);
cacheHitRing.add(options.stats.cacheHits);
responseTimeRing.add(options.stats.responseTime);
var sps = parasoupRing.getAveragePerTime(2, 1000);
var bps = byteSumRing.getAveragePerTime(2, 1000);
var chps = cacheHitRing.getAveragePerTime(2, 1000);
var art = responseTimeRing.getAveragePerTime(2, 1000);
var chr = (100 / options.stats.assetsServed) * options.stats.cacheHits;
var cacheHitRatio = Math.floor(chr * 100) / 100 ;
var status = '';
status += 'total data served: ' + convertToHumanReadable(sumBytes) + ' ' + convertToHumanReadable(bps) + '/s\n';
status += 'assets on server: ' + options.stats.assetCount + '\n';
status += 'parasoups served: ' + served + ' ' + sps + '/s\n';
status += 'parasoup asset cache: ' + options.stats.parasoupAssetCache + '\n';
status += 'memory cache hits: ' + options.stats.cacheHits + ' ' + chps + '/s ' + cacheHitRatio + '%\n';
status += 'soup server errors: ' + options.stats.soupErrors + '\n';
status += 'average response time: ' + art + 'ms';
return status;
};
} ());
statusProvider.push(function() {
return 'active logins: ' + authCheck.getActiveLoginCount();
});
statusProvider.push(options.assetLoader.getStatus);
statusProvider.push(function() {
if (fs.existsSync('./externalMessage')) {
return fs.readFileSync('./externalMessage');
} else {
return '';
}
});
options.statPrinter = new statPrinter(statusProvider, options.statsPerSecond);
options.logger.info("startup done");
options.logger.console("startup done");
};
function dropPrivsAndStart() {
process.setuid(options.uid);
async.series({
logger: function(cb) {
options.logger = new Logger(options, function(err) {
console.log('logger ready');
cb(err);
});
},
cacheHandler: function(cb) {
new Cache(options, function(err, cacheHandler) {
if (err) {
return cb(err);
} else {
console.log('cachehandler ready');
options.logger.console('mongodb connected');
options.cacheHandler = cacheHandler;
cb(null);
}
});
}
}, function(err) {
if (err) {
throw err;
} else {
options.logger.info("startup");
options.logger.console("startup");
startupComponents(options);
}
});
}
function onListen(listener) {
onListen.listeners++;
console.log('listen', listener)
if (options.listen.length === onListen.listeners) {
dropPrivsAndStart();
}
}
onListen.listeners = 0;
server = http.createServer(requestDispatcher);
options.listen.forEach(function(l) {
console.log('try listen', l);
server.listen(l.port, l.ip, function() {
onListen(l);
});
});