-
Notifications
You must be signed in to change notification settings - Fork 80
/
route.js
106 lines (87 loc) · 3.1 KB
/
route.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
var Cookies = Npm.require('cookies');
Balancer.handleHttp = function handleHttp(req, res) {
if(process.env['CLUSTER_WORKER_ID']) {
return false;
}
if(!Cluster.discovery) return Balancer._processHereHTTP(req, res);
// if this is from a balance, we don't need to proxy it
if(req.headers['from-balancer']) {
return Balancer._processHereHTTP(req, res);
}
var cookies = new Cookies(req, res);
if(/\/sockjs\/info/.test(req.url)) {
Balancer._sendSockJsInfo(req, res, cookies);
return true;
}
// try to get endpointHash from the our cluster-ddp url
// this is for sockjs long polling requets
var endpointInfo = Balancer._rewriteDdpUrl(req);
if(endpointInfo) {
var endpoint =
Balancer._pickJustEndpoint(endpointInfo.hash, endpointInfo.service);
if(!endpoint) {
// seems like sockjs connection is not there yet!
// let's end it here.
var message = "Cluster: there is no endpoint but we've a hash: ";
console.error(message + endpointInfo.hash);
res.writeHead(500);
res.end();
return true;
}
}
if(!endpointInfo) {
// seems like this is just static resources
// we can get the endpointHash from the cookie
var endpointHash = cookies.get('cluster-endpoint');
var endpoint = Balancer._pickEndpoint(endpointHash, cookies);
if(!endpoint) return Balancer._processHereHTTP(req, res);
}
if(endpoint === Cluster._endpoint) {
return Balancer._processHereHTTP(req, res);
}
Balancer._setFromBalanceUrlHeader(req);
Balancer._proxyWeb(req, res, endpoint, cookies);
return true;
};
Balancer.handleWs = function handleWs(req, socket, head) {
if(process.env['CLUSTER_WORKER_ID']) {
return false;
}
if(!Cluster.discovery) return Balancer._processHereWS(req, socket, head);
if(req.headers['from-balancer']) {
// if this is from a balance, we don't need to proxy it
return Balancer._processHereWS(req, socket, head)
}
// try to get endpointHash from the our cluster-ddp url
var endpointInfo = Balancer._rewriteDdpUrl(req);
if(endpointInfo) {
var endpoint =
Balancer._pickJustEndpoint(endpointInfo.hash, endpointInfo.service);
}
// try to get the endpointHash from the cookie
// this is when there is no balancer url
if(!endpointInfo) {
var cookies = new Cookies(req);
var endpointHash = cookies.get('cluster-endpoint');
if(endpointHash) {
var endpoint = Balancer._pickJustEndpoint(endpointHash);
} else {
// seems like a direct connection
// just process here. We don't need to route it to a random web service
// because, it is possible that this endpoint is for some other service
// than web.
return Balancer._processHereWS(req, socket, head);
}
}
if(!endpoint) {
return Balancer._processHereWS(req, socket, head);
}
if(endpoint === Cluster._endpoint) {
return Balancer._processHereWS(req, socket, head);
}
Balancer._setFromBalanceUrlHeader(req);
Balancer._proxyWs(req, socket, head, endpoint);
return true;
};
OverShadowServerEvent('request', Balancer.handleHttp);
OverShadowServerEvent('upgrade', Balancer.handleWs);