@@ -105,16 +105,12 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
105105 #watchAll: boolean = false ;
106106 #kvRefreshInterval: number = DEFAULT_REFRESH_INTERVAL_IN_MS ;
107107 #kvRefreshTimer: RefreshTimer ;
108- #lastKvChangeDetectedTime: Date = new Date ( 0 ) ;
109- #isKvStale: boolean = false ;
110108
111109 // Feature flags
112110 #featureFlagEnabled: boolean = false ;
113111 #featureFlagRefreshEnabled: boolean = false ;
114112 #ffRefreshInterval: number = DEFAULT_REFRESH_INTERVAL_IN_MS ;
115113 #ffRefreshTimer: RefreshTimer ;
116- #lastFfChangeDetectedTime: Date = new Date ( 0 ) ;
117- #isFfStale: boolean = false ;
118114
119115 // Key Vault references
120116 #secretRefreshEnabled: boolean = false ;
@@ -173,7 +169,7 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
173169 if ( setting . label ?. includes ( "*" ) || setting . label ?. includes ( "," ) ) {
174170 throw new ArgumentError ( ErrorMessages . INVALID_WATCHED_SETTINGS_LABEL ) ;
175171 }
176- this . #sentinels. set ( setting , { etag : undefined , timestamp : new Date ( 0 ) } ) ;
172+ this . #sentinels. set ( setting , { etag : undefined , lastServerResponseTime : new Date ( 0 ) } ) ;
177173 }
178174 }
179175
@@ -522,19 +518,6 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
522518 } ;
523519 const { items, pageWatchers } = await this . #listConfigurationSettings( listOptions ) ;
524520
525- for ( const pageWatcher of pageWatchers ) {
526- if ( loadFeatureFlag ) {
527- this . #isFfStale = pageWatcher . timestamp < this . #lastFfChangeDetectedTime;
528- if ( this . #isFfStale) {
529- return [ ] ;
530- }
531- } else {
532- this . #isKvStale = pageWatcher . timestamp < this . #lastKvChangeDetectedTime;
533- if ( this . #isKvStale) {
534- return [ ] ;
535- }
536- }
537- }
538521 selector . pageWatchers = pageWatchers ;
539522 settings = items ;
540523 } else { // snapshot selector
@@ -611,7 +594,7 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
611594 const response = await this . #getConfigurationSetting( configurationSettingId , { onlyIfChanged : false } ) ;
612595 this . #sentinels. set ( watchedSetting , {
613596 etag : isRestError ( response ) ? undefined : response . etag ,
614- timestamp : this . #getResponseTimestamp( response )
597+ lastServerResponseTime : this . #getResponseTimestamp( response )
615598 } ) ;
616599 }
617600 }
@@ -666,38 +649,26 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
666649 let needRefresh = false ;
667650 let changedSentinel ;
668651 let changedSentinelWatcher ;
669- if ( this . #isKvStale) {
670- needRefresh = true ;
671- // skip checking changes
652+ if ( this . #watchAll) {
653+ needRefresh = await this . #checkConfigurationSettingsChange( this . #kvSelectors) ;
672654 } else {
673- if ( this . #watchAll) {
674- needRefresh = await this . #checkConfigurationSettingsChange( this . #kvSelectors) ;
675- } else {
676- const getOptions : GetConfigurationSettingOptions = {
677- // send conditional request only when CDN is not used
678- onlyIfChanged : ! this . #isCdnUsed
679- } ;
680- for ( const watchedSetting of this . #sentinels. keys ( ) ) {
681- const configurationSettingId : ConfigurationSettingId = { key : watchedSetting . key , label : watchedSetting . label } ;
682- const response : GetConfigurationSettingResponse | RestError =
683- await this . #getConfigurationSetting( configurationSettingId , getOptions ) ;
684-
685- const timestamp = this . #getResponseTimestamp( response ) ;
686- const watcher = this . #sentinels. get ( watchedSetting ) ;
687- const isUpToDate = watcher ?. timestamp ? timestamp > watcher . timestamp : true ;
688- const isDeleted = isRestError ( response ) && watcher ?. etag !== undefined ; // previously existed, now deleted
689- const isChanged =
690- ! isRestError ( response ) &&
691- response . statusCode === 200 &&
692- watcher ?. etag !== response . etag ; // etag changed
693-
694- if ( isUpToDate && ( isDeleted || isChanged ) ) {
695- this . #lastKvChangeDetectedTime = timestamp ;
696- changedSentinel = watchedSetting ;
697- changedSentinelWatcher = watcher ;
698- needRefresh = true ;
699- break ;
700- }
655+ for ( const watchedSetting of this . #sentinels. keys ( ) ) {
656+ const configurationSettingId : ConfigurationSettingId = { key : watchedSetting . key , label : watchedSetting . label } ;
657+ const response : GetConfigurationSettingResponse | RestError =
658+ await this . #getConfigurationSetting( configurationSettingId , { onlyIfChanged : true } ) ;
659+
660+ const watcher : SettingWatcher = this . #sentinels. get ( watchedSetting ) ! ; // watcher should always exist for sentinels
661+ const isDeleted = isRestError ( response ) && watcher . etag !== undefined ; // previously existed, now deleted
662+ const isChanged =
663+ ! isRestError ( response ) &&
664+ response . statusCode === 200 &&
665+ watcher . etag !== response . etag ; // etag changed
666+
667+ if ( isDeleted || isChanged ) {
668+ changedSentinel = watchedSetting ;
669+ changedSentinelWatcher = { etag : isChanged ? response . etag : undefined } ;
670+ needRefresh = true ;
671+ break ;
701672 }
702673 }
703674 }
@@ -707,11 +678,15 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
707678 await adapter . onChangeDetected ( ) ;
708679 }
709680 await this . #loadSelectedKeyValues( ) ;
710- this . #sentinels. set ( changedSentinel , changedSentinelWatcher ) ; // update the changed sentinel's watcher
681+
682+ if ( changedSentinel && changedSentinelWatcher ) {
683+ // update the changed sentinel's watcher after loading new values, this can ensure a failed refresh will retry on next refresh
684+ this . #sentinels. set ( changedSentinel , changedSentinelWatcher ) ;
685+ }
711686 }
712687
713688 this . #kvRefreshTimer. reset ( ) ;
714- return Promise . resolve ( needRefresh && ! this . #isKvStale ) ;
689+ return Promise . resolve ( needRefresh ) ;
715690 }
716691
717692 /**
@@ -725,20 +700,14 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
725700 }
726701
727702 let needRefresh = false ;
728- if ( this . #isFfStale) {
729- needRefresh = true ;
730- // skip checking changes
731- } else {
732- const refreshFeatureFlag = true ;
733- needRefresh = await this . #checkConfigurationSettingsChange( this . #ffSelectors, refreshFeatureFlag ) ;
734- }
703+ needRefresh = await this . #checkConfigurationSettingsChange( this . #ffSelectors) ;
735704
736705 if ( needRefresh ) {
737706 await this . #loadFeatureFlags( ) ;
738707 }
739708
740709 this . #ffRefreshTimer. reset ( ) ;
741- return Promise . resolve ( needRefresh && ! this . #isFfStale ) ;
710+ return Promise . resolve ( needRefresh ) ;
742711 }
743712
744713 async #refreshSecrets( ) : Promise < boolean > {
@@ -765,7 +734,7 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
765734 * @param selectors - The @see PagedSettingSelector of the kev-value collection.
766735 * @returns true if key-value collection has changed, false otherwise.
767736 */
768- async #checkConfigurationSettingsChange( selectors : PagedSettingsWatcher [ ] , refreshFeatureFlag : boolean = false ) : Promise < boolean > {
737+ async #checkConfigurationSettingsChange( selectors : PagedSettingsWatcher [ ] ) : Promise < boolean > {
769738 const funcToExecute = async ( client ) => {
770739 for ( const selector of selectors ) {
771740 if ( selector . snapshotName ) { // skip snapshot selector
@@ -794,14 +763,9 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
794763 const timestamp = this . #getResponseTimestamp( page ) ;
795764 // when conditional request is sent, the response will be 304 if not changed
796765 if ( i >= pageWatchers . length || // new page
797- ( timestamp > pageWatchers [ i ] . timestamp && // up to date
766+ ( timestamp > pageWatchers [ i ] . lastServerResponseTime && // up to date
798767 page . _response . status === 200 && // page changed
799768 page . etag !== pageWatchers [ i ] . etag ) ) {
800- if ( refreshFeatureFlag ) {
801- this . #lastFfChangeDetectedTime = timestamp ;
802- } else {
803- this . #lastKvChangeDetectedTime = timestamp ;
804- }
805769 return true ;
806770 }
807771 i ++ ;
@@ -852,7 +816,7 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
852816
853817 const items : ConfigurationSetting [ ] = [ ] ;
854818 for await ( const page of pageIterator ) {
855- pageWatchers . push ( { etag : page . etag , timestamp : this . #getResponseTimestamp( page ) } ) ;
819+ pageWatchers . push ( { etag : page . etag , lastServerResponseTime : this . #getResponseTimestamp( page ) } ) ;
856820 items . push ( ...page . items ) ;
857821 }
858822 return { items, pageWatchers } ;
0 commit comments