@@ -527,24 +527,36 @@ def get_culling(
527527 user : AuthenticatedAPIUser | AnonymousAPIUser , resource_pool : ResourcePool , nb_config : NotebooksConfig
528528) -> Culling :
529529 """Create the culling specification for an AmaltheaSession."""
530- if user .is_anonymous :
531- # NOTE: Anonymous sessions should not be hibernated at all, but there is no such option in Amalthea
532- # So in this case we set a very low hibernation threshold so the session is deleted quickly after
533- # it is hibernated.
534- hibernation_threshold : timedelta | None = timedelta (seconds = 1 )
535- else :
536- hibernation_threshold = (
537- timedelta (seconds = resource_pool .hibernation_threshold )
538- if resource_pool .hibernation_threshold is not None
539- else None
540- )
530+ hibernation_threshold : timedelta | None = None
531+ # NOTE: A value of zero on the resource_pool hibernation threshold
532+ # is interpreted by Amalthea as "never automatically delete after hibernation"
533+ match (user .is_anonymous , resource_pool .hibernation_threshold ):
534+ case True , _:
535+ # NOTE: Anonymous sessions should not be hibernated at all, but there is no such option in Amalthea
536+ # So in this case we set a very low hibernation threshold so the session is deleted quickly after
537+ # it is hibernated.
538+ hibernation_threshold = timedelta (seconds = 1 )
539+ case False , int ():
540+ hibernation_threshold = timedelta (seconds = resource_pool .hibernation_threshold )
541+ case False , None :
542+ hibernation_threshold = timedelta (seconds = nb_config .sessions .culling .registered .hibernated_seconds )
543+
544+ idle_duration : timedelta | None = None
545+ # NOTE: A value of zero on the resource_pool idle threshold
546+ # is interpreted by Amalthea as "never automatically hibernate for idleness"
547+ match (user .is_anonymous , resource_pool .idle_threshold ):
548+ case True , None :
549+ idle_duration = timedelta (seconds = nb_config .sessions .culling .anonymous .idle_seconds )
550+ case _, int ():
551+ idle_duration = timedelta (seconds = resource_pool .idle_threshold )
552+ case False , None :
553+ idle_duration = timedelta (seconds = nb_config .sessions .culling .registered .idle_seconds )
554+
541555 return Culling (
542556 maxAge = timedelta (seconds = nb_config .sessions .culling .registered .max_age_seconds ),
543557 maxFailedDuration = timedelta (seconds = nb_config .sessions .culling .registered .failed_seconds ),
544558 maxHibernatedDuration = hibernation_threshold ,
545- maxIdleDuration = timedelta (seconds = resource_pool .idle_threshold )
546- if resource_pool .idle_threshold is not None
547- else None ,
559+ maxIdleDuration = idle_duration ,
548560 maxStartingDuration = timedelta (seconds = nb_config .sessions .culling .registered .pending_seconds ),
549561 )
550562
0 commit comments