11var Cookies = Npm . require ( 'cookies' ) ;
22
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-
123Balancer . handleHttp = function handleHttp ( req , res ) {
13- if ( ! Cluster . discovery ) return processHereHTTP ( ) ;
4+ if ( ! Cluster . discovery ) return Balancer . _processHereHTTP ( req , res ) ;
145
156 // if this is from a balance, we don't need to proxy it
167 if ( req . headers [ 'from-balancer' ] ) {
17- return processHereHTTP ( ) ;
8+ return Balancer . _processHereHTTP ( req , res ) ;
189 }
1910
2011 var cookies = new Cookies ( req , res ) ;
@@ -47,11 +38,11 @@ Balancer.handleHttp = function handleHttp(req, res) {
4738 // we can get the endpointHash from the cookie
4839 var endpointHash = cookies . get ( 'cluster-endpoint' ) ;
4940 var endpoint = Balancer . _pickEndpoint ( endpointHash , cookies ) ;
50- if ( ! endpoint ) return processHereHTTP ( ) ;
41+ if ( ! endpoint ) return Balancer . _processHereHTTP ( req , res ) ;
5142 }
5243
5344 if ( endpoint === Cluster . _endpoint ) {
54- return processHereHTTP ( ) ;
45+ return Balancer . _processHereHTTP ( req , res ) ;
5546 }
5647
5748 Balancer . _setFromBalanceUrlHeader ( req ) ;
@@ -60,11 +51,11 @@ Balancer.handleHttp = function handleHttp(req, res) {
6051} ;
6152
6253Balancer . 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 ) ;
6455
6556 if ( req . headers [ 'from-balancer' ] ) {
6657 // 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 )
6859 }
6960
7061 // try to get endpointHash from the our cluster-ddp url
@@ -86,16 +77,16 @@ Balancer.handleWs = function handleWs(req, socket, head) {
8677 // just process here. We don't need to route it to a random web service
8778 // because, it is possible that this endpoint is for some other service
8879 // than web.
89- return processHereWS ( req , socket , head ) ;
80+ return Balancer . _processHereWS ( req , socket , head ) ;
9081 }
9182 }
9283
9384 if ( ! endpoint ) {
94- return processHereWS ( req , socket , head ) ;
85+ return Balancer . _processHereWS ( req , socket , head ) ;
9586 }
9687
9788 if ( endpoint === Cluster . _endpoint ) {
98- return processHereWS ( req , socket , head ) ;
89+ return Balancer . _processHereWS ( req , socket , head ) ;
9990 }
10091
10192 Balancer . _setFromBalanceUrlHeader ( req ) ;
@@ -104,25 +95,4 @@ Balancer.handleWs = function handleWs(req, socket, head) {
10495} ;
10596
10697OverShadowServerEvent ( '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