@@ -107,54 +107,64 @@ const metricsServer = http.createServer((req, res) => {
107107
108108 // Health check endpoint
109109 if ( url . pathname === '/health' || url . pathname === '/healthz' ) {
110+ let responseSent = false ;
111+
112+ const sendResponse = ( statusCode , message ) => {
113+ if ( responseSent ) return ;
114+ responseSent = true ;
115+ res . writeHead ( statusCode , { 'Content-Type' : 'text/plain' } ) ;
116+ res . end ( message ) ;
117+ } ;
118+
110119 // Check Firewalla health endpoint directly
111120 const healthCheck = http . get ( `${ FIREWALLA_HEALTH_URL } /healthz` , ( healthRes ) => {
112121 if ( healthRes . statusCode === 200 ) {
113- res . writeHead ( 200 , { 'Content-Type' : 'text/plain' } ) ;
114- res . end ( 'ok' ) ;
122+ sendResponse ( 200 , 'ok' ) ;
115123 } else {
116- res . writeHead ( 503 , { 'Content-Type' : 'text/plain' } ) ;
117- res . end ( 'unhealthy' ) ;
124+ sendResponse ( 503 , 'unhealthy' ) ;
118125 }
119126 } ) ;
120-
127+
121128 healthCheck . on ( 'error' , ( err ) => {
122129 console . error ( 'Health check failed:' , err . message ) ;
123- res . writeHead ( 503 , { 'Content-Type' : 'text/plain' } ) ;
124- res . end ( 'unhealthy' ) ;
130+ sendResponse ( 503 , 'unhealthy' ) ;
125131 } ) ;
126-
132+
127133 healthCheck . setTimeout ( 5000 , ( ) => {
128134 healthCheck . destroy ( ) ;
129- res . writeHead ( 503 , { 'Content-Type' : 'text/plain' } ) ;
130- res . end ( 'unhealthy - timeout' ) ;
135+ sendResponse ( 503 , 'unhealthy - timeout' ) ;
131136 } ) ;
132137 return ;
133138 }
134139
135140 // Readiness check endpoint
136141 if ( url . pathname === '/ready' || url . pathname === '/readyz' ) {
142+ let responseSent = false ;
143+
144+ const sendResponse = ( statusCode , message ) => {
145+ if ( responseSent ) return ;
146+ responseSent = true ;
147+ res . writeHead ( statusCode , { 'Content-Type' : 'text/plain' } ) ;
148+ res . end ( message ) ;
149+ } ;
150+
137151 // Check if we can reach Firewalla
138152 const healthCheck = http . get ( `${ FIREWALLA_HEALTH_URL } /healthz` , ( healthRes ) => {
139153 if ( healthRes . statusCode === 200 ) {
140- res . writeHead ( 200 , { 'Content-Type' : 'text/plain' } ) ;
141- res . end ( 'ready' ) ;
154+ sendResponse ( 200 , 'ready' ) ;
142155 } else {
143- res . writeHead ( 503 , { 'Content-Type' : 'text/plain' } ) ;
144- res . end ( 'not ready' ) ;
156+ sendResponse ( 503 , 'not ready' ) ;
145157 }
146158 } ) ;
147-
159+
148160 healthCheck . on ( 'error' , ( err ) => {
149161 console . error ( 'Readiness check failed:' , err . message ) ;
150- res . writeHead ( 503 , { 'Content-Type' : 'text/plain' } ) ;
151- res . end ( 'not ready' ) ;
162+ sendResponse ( 503 , 'not ready' ) ;
152163 } ) ;
153-
164+
154165 healthCheck . setTimeout ( 5000 , ( ) => {
155166 healthCheck . destroy ( ) ;
156- res . writeHead ( 503 , { 'Content-Type' : 'text/plain' } ) ;
157- res . end ( 'not ready - timeout' ) ;
167+ sendResponse ( 503 , 'not ready - timeout' ) ;
158168 } ) ;
159169 return ;
160170 }
0 commit comments