-
Notifications
You must be signed in to change notification settings - Fork 80
/
utils.js
180 lines (153 loc) · 5.33 KB
/
utils.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
var urlParse = Npm.require('url').parse;
var urlResolve = Npm.require('url').resolve;
Balancer._buildCookie = function _buildCookie(name, service) {
return name + "::" + service;
};
Balancer._urlToTarget = function _urlToTarget(url) {
var parsedUrl = urlParse(url);
var target = {
host: parsedUrl.hostname,
port: parsedUrl.port
};
return target;
};
Balancer._pickAndSetEndpointHash =
function _pickAndSetEndpointHash(cookies) {
var service = Cluster._uiService;
var endpointHash = Cluster.discovery.pickEndpointHash(service);
if(endpointHash) {
var cookieOptions = {
httpOnly: true
};
var cookieName = Balancer._buildCookie('cluster-endpoint', service);
cookies.set(cookieName, endpointHash, cookieOptions);
return endpointHash;
} else {
// no endpoint point, simply process the request here
return false;
}
};
Balancer._pickEndpoint =
function _pickEndpoint(endpointHash, cookies, retries) {
retries = retries || 0;
if(retries > 1) return false;
if(!endpointHash) {
endpointHash = Balancer._pickAndSetEndpointHash(cookies);
if(!endpointHash) {
// no endpoint, simply process the request here
return false;
}
}
var endpoint = Cluster.discovery.hashToEndpoint(endpointHash);
if(!endpoint) {
// oops, no such endpoint. Let's get a new one.
return Balancer._pickEndpoint(null, cookies, ++retries);
}
return endpoint;
};
Balancer._pickJustEndpoint = function _pickJustEndpoint(endpointHash, service) {
if(!service) throw new Error("service name is required");
if(!endpointHash) {
// no hash, pick a new endpoint
return Cluster.discovery.pickEndpoint(service);
}
var endpoint = Cluster.discovery.hashToEndpoint(endpointHash);
if(!endpoint) {
// oops, no such endpoint for this hash
// pick a new endpoint
return Cluster.discovery.pickEndpoint(service);
}
return endpoint;
};
Balancer._setFromBalanceUrlHeader =
function _setFromBalanceUrlHeader(req, balancerUrl) {
req.headers['from-balancer'] = "YES";
};
Balancer._sendSockJsInfo =
function _sendSockJsInfo(req, res, cookies) {
var urlParsed = req.url.match(/(\w+)\/sockjs\/info/);
var serviceName = urlParsed && urlParsed[1];
if(!serviceName) {
serviceName = Cluster._uiService;
}
if(serviceName === Cluster._uiService) {
var cookieName = Balancer._buildCookie('cluster-endpoint', serviceName);
var endpointHash = cookies.get(cookieName);
var endpoint = Balancer._pickJustEndpoint(endpointHash, serviceName);
} else if(Cluster._isPublicService(serviceName)) {
var endpoint = Balancer._pickJustEndpoint(null, serviceName);
}
if(!endpoint) {
// we can't process this here.
// TODO: better error handling
console.error("Cluster: no such endpoint for service:" + serviceName);
res.writeHead(404);
return res.end("no such endpoint for service: " + serviceName);
}
var endpointHash = Cluster.discovery.endpointToHash(endpoint);
// when no balancer url is provided,
// using ROOT_URL instead of relative path
var balancer =
Cluster.discovery.pickBalancer(endpointHash) ||
process.env.ROOT_URL;
// add tail backslash if path not end with it
balancer = (/\/$/).test(balancer)? balancer : balancer + '/';
var base_url =
urlResolve(balancer, "cluster-ddp/" + endpointHash + "/" + serviceName);
var info = {
websocket: !process.env['DISABLE_WEBSOCKETS'],
origins: ["*:*"],
cookie_needed: false,
entropy: Math.ceil(Math.random() * 9999999999),
base_url: base_url
};
res.writeHead(200, {
'content-type': 'application/json',
'access-control-allow-origin': '*'
});
res.end(JSON.stringify(info));
}
Balancer._rewriteDdpUrl = function _rewriteDdpUrl(req) {
var regExp = /cluster-ddp\/(\w+)\/(\w+)/;
var parsedUrl = req.url.match(regExp);
if(parsedUrl) {
req.url = req.url.replace(regExp, "sockjs/").replace(/[\/]+/g, "/");
var endpointHash = {
hash: parsedUrl[1],
service: parsedUrl[2] || Cluster._uiService
};
return endpointHash;
}
};
Balancer._proxyWeb = function _proxyWeb(req, res, endpoint, cookies, retries) {
// give support for sockjs long polling
// otherwise meteor kill the connection after 5 secs by default
if(/sockjs/.test(req.url)) {
res.setTimeout(2 * 60 * 1000);
}
retries = retries || 0;
var target = Balancer._urlToTarget(endpoint);
Balancer.proxy.web(req, res, {target: target}, function(error) {
// It's pretty hard to do re-trying ourselves,
// because of the SOCKJS protocol
// And it's possible to have we've already send headers and data
// So, we don't simply retry here. Let it handle by the client
// We can't also send 500 error because of the above issue
printError("Connection dropped", true);
function printError(message, dontSendHeaders) {
message = message || error.message;
console.error("Cluster: web proxy error: ", message);
if(!dontSendHeaders) {
res.writeHead(500);
res.end("Internal Error: Please reload.");
}
}
});
};
Balancer._proxyWs = function proxyWs(req, socket, head, endpoint) {
var target = Balancer._urlToTarget(endpoint);
Balancer.proxy.ws(req, socket, head, {target: target}, function(error) {
// not sure we can re-try websockets, simply log it
console.error("WS proxying failed! to: ", endpoint, "err:", error.message);
});
};