@@ -211,15 +211,17 @@ export class OAuth2Service {
211211 }
212212
213213 /**
214- * Start periodic health check to reconnect if OIDC server becomes available
215- * Uses exponential backoff: 1min, 2min, 4min, capped at 4min
214+ * Start periodic health check to monitor OIDC availability
215+ * Uses exponential backoff when reconnecting: 1s, 2s, 4s, up to 4min
216+ * Switches to regular monitoring (every 4 minutes) once connected
216217 *
217218 * @param {number } initialIntervalMs - Initial interval in milliseconds (default: 1000 = 1 second)
219+ * @param {number } monitoringIntervalMs - Interval for continuous monitoring when connected (default: 240000 = 4 minutes)
218220 *
219221 * @example
220- * oauth2Service.startHealthCheck(1000) // Start checking at 1 second, then exponential backoff
222+ * oauth2Service.startHealthCheck(1000, 240000 ) // Start checking at 1 second, monitor every 4 minutes when connected
221223 */
222- startHealthCheck ( initialIntervalMs : number = 1000 ) : void {
224+ startHealthCheck ( initialIntervalMs : number = 1000 , monitoringIntervalMs : number = 240000 ) : void {
223225 if ( this . healthCheckInterval ) {
224226 console . log ( 'OAuth2Service: Health check already running' )
225227 return
@@ -235,32 +237,70 @@ export class OAuth2Service {
235237 console . log ( 'OAuth2Service: Starting health check with exponential backoff' )
236238
237239 const scheduleNextCheck = ( ) => {
238- if ( ! this . initialized && this . wellKnownUrl ) {
239- // Calculate delay with exponential backoff, capped at 4 minutes
240- const delay = Math . min ( initialIntervalMs * Math . pow ( 2 , this . healthCheckAttempts ) , 240000 )
241- const delayDisplay =
242- delay < 60000
243- ? `${ ( delay / 1000 ) . toFixed ( 0 ) } second(s)`
244- : `${ ( delay / 60000 ) . toFixed ( 1 ) } minute(s)`
240+ if ( ! this . wellKnownUrl ) {
241+ return
242+ }
245243
244+ let delay : number
245+
246+ if ( this . initialized ) {
247+ // When connected, monitor every 4 minutes
248+ delay = monitoringIntervalMs
249+ } else {
250+ // When disconnected, use exponential backoff
251+ delay = Math . min ( initialIntervalMs * Math . pow ( 2 , this . healthCheckAttempts ) , 240000 )
252+ }
253+
254+ const delayDisplay =
255+ delay < 60000
256+ ? `${ ( delay / 1000 ) . toFixed ( 0 ) } second(s)`
257+ : `${ ( delay / 60000 ) . toFixed ( 1 ) } minute(s)`
258+
259+ if ( this . initialized ) {
260+ console . log ( `OAuth2Service: Monitoring scheduled in ${ delayDisplay } ` )
261+ } else {
246262 console . log (
247263 `OAuth2Service: Health check scheduled in ${ delayDisplay } (attempt ${ this . healthCheckAttempts + 1 } )`
248264 )
265+ }
249266
250- this . healthCheckInterval = setTimeout ( async ( ) => {
267+ this . healthCheckInterval = setTimeout ( async ( ) => {
268+ if ( this . initialized ) {
269+ // When connected, verify OIDC is still available
270+ console . log ( 'OAuth2Service: Verifying OIDC availability...' )
271+ try {
272+ const response = await fetch ( this . wellKnownUrl )
273+ if ( ! response . ok ) {
274+ throw new Error ( `OIDC server returned ${ response . status } ` )
275+ }
276+ console . log ( 'OAuth2Service: OIDC is available' )
277+ // Continue monitoring
278+ scheduleNextCheck ( )
279+ } catch ( error ) {
280+ console . error ( 'OAuth2Service: OIDC server is no longer available!' )
281+ this . initialized = false
282+ this . oidcConfig = null
283+ this . healthCheckAttempts = 0
284+ console . log ( 'OAuth2Service: Attempting to reconnect...' )
285+ // Schedule reconnection with exponential backoff
286+ scheduleNextCheck ( )
287+ }
288+ } else {
289+ // When disconnected, attempt to reconnect
251290 console . log ( 'OAuth2Service: Health check - attempting to reconnect to OIDC server...' )
252291 try {
253292 await this . initializeFromWellKnown ( this . wellKnownUrl )
254293 console . log ( 'OAuth2Service: Successfully reconnected to OIDC server!' )
255- // Stop health check once reconnected
256- this . stopHealthCheck ( )
294+ this . healthCheckAttempts = 0
295+ // Switch to continuous monitoring
296+ scheduleNextCheck ( )
257297 } catch ( error ) {
258298 this . healthCheckAttempts ++
259- // Schedule next check with longer interval
299+ // Schedule next reconnection attempt
260300 scheduleNextCheck ( )
261301 }
262- } , delay )
263- }
302+ }
303+ } , delay )
264304 }
265305
266306 // Start the first check
0 commit comments