@@ -16,11 +16,11 @@ const ENDPOINT_KEY_NAME = "Endpoint";
1616const ID_KEY_NAME = "Id" ;
1717const SECRET_KEY_NAME = "Secret" ;
1818const TRUSTED_DOMAIN_LABELS = [ ".azconfig." , ".appconfig." ] ;
19- const FALLBACK_CLIENT_REFRESH_EXPIRE_INTERVAL = 60 * 60 * 1000 ; // 1 hour in milliseconds
19+ const FALLBACK_CLIENT_EXPIRE_INTERVAL = 60 * 60 * 1000 ; // 1 hour in milliseconds
2020const MINIMAL_CLIENT_REFRESH_INTERVAL = 30 * 1000 ; // 30 seconds in milliseconds
21- const DNS_RESOLVER_TIMEOUT = 1_000 ; // 1 second in milliseconds
21+ const DNS_RESOLVER_TIMEOUT = 1_000 ; // 1 second in milliseconds, in most cases, dns resolution should be within 200 milliseconds
2222const DNS_RESOLVER_TRIES = 2 ;
23- const MAX_RESOLVESRV_TIMES = 15 ;
23+ const MAX_ALTNATIVE_SRV_COUNT = 10 ;
2424
2525export class ConfigurationClientManager {
2626 #isFailoverable: boolean ;
@@ -35,8 +35,8 @@ export class ConfigurationClientManager {
3535 #staticClients: ConfigurationClientWrapper [ ] ; // there should always be only one static client
3636 #dynamicClients: ConfigurationClientWrapper [ ] ;
3737 #replicaCount: number = 0 ;
38- #lastFallbackClientRefreshTime : number = 0 ;
39- #lastFallbackClientRefreshAttempt: number = 0 ;
38+ #lastFallbackClientUpdateTime : number = 0 ; // enforce to discover fallback client when it is expired
39+ #lastFallbackClientRefreshAttempt: number = 0 ; // avoid refreshing clients before the minimal refresh interval
4040
4141 constructor (
4242 connectionStringOrEndpoint ?: string | URL ,
@@ -119,8 +119,7 @@ export class ConfigurationClientManager {
119119 ( ! this . #dynamicClients ||
120120 // All dynamic clients are in backoff means no client is available
121121 this . #dynamicClients. every ( client => currentTime < client . backoffEndTime ) ||
122- currentTime >= this . #lastFallbackClientRefreshTime + FALLBACK_CLIENT_REFRESH_EXPIRE_INTERVAL ) ) {
123- this . #lastFallbackClientRefreshAttempt = currentTime ;
122+ currentTime >= this . #lastFallbackClientUpdateTime + FALLBACK_CLIENT_EXPIRE_INTERVAL ) ) {
124123 await this . #discoverFallbackClients( this . endpoint . hostname ) ;
125124 return availableClients . concat ( this . #dynamicClients) ;
126125 }
@@ -138,19 +137,18 @@ export class ConfigurationClientManager {
138137 async refreshClients ( ) {
139138 const currentTime = Date . now ( ) ;
140139 if ( this . #isFailoverable &&
141- currentTime >= new Date ( this . #lastFallbackClientRefreshAttempt + MINIMAL_CLIENT_REFRESH_INTERVAL ) . getTime ( ) ) {
142- this . #lastFallbackClientRefreshAttempt = currentTime ;
140+ currentTime >= this . #lastFallbackClientRefreshAttempt + MINIMAL_CLIENT_REFRESH_INTERVAL ) {
143141 await this . #discoverFallbackClients( this . endpoint . hostname ) ;
144142 }
145143 }
146144
147145 async #discoverFallbackClients( host : string ) {
146+ this . #lastFallbackClientRefreshAttempt = Date . now ( ) ;
148147 let result : string [ ] ;
149148 try {
150149 result = await this . #querySrvTargetHost( host ) ;
151150 } catch ( error ) {
152151 console . warn ( `Failed to build fallback clients, ${ error . message } ` ) ;
153- this . #lastFallbackClientRefreshTime = Date . now ( ) ;
154152 return ; // swallow the error when srv query fails
155153 }
156154
@@ -163,14 +161,14 @@ export class ConfigurationClientManager {
163161 continue ;
164162 }
165163 const client = this . #credential ?
166- new AppConfigurationClient ( targetEndpoint , this . #credential, this . #clientOptions) :
167- new AppConfigurationClient ( buildConnectionString ( targetEndpoint , this . #secret, this . #id) , this . #clientOptions) ;
164+ new AppConfigurationClient ( targetEndpoint , this . #credential, this . #clientOptions) :
165+ new AppConfigurationClient ( buildConnectionString ( targetEndpoint , this . #secret, this . #id) , this . #clientOptions) ;
168166 newDynamicClients . push ( new ConfigurationClientWrapper ( targetEndpoint , client ) ) ;
169167 }
170168 }
171169
172170 this . #dynamicClients = newDynamicClients ;
173- this . #lastFallbackClientRefreshTime = Date . now ( ) ;
171+ this . #lastFallbackClientUpdateTime = Date . now ( ) ;
174172 this . #replicaCount = this . #dynamicClients. length ;
175173 }
176174
@@ -194,7 +192,7 @@ export class ConfigurationClientManager {
194192
195193 // Look up SRV records for alternate hosts
196194 let index = 0 ;
197- while ( index < MAX_RESOLVESRV_TIMES ) {
195+ while ( index < MAX_ALTNATIVE_SRV_COUNT ) {
198196 const currentAlt = `${ ALT_KEY_NAME } ${ index } ` ;
199197 const altRecords = await resolver . resolveSrv ( `${ currentAlt } .${ TCP_KEY_NAME } .${ originHost } ` ) ;
200198 if ( altRecords . length === 0 ) {
0 commit comments