@@ -136,6 +136,14 @@ export type ExtractActorConnState<A extends AnyActorInstance> =
136136 ? ConnState
137137 : never ;
138138
139+ enum CanSleep {
140+ Yes ,
141+ NotReady ,
142+ ActiveConns ,
143+ ActiveHonoHttpRequests ,
144+ ActiveRawWebSockets ,
145+ }
146+
139147export class ActorInstance < S , CP , CS , V , I , DB extends AnyDatabaseProvider > {
140148 // Shared actor context for this instance
141149 actorContext : ActorContext < S , CP , CS , V , I , DB > ;
@@ -1913,7 +1921,7 @@ export class ActorInstance<S, CP, CS, V, I, DB extends AnyDatabaseProvider> {
19131921
19141922 this . #rLog. debug ( {
19151923 msg : "resetting sleep timer" ,
1916- canSleep,
1924+ canSleep : CanSleep [ canSleep ] ,
19171925 existingTimeout : ! ! this . #sleepTimeout,
19181926 timeout : this . #config. options . sleepTimeout ,
19191927 } ) ;
@@ -1926,34 +1934,36 @@ export class ActorInstance<S, CP, CS, V, I, DB extends AnyDatabaseProvider> {
19261934 // Don't set a new timer if already sleeping
19271935 if ( this . #sleepCalled) return ;
19281936
1929- if ( canSleep ) {
1937+ if ( canSleep === CanSleep . Yes ) {
19301938 this . #sleepTimeout = setTimeout ( ( ) => {
19311939 this . _startSleep ( ) ;
19321940 } , this . #config. options . sleepTimeout ) ;
19331941 }
19341942 }
19351943
19361944 /** If this actor can be put in a sleeping state. */
1937- #canSleep( ) : boolean {
1938- if ( ! this . #ready) return false ;
1945+ #canSleep( ) : CanSleep {
1946+ if ( ! this . #ready) return CanSleep . NotReady ;
1947+
1948+ // Do not sleep if Hono HTTP requests are in-flight
1949+ if ( this . #activeHonoHttpRequests > 0 )
1950+ return CanSleep . ActiveHonoHttpRequests ;
1951+
1952+ // TODO: When WS hibernation is ready, update this to only count non-hibernatable websockets
1953+ // Do not sleep if there are raw websockets open
1954+ if ( this . #activeRawWebSockets. size > 0 )
1955+ return CanSleep . ActiveRawWebSockets ;
19391956
19401957 // Check for active conns. This will also cover active actions, since all actions have a connection.
19411958 for ( const conn of this . #connections. values ( ) ) {
19421959 // TODO: Enable this when hibernation is implemented. We're waiting on support for Guard to not auto-wake the actor if it sleeps.
19431960 // if (conn.status === "connected" && !conn.isHibernatable)
19441961 // return false;
19451962
1946- if ( conn . status === "connected" ) return false ;
1963+ if ( conn . status === "connected" ) return CanSleep . ActiveConns ;
19471964 }
19481965
1949- // Do not sleep if Hono HTTP requests are in-flight
1950- if ( this . #activeHonoHttpRequests > 0 ) return false ;
1951-
1952- // TODO: When WS hibernation is ready, update this to only count non-hibernatable websockets
1953- // Do not sleep if there are raw websockets open
1954- if ( this . #activeRawWebSockets. size > 0 ) return false ;
1955-
1956- return true ;
1966+ return CanSleep . Yes ;
19571967 }
19581968
19591969 /**
0 commit comments