-
Notifications
You must be signed in to change notification settings - Fork 12
/
request.js
233 lines (222 loc) · 6.29 KB
/
request.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
'use strict';
var stream = require('stream');
var MSG_TYPE = require('./consts').MSG_TYPE;
var PROTOCOL_STATUS = require('./consts').PROTOCOL_STATUS;
var createConnection = require('./connection');
var BEGIN_REQUEST_DATA_NO_KEEP_CONN = new Buffer('\0\x01\0\0\0\0\0\0'); // FCGI_ROLE_RESPONDER && !FCGI_KEEP_CONN
var BEGIN_REQUEST_DATA_KEEP_CONN = new Buffer('\0\x01\x01\0\0\0\0\0'); // FCGI_ROLE_RESPONDER && FCGI_KEEP_CONN
module.exports = function(client){
var multiConnMode = !client._mpxsConns;
var maxConcurrent = multiConnMode ? client._maxConns : client._maxReqs;
var curConcurrent = 0;
// multi conn mode
var allocReq = null;
if(multiConnMode) {
// build connection for each request
allocReq = function(cb){
if(curConcurrent >= maxConcurrent) {
setImmediate(function(){
cb(new Error('Requests reached maximum allowed concurrency limit.'));
});
return;
}
curConcurrent++;
var conn = createConnection(client);
var cbCalled = false;
var endData = null;
var handlers = {
stdout: function(){},
stderr: function(){},
end: function(){},
};
conn.on('error', function(err){
return;
});
conn.on('close', function(){
curConcurrent--;
if(cbCalled) {
handlers.end(endData);
} else {
cb(new Error('Cannot send request to server.'));
}
});
conn.on('message-' + MSG_TYPE.FCGI_STDOUT, function(reqId, data){
handlers.stdout(data);
});
conn.on('message-' + MSG_TYPE.FCGI_STDERR, function(reqId, data){
handlers.stderr(data);
});
conn.on('message-' + MSG_TYPE.FCGI_END_REQUEST, function(reqId, data){
endData = data;
});
conn.on('connect', function(){
cbCalled = true;
cb(null, conn, 1, handlers);
});
};
} else {
// connection manager for non-multiConnMode
var handlers = new Array(maxConcurrent);
var waitNextConnect = [];
var nextReqId = 0;
var conn = null;
var newConn = function(){
conn = createConnection(client);
conn.on('connect', function(){
nextReqId = 1;
var funcs = waitNextConnect;
waitNextConnect = [];
for(var i=0; i<funcs.length; i++) {
funcs[i]();
}
});
conn.on('error', function(err){
if(nextReqId) {
nextReqId = 0;
} else if(waitNextConnect.length) {
// connecting error again with requests in queue
for(var i=0; i < waitNextConnect.length; i++) {
waitNextConnect[i](new Error('Cannot send request to server.'));
}
waitNextConnect = [];
}
});
conn.on('close', function(){
var endFuncs = [];
if(curConcurrent) {
// clear reqs with req ids
for(var i=0; i<handlers.length; i++) {
if(handlers[i]) {
endFuncs.push(handlers[i].end);
handlers[i] = null;
curConcurrent--;
}
}
}
conn = null;
nextReqId = 0;
if(waitNextConnect.length) {
newConn();
}
for(var i=0; i<endFuncs.length; i++) {
endFuncs[i]();
}
});
conn.on('message-' + MSG_TYPE.FCGI_STDOUT, function(reqId, data){
handlers[reqId].stdout(data);
});
conn.on('message-' + MSG_TYPE.FCGI_STDERR, function(reqId, data){
handlers[reqId].stderr(data);
});
conn.on('message-' + MSG_TYPE.FCGI_END_REQUEST, function(reqId, data){
curConcurrent--;
var endFunc = handlers[reqId].end;
handlers[reqId] = null;
// check whether connection is needed to be rebuilt
if(!curConcurrent) {
nextReqId = 0;
conn.end();
} else if(nextReqId >= 65536) {
conn.end();
}
endFunc(data);
});
};
allocReq = function(cb){
// use current connection
if(curConcurrent >= maxConcurrent) {
setImmediate(function(){
cb(new Error('Requests reached maximum allowed concurrency limit.'));
});
return;
}
curConcurrent++;
var funcs = {
stdout: function(){},
stderr: function(){},
end: function(){},
};
if(nextReqId && nextReqId < 65536) {
// alloc a new req id
var reqId = nextReqId++;
handlers[reqId] = funcs;
setImmediate(function(){
cb(null, conn, reqId, funcs);
});
return;
}
waitNextConnect.push(function(err){
// wait connection to be available
if(err) {
cb(err);
return;
}
var reqId = nextReqId++;
handlers[reqId] = funcs;
cb(null, conn, reqId, funcs);
});
if(!conn) newConn();
};
}
return function(params, cb){
allocReq(function(err, conn, reqId, handlers){
if(err) {
cb(err);
return;
}
var exitStatus = null;
var stdin = new stream.Writable();
stdin._write = function(chunk, encoding, cb){
conn.send(MSG_TYPE.FCGI_STDIN, reqId, chunk, cb);
};
stdin.on('finish', function(){
conn.send(MSG_TYPE.FCGI_STDIN, reqId, null);
});
var stdout = new stream.Readable();
stdout._read = function(n){};
var stderr = new stream.Readable();
stderr._read = function(n){};
handlers.stdout = function(data){
stdout.push(data);
};
handlers.stderr = function(data){
stderr.push(data);
};
handlers.end = function(data){
if(data) {
var exitCode = data.readInt32BE(0, true);
var protocolStatus = data.readUInt8(4, true);
if(protocolStatus === PROTOCOL_STATUS.FCGI_CANT_MPX_CONN) {
exitStatus = new Error('Server rejected request: exceeds maximum number of concurrent requests.');
} else if(protocolStatus === PROTOCOL_STATUS.FCGI_OVERLOADED) {
exitStatus = new Error('Server rejected request: resource not available.');
} else if(protocolStatus === PROTOCOL_STATUS.FCGI_UNKNOWN_ROLE) {
exitStatus = new Error('Server rejected request: FastCGI role not supported.');
} else {
exitStatus = exitCode;
}
} else {
exitStatus = new Error('Request unexpectedly ended.');
}
stdout.push(null);
stderr.push(null);
};
if(multiConnMode) conn.send(MSG_TYPE.FCGI_BEGIN_REQUEST, reqId, BEGIN_REQUEST_DATA_NO_KEEP_CONN);
else conn.send(MSG_TYPE.FCGI_BEGIN_REQUEST, reqId, BEGIN_REQUEST_DATA_KEEP_CONN);
conn.send(MSG_TYPE.FCGI_PARAMS, reqId, params);
conn.send(MSG_TYPE.FCGI_PARAMS, reqId, null);
// TODO add force end method to let server run with single req
cb(null, {
abort: function(){
conn.send(MSG_TYPE.FCGI_ABORT_REQUEST, reqId, new Buffer(0));
},
stdin: stdin,
stdout: stdout,
stderr: stderr,
getExitStatus: function(){
return exitStatus;
}
});
});
};
};