@@ -171,12 +171,11 @@ def apply_headers(self, headers):
171171 else :
172172 headers .pop (_REGIONAL_ACCESS_BOUNDARY_HEADER , None )
173173
174- def maybe_start_refresh (self , credentials , request ):
175- """Starts a background thread to refresh the Regional Access Boundary if needed .
174+ def _should_refresh (self ):
175+ """Checks if the Regional Access Boundary data needs a refresh and is not in cooldown .
176176
177- Args:
178- credentials (google.auth.credentials.Credentials): The credentials to refresh.
179- request (google.auth.transport.Request): The object used to make HTTP requests.
177+ Returns:
178+ bool: True if a refresh is required, False otherwise.
180179 """
181180 rab_data = self ._data
182181
@@ -187,10 +186,22 @@ def maybe_start_refresh(self, credentials, request):
187186 and _helpers .utcnow ()
188187 < (rab_data .expiry - REGIONAL_ACCESS_BOUNDARY_REFRESH_THRESHOLD )
189188 ):
190- return
189+ return False
191190
192191 # Don't start a new refresh if the cooldown is still in effect.
193192 if rab_data .cooldown_expiry and _helpers .utcnow () < rab_data .cooldown_expiry :
193+ return False
194+
195+ return True
196+
197+ def maybe_start_refresh (self , credentials , request ):
198+ """Starts a background thread to refresh the Regional Access Boundary if needed.
199+
200+ Args:
201+ credentials (google.auth.credentials.Credentials): The credentials to refresh.
202+ request (google.auth.transport.Request): The object used to make HTTP requests.
203+ """
204+ if not self ._should_refresh ():
194205 return
195206
196207 # If all checks pass, start the background refresh.
@@ -199,6 +210,22 @@ def maybe_start_refresh(self, credentials, request):
199210 else :
200211 self .refresh_manager .start_refresh (credentials , request , self )
201212
213+ async def maybe_start_refresh_async (self , credentials , request ):
214+ """Starts a background refresh or performs a blocking refresh asynchronously.
215+
216+ Args:
217+ credentials (google.auth.credentials.Credentials): The credentials to refresh.
218+ request (google.auth.aio.transport.Request): The object used to make HTTP requests.
219+ """
220+ if not self ._should_refresh ():
221+ return
222+
223+ # If all checks pass, start the refresh.
224+ if self ._use_blocking_regional_access_boundary_lookup :
225+ await self .start_blocking_refresh_async (credentials , request )
226+ else :
227+ self .refresh_manager .start_refresh (credentials , request , self )
228+
202229 def start_blocking_refresh (self , credentials , request ):
203230 """Initiates a blocking lookup of the Regional Access Boundary.
204231
@@ -228,6 +255,37 @@ def start_blocking_refresh(self, credentials, request):
228255
229256 self .process_regional_access_boundary_info (regional_access_boundary_info )
230257
258+ async def start_blocking_refresh_async (self , credentials , request ):
259+ """Initiates a blocking lookup of the Regional Access Boundary asynchronously.
260+
261+ If the lookup raises an exception, it is caught and logged as a warning,
262+ and the lookup is treated as a failure (entering cooldown). Exceptions
263+ are not propagated to the caller.
264+
265+ Args:
266+ credentials (google.auth.credentials.Credentials): The credentials to refresh.
267+ request (google.auth.aio.transport.Request): The object used to make HTTP requests.
268+ """
269+ try :
270+ # The fail_fast parameter is set to True to ensure we don't block the calling
271+ # thread for too long. This will do two things: 1) set a timeout to 3s
272+ # instead of the default 120s and 2) ensure we do not retry at all
273+ regional_access_boundary_info = (
274+ await credentials ._lookup_regional_access_boundary (
275+ request , fail_fast = True
276+ )
277+ )
278+ except Exception as e :
279+ if _helpers .is_logging_enabled (_LOGGER ):
280+ _LOGGER .warning (
281+ "Regional Access Boundary lookup raised an exception: %s" ,
282+ e ,
283+ exc_info = True ,
284+ )
285+ regional_access_boundary_info = None
286+
287+ self .process_regional_access_boundary_info (regional_access_boundary_info )
288+
231289 def process_regional_access_boundary_info (self , regional_access_boundary_info ):
232290 """Processes the regional access boundary info and updates the state.
233291
0 commit comments