@@ -426,55 +426,46 @@ def list_projects(
426
426
427
427
def refresh (self , project : Optional [str ] = None ):
428
428
try :
429
- self .cached_registry_proto = self .proto ()
429
+ self .cached_registry_proto = self .proto (force_refresh = True )
430
430
self .cached_registry_proto_created = _utc_now ()
431
431
except Exception as e :
432
432
logger .debug (f"Error while refreshing registry: { e } " , exc_info = True )
433
433
434
434
def _refresh_cached_registry_if_necessary (self ):
435
435
if self .cache_mode == "sync" :
436
-
437
- def is_cache_expired ():
436
+ # Try acquiring the lock without blocking
437
+ if not self ._refresh_lock .acquire (blocking = False ):
438
+ logger .debug (
439
+ "Skipping refresh if lock is already held by another thread"
440
+ )
441
+ return
442
+ try :
438
443
if self .cached_registry_proto == RegistryProto ():
439
- if self .cached_registry_proto_ttl .total_seconds () == 0 :
440
- return False
441
- else :
442
- return True
443
-
444
- # Cache is expired if it's None or creation time is None
445
- if (
446
- self .cached_registry_proto is None
447
- or not hasattr (self , "cached_registry_proto_created" )
448
- or self .cached_registry_proto_created is None
449
- ):
450
- return True
451
-
452
- # Cache is expired if TTL > 0 and current time exceeds creation + TTL
453
- if self .cached_registry_proto_ttl .total_seconds () > 0 and _utc_now () > (
454
- self .cached_registry_proto_created + self .cached_registry_proto_ttl
455
- ):
456
- return True
457
-
458
- return False
459
-
460
- if is_cache_expired ():
461
- if not self ._refresh_lock .acquire (blocking = False ):
462
- logger .debug (
463
- "Skipping refresh if lock is already held by another thread"
464
- )
465
- return
466
- try :
467
- logger .info (
468
- f"Registry cache expired(ttl: { self .cached_registry_proto_ttl .total_seconds ()} seconds), so refreshing"
444
+ expired = False
445
+ else :
446
+ expired = (
447
+ self .cached_registry_proto is None
448
+ or self .cached_registry_proto_created is None
449
+ ) or (
450
+ self .cached_registry_proto_ttl .total_seconds () > 0
451
+ and (
452
+ _utc_now ()
453
+ > (
454
+ self .cached_registry_proto_created
455
+ + self .cached_registry_proto_ttl
456
+ )
457
+ )
469
458
)
459
+ if expired :
460
+ logger .debug ("Registry cache expired, so refreshing" )
470
461
self .refresh ()
471
- except Exception as e :
472
- logger .debug (
473
- f"Error in _refresh_cached_registry_if_necessary: { e } " ,
474
- exc_info = True ,
475
- )
476
- finally :
477
- self ._refresh_lock .release ()
462
+ except Exception as e :
463
+ logger .debug (
464
+ f"Error in _refresh_cached_registry_if_necessary: { e } " ,
465
+ exc_info = True ,
466
+ )
467
+ finally :
468
+ self ._refresh_lock .release () # Always release the lock safely
478
469
479
470
def _start_thread_async_refresh (self , cache_ttl_seconds ):
480
471
self .refresh ()
0 commit comments