Skip to content

Commit ae14eb5

Browse files
committed
Merge branch 'multi-core'
2 parents 493414e + 1ccb7ae commit ae14eb5

File tree

13 files changed

+814
-130
lines changed

13 files changed

+814
-130
lines changed

lib/server/api.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ Cluster.connect = function(connUrl) {
88
var backendName = matched[1];
99
var backend = this._discoveryBackends[backendName];
1010
if(!backend) {
11-
throw new Error("cluster: no discovery backend named " + backendName);
11+
throw new Error("Cluster: no discovery backend named " + backendName);
1212
}
1313

1414
this.discovery = backend;
15-
console.info("cluster: connecting to '%s' discovery backend", backendName);
15+
console.info("Cluster: connecting to '%s' discovery backend", backendName);
1616
this.discovery.connect(connUrl);
1717

1818
var warnMessage = "trying to connect, but already connected";
1919
Cluster.connect = Cluster._blockCallAgain(warnMessage);
2020
} else {
21-
throw new Error("cluster: connect url should an url");
21+
throw new Error("Cluster: connect url should an url");
2222
}
2323
};
2424

@@ -28,7 +28,7 @@ Cluster.allowPublicAccess = function allowPublicAccess(serviceList) {
2828
serviceList = _.toArray(arguments);
2929
}
3030

31-
var message = "cluster: allow public access for '%s' service(s)";
31+
var message = "Cluster: allow public access for '%s' service(s)";
3232
console.info(message, serviceList.join(", "));
3333

