11import { net } from "electron" ;
22import { injectable , postConstruct } from "inversify" ;
3+ import { getBackoffDelay } from "../../../shared/utils/backoff.js" ;
34import { logger } from "../../lib/logger.js" ;
45import { TypedEventEmitter } from "../../lib/typed-event-emitter.js" ;
56import {
@@ -19,7 +20,7 @@ const ONLINE_POLL_INTERVAL_MS = 3_000;
1920export class ConnectivityService extends TypedEventEmitter < ConnectivityEvents > {
2021 private isOnline = false ;
2122 private pollTimeoutId : ReturnType < typeof setTimeout > | null = null ;
22- private currentPollInterval = MIN_POLL_INTERVAL_MS ;
23+ private offlinePollAttempt = 0 ;
2324
2425 @postConstruct ( )
2526 init ( ) : void {
@@ -45,7 +46,7 @@ export class ConnectivityService extends TypedEventEmitter<ConnectivityEvents> {
4546 log . info ( "Connectivity status changed" , { isOnline : online } ) ;
4647 this . emit ( ConnectivityEvent . StatusChange , { isOnline : online } ) ;
4748
48- this . currentPollInterval = MIN_POLL_INTERVAL_MS ;
49+ this . offlinePollAttempt = 0 ;
4950 }
5051
5152 private async checkConnectivity ( ) : Promise < void > {
@@ -73,7 +74,7 @@ export class ConnectivityService extends TypedEventEmitter<ConnectivityEvents> {
7374 private startPolling ( ) : void {
7475 if ( this . pollTimeoutId ) return ;
7576
76- this . currentPollInterval = MIN_POLL_INTERVAL_MS ;
77+ this . offlinePollAttempt = 0 ;
7778 this . schedulePoll ( ) ;
7879 }
7980
@@ -82,7 +83,11 @@ export class ConnectivityService extends TypedEventEmitter<ConnectivityEvents> {
8283 // when offline: poll more frequently with backoff to detect recovery
8384 const interval = this . isOnline
8485 ? ONLINE_POLL_INTERVAL_MS
85- : this . currentPollInterval ;
86+ : getBackoffDelay ( this . offlinePollAttempt , {
87+ initialDelayMs : MIN_POLL_INTERVAL_MS ,
88+ maxDelayMs : MAX_POLL_INTERVAL_MS ,
89+ multiplier : 1.5 ,
90+ } ) ;
8691
8792 this . pollTimeoutId = setTimeout ( async ( ) => {
8893 this . pollTimeoutId = null ;
@@ -91,10 +96,7 @@ export class ConnectivityService extends TypedEventEmitter<ConnectivityEvents> {
9196 await this . checkConnectivity ( ) ;
9297
9398 if ( ! this . isOnline && wasOffline ) {
94- this . currentPollInterval = Math . min (
95- this . currentPollInterval * 1.5 ,
96- MAX_POLL_INTERVAL_MS ,
97- ) ;
99+ this . offlinePollAttempt ++ ;
98100 }
99101
100102 this . schedulePoll ( ) ;
0 commit comments