1
1
var Cookies = Npm . require ( 'cookies' ) ;
2
2
3
- var workers = null ;
4
-
5
- // We need to start the worker pool after the server binded
6
- // This allow use to play nicely with tools like userdown
7
- WebApp . onListening ( function ( ) {
8
- var workersCount = Balancer . _getWorkersCount ( ) ;
9
- workers = new WorkerPool ( workersCount ) ;
10
- } ) ;
11
-
12
3
Balancer . handleHttp = function handleHttp ( req , res ) {
13
- if ( ! Cluster . discovery ) return processHereHTTP ( ) ;
4
+ if ( ! Cluster . discovery ) return Balancer . _processHereHTTP ( req , res ) ;
14
5
15
6
// if this is from a balance, we don't need to proxy it
16
7
if ( req . headers [ 'from-balancer' ] ) {
17
- return processHereHTTP ( ) ;
8
+ return Balancer . _processHereHTTP ( req , res ) ;
18
9
}
19
10
20
11
var cookies = new Cookies ( req , res ) ;
@@ -47,11 +38,11 @@ Balancer.handleHttp = function handleHttp(req, res) {
47
38
// we can get the endpointHash from the cookie
48
39
var endpointHash = cookies . get ( 'cluster-endpoint' ) ;
49
40
var endpoint = Balancer . _pickEndpoint ( endpointHash , cookies ) ;
50
- if ( ! endpoint ) return processHereHTTP ( ) ;
41
+ if ( ! endpoint ) return Balancer . _processHereHTTP ( req , res ) ;
51
42
}
52
43
53
44
if ( endpoint === Cluster . _endpoint ) {
54
- return processHereHTTP ( ) ;
45
+ return Balancer . _processHereHTTP ( req , res ) ;
55
46
}
56
47
57
48
Balancer . _setFromBalanceUrlHeader ( req ) ;
@@ -60,11 +51,11 @@ Balancer.handleHttp = function handleHttp(req, res) {
60
51
} ;
61
52
62
53
Balancer . handleWs = function handleWs ( req , socket , head ) {
63
- if ( ! Cluster . discovery ) return processHereWS ( req , socket , head ) ;
54
+ if ( ! Cluster . discovery ) return Balancer . _processHereWS ( req , socket , head ) ;
64
55
65
56
if ( req . headers [ 'from-balancer' ] ) {
66
57
// if this is from a balance, we don't need to proxy it
67
- return processHereWS ( req , socket , head )
58
+ return Balancer . _processHereWS ( req , socket , head )
68
59
}
69
60
70
61
// try to get endpointHash from the our cluster-ddp url
@@ -86,16 +77,16 @@ Balancer.handleWs = function handleWs(req, socket, head) {
86
77
// just process here. We don't need to route it to a random web service
87
78
// because, it is possible that this endpoint is for some other service
88
79
// than web.
89
- return processHereWS ( req , socket , head ) ;
80
+ return Balancer . _processHereWS ( req , socket , head ) ;
90
81
}
91
82
}
92
83
93
84
if ( ! endpoint ) {
94
- return processHereWS ( req , socket , head ) ;
85
+ return Balancer . _processHereWS ( req , socket , head ) ;
95
86
}
96
87
97
88
if ( endpoint === Cluster . _endpoint ) {
98
- return processHereWS ( req , socket , head ) ;
89
+ return Balancer . _processHereWS ( req , socket , head ) ;
99
90
}
100
91
101
92
Balancer . _setFromBalanceUrlHeader ( req ) ;
@@ -104,25 +95,4 @@ Balancer.handleWs = function handleWs(req, socket, head) {
104
95
} ;
105
96
106
97
OverShadowServerEvent ( 'request' , Balancer . handleHttp ) ;
107
- OverShadowServerEvent ( 'upgrade' , Balancer . handleWs ) ;
108
-
109
- // Process locally. If there are any workers running, proxy DDP traffic to them
110
-
111
- function processHereHTTP ( ) {
112
- return false ;
113
- }
114
-
115
- function processHereWS ( req , socket , head ) {
116
- if ( process . env [ 'CLUSTER_WORKER_ID' ] ) return ;
117
-
118
- var worker = workers && workers . pickWorker ( ) ;
119
- // No worker, can't proxy. So process here.
120
- if ( ! worker ) return false ;
121
-
122
- var target = { host : "127.0.0.1" , port : worker . port } ;
123
- Balancer . proxy . ws ( req , socket , head , { target : target } , function ( error ) {
124
- // not sure we can re-try websockets, simply log it
125
- console . error ( "Cluster: WS proxying to the worker:" , worker , error . message ) ;
126
- } ) ;
127
- return true ;
128
- }
98
+ OverShadowServerEvent ( 'upgrade' , Balancer . handleWs ) ;
0 commit comments