3434
serviceList.forEach(function(service) {
@@ -51,7 +51,7 @@ Cluster.register = function register(name, options) {
5151
options.endpoint = options.endpoint || process.env.CLUSTER_ENDPOINT_URL;
5252

5353
if(!options.endpoint) {
54-
console.info("cluster: using ROOT_URL as the cluster endpoint");
54+
console.info("Cluster: using ROOT_URL as the cluster endpoint");
5555
options.endpoint = process.env.ROOT_URL;
5656
}
5757

@@ -68,9 +68,9 @@ Cluster.register = function register(name, options) {
6868
process.env.CLUSTER_UI_SERVICE ||
6969
name;
7070

71-
console.info("cluster: registering this node as service '%s'", name);
72-
console.info("cluster: endpoint url =", options.endpoint);
73-
console.info("cluster: balancer url =", options.balancer);
71+
console.info("Cluster: registering this node as service '%s'", name);
72+
console.info("Cluster: endpoint url =", options.endpoint);
73+
console.info("Cluster: balancer url =", options.balancer);
7474
this.discovery.register(name, options);
7575

7676
Cluster.register = Cluster._blockCallAgain("already registered as - " + name);
@@ -87,6 +87,6 @@ function registerDiscoveryBackend(name, backend) {
8787

8888
Cluster._blockCallAgain = function(message) {
8989
return function() {
90-
throw new Error("cluster: " + message);
90+
throw new Error("Cluster: " + message);
9191
};
9292
};

lib/server/balancer/namespace.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var HttpProxy = Npm.require('http-proxy');
2+
3+
Balancer = {};
4+
Balancer.proxy = HttpProxy.createProxyServer({
5+
xfwd: true
6+
});

lib/server/balancer/route.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
var Cookies = Npm.require('cookies');
2+
3+
Balancer.handleHttp = function handleHttp(req, res) {
4+
if(!Cluster.discovery) return Balancer._processHereHTTP(req, res);
5+
6+
// if this is from a balance, we don't need to proxy it
7+
if(req.headers['from-balancer']) {
8+
return Balancer._processHereHTTP(req, res);
9+
}
10+
11+
var cookies = new Cookies(req, res);
12+
13+
if(/\/sockjs\/info/.test(req.url)) {
14+
Balancer._sendSockJsInfo(req, res, cookies);
15+
return true;
16+
}
17+
18+
// try to get endpointHash from the our cluster-ddp url
19+
// this is for sockjs long polling requets
20+
var endpointInfo = Balancer._rewriteDdpUrl(req);
21+
if(endpointInfo) {
22+
var endpoint =
23+
Balancer._pickJustEndpoint(endpointInfo.hash, endpointInfo.service);
24+
25+
if(!endpoint) {
26+
// seems like sockjs connection is not there yet!
27+
// let's end it here.
28+
var message = "Cluster: there is no endpoint but we've a hash: ";
29+
console.error(message + endpointInfo.hash);
30+
res.writeHead(500);
31+
res.end();
32+
return true;
33+
}
34+
}
35+
36+
if(!endpointInfo) {
37+
// seems like this is just static resources
38+
// we can get the endpointHash from the cookie
39+
var endpointHash = cookies.get('cluster-endpoint');
40+
var endpoint = Balancer._pickEndpoint(endpointHash, cookies);
41+
if(!endpoint) return Balancer._processHereHTTP(req, res);
42+
}
43+
44+
if(endpoint === Cluster._endpoint) {
45+
return Balancer._processHereHTTP(req, res);
46+
}
47+
48+
Balancer._setFromBalanceUrlHeader(req);
49+
Balancer._proxyWeb(req, res, endpoint, cookies);
50+
return true;
51+
};
52+
53+
Balancer.handleWs = function handleWs(req, socket, head) {
54+
if(!Cluster.discovery) return Balancer._processHereWS(req, socket, head);
55+
56+
if(req.headers['from-balancer']) {
57+
// if this is from a balance, we don't need to proxy it
58+
return Balancer._processHereWS(req, socket, head)
59+
}
60+
61+
// try to get endpointHash from the our cluster-ddp url
62+
var endpointInfo = Balancer._rewriteDdpUrl(req);
63+
if(endpointInfo) {
64+
var endpoint =
65+
Balancer._pickJustEndpoint(endpointInfo.hash, endpointInfo.service);
66+
}
67+
68+
// try to get the endpointHash from the cookie
69+
// this is when there is no balancer url
70+
if(!endpointInfo) {
71+
var cookies = new Cookies(req);
72+
var endpointHash = cookies.get('cluster-endpoint');
73+
if(endpointHash) {
74+
var endpoint = Balancer._pickJustEndpoint(endpointHash);
75+
} else {
76+
// seems like a direct connection
77+
// just process here. We don't need to route it to a random web service
78+
// because, it is possible that this endpoint is for some other service
79+
// than web.
80+
return Balancer._processHereWS(req, socket, head);
81+
}
82+
}
83+
84+
if(!endpoint) {
85+
return Balancer._processHereWS(req, socket, head);
86+
}
87+
88+
if(endpoint === Cluster._endpoint) {
89+
return Balancer._processHereWS(req, socket, head);
90+
}
91+
92+
Balancer._setFromBalanceUrlHeader(req);
93+
Balancer._proxyWs(req, socket, head, endpoint);
94+
return true;
95+
};
96+
97+
OverShadowServerEvent('request', Balancer.handleHttp);
98+
OverShadowServerEvent('upgrade', Balancer.handleWs);

lib/server/balancer.js renamed to lib/server/balancer/utils.js

Lines changed: 3 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
1-
var Cookies = Npm.require('cookies');
2-
var HttpProxy = Npm.require('http-proxy');
31
var urlParse = Npm.require('url').parse;
42
var urlResolve = Npm.require('url').resolve;
53

6-
Balancer = {};
7-
Balancer.proxy = HttpProxy.createProxyServer({
8-
xfwd: true
9-
});
10-
114
Balancer._buildCookie = function _buildCookie(name, service) {
125
return name + "::" + service;
136
};
@@ -108,7 +101,7 @@ function _sendSockJsInfo(req, res, cookies) {
108101
if(!endpoint) {
109102
// we can't process this here.
110103
// TODO: better error handling
111-
console.error("cluster: no such endpoint for service:" + serviceName);
104+
console.error("Cluster: no such endpoint for service:" + serviceName);
112105
res.writeHead(404);
113106
return res.end("no such endpoint for service: " + serviceName);
114107
}
@@ -170,7 +163,7 @@ Balancer._proxyWeb = function _proxyWeb(req, res, endpoint, cookies, retries) {
170163
}
171164

172165
function printError() {
173-
console.error("cluster: web proxy error: ", error.message);
166+
console.error("Cluster: web proxy error: ", error.message);
174167
res.writeHead(500);
175168
res.end("Internal Error: Please reload.");
176169
}
@@ -183,107 +176,4 @@ Balancer._proxyWs = function proxyWs(req, socket, head, endpoint) {
183176
// not sure we can re-try websockets, simply log it
184177
console.error("WS proxying failed! to: " + endpoint);
185178
});
186-
};
187-
188-
Balancer.handleHttp = function handleHttp(req, res) {
189-
if(!Cluster.discovery) return false;
190-
191-
// if this is from a balance, we don't need to proxy it
192-
if(req.headers['from-balancer']) {
193-
return false;
194-
}
195-
196-
var cookies = new Cookies(req, res);
197-
198-
if(/\/sockjs\/info/.test(req.url)) {
199-
Balancer._sendSockJsInfo(req, res, cookies);
200-
return true;
201-
}
202-
203-
// try to get endpointHash from the our cluster-ddp url
204-
// this is for sockjs long polling requets
205-
var endpointInfo = Balancer._rewriteDdpUrl(req);
206-
if(endpointInfo) {
207-
var endpoint =
208-
Balancer._pickJustEndpoint(endpointInfo.hash, endpointInfo.service);
209-
210-
if(!endpoint) {
211-
// seems like sockjs connection is not there yet!
212-
// let's end it here.
213-
var message = "cluster: there is no endpoint but we've a hash: ";
214-
console.error(message + endpointInfo.hash);
215-
res.writeHead(500);
216-
res.end();
217-
return true;
218-
}
219-
}
220-
221-
if(!endpointInfo) {
222-
// seems like this is just static resources
223-
// we can get the endpointHash from the cookie
224-
var serviceName = Cluster._uiService;
225-
var cookieName = Balancer._buildCookie('cluster-endpoint', serviceName);
226-
var endpointHash = cookies.get(cookieName);
227-
var endpoint = Balancer._pickEndpoint(endpointHash, cookies);
228-
if(!endpoint) return false;
229-
}
230-
231-
if(endpoint === Cluster._endpoint) {
232-
return false;
233-
}
234-
235-
Balancer._setFromBalanceUrlHeader(req);
236-
Balancer._proxyWeb(req, res, endpoint, cookies);
237-
return true;
238-
};
239-
240-
Balancer.handleWs = function handleWs(req, socket, head) {
241-
if(!Cluster.discovery) return false;
242-
243-
if(req.headers['from-balancer']) {
244-
// if this is from a balance, we don't need to proxy it
245-
return false
246-
}
247-
248-
// try to get endpointHash from the our cluster-ddp url
249-
var endpointInfo = Balancer._rewriteDdpUrl(req);
250-
if(endpointInfo) {
251-
var endpoint =
252-
Balancer._pickJustEndpoint(endpointInfo.hash, endpointInfo.service);
253-
}
254-
255-
// try to get the endpointHash from the cookie
256-
// this is when there is no balancer url
257-
if(!endpointInfo) {
258-
var cookies = new Cookies(req);
259-
260-
var serviceName = Cluster._uiService;
261-
var cookieName = Balancer._buildCookie('cluster-endpoint', serviceName);
262-
var endpointHash = cookies.get(cookieName);
263-
264-
if(endpointHash) {
265-
var endpoint = Balancer._pickJustEndpoint(endpointHash, serviceName);
266-
} else {
267-
// seems like a direct connection
268-
// just process here. We don't need to route it to a random web service
269-
// because, it is possible that this endpoint is for some other service
270-
// than web.
271-
return false;
272-
}
273-
}
274-
275-
if(!endpoint) {
276-
return false;
277-
}
278-
279-
if(endpoint === Cluster._endpoint) {
280-
return false;
281-
}
282-
283-
Balancer._setFromBalanceUrlHeader(req);
284-
Balancer._proxyWs(req, socket, head, endpoint);
285-
return true;
286-
};
287-
288-
OverShadowServerEvent('request', Balancer.handleHttp);
289-
OverShadowServerEvent('upgrade', Balancer.handleWs);
179+
};

0 commit comments

Comments
 (0)