You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -287,7 +291,7 @@ export class ActorInstance<S, CP, CS, V, I, DB extends AnyDatabaseProvider> {
287
291
}
288
292
289
293
get #sleepingSupported(): boolean{
290
-
returnthis.#actorDriver.sleep!==undefined;
294
+
returnthis.#actorDriver.startSleep!==undefined;
291
295
}
292
296
293
297
/**
@@ -1517,10 +1521,6 @@ export class ActorInstance<S, CP, CS, V, I, DB extends AnyDatabaseProvider> {
1517
1521
thrownewerrors.FetchHandlerNotDefined();
1518
1522
}
1519
1523
1520
-
// Track active raw fetch while handler runs
1521
-
this.#activeRawFetchCount++;
1522
-
this.#resetSleepTimer();
1523
-
1524
1524
try{
1525
1525
constresponse=awaitthis.#config.onFetch(
1526
1526
this.actorContext,
@@ -1538,12 +1538,6 @@ export class ActorInstance<S, CP, CS, V, I, DB extends AnyDatabaseProvider> {
1538
1538
});
1539
1539
throwerror;
1540
1540
}finally{
1541
-
// Decrement active raw fetch counter and re-evaluate sleep
1542
-
this.#activeRawFetchCount =Math.max(
1543
-
0,
1544
-
this.#activeRawFetchCount -1,
1545
-
);
1546
-
this.#resetSleepTimer();
1547
1541
this.#savePersistThrottled();
1548
1542
}
1549
1543
}
@@ -1880,6 +1874,29 @@ export class ActorInstance<S, CP, CS, V, I, DB extends AnyDatabaseProvider> {
1880
1874
}
1881
1875
}
1882
1876
1877
+
/**
1878
+
* Called by router middleware when an HTTP request begins.
1879
+
*/
1880
+
__beginHonoHttpRequest(){
1881
+
this.#activeHonoHttpRequests++;
1882
+
this.#resetSleepTimer();
1883
+
}
1884
+
1885
+
/**
1886
+
* Called by router middleware when an HTTP request ends.
1887
+
*/
1888
+
__endHonoHttpRequest(){
1889
+
this.#activeHonoHttpRequests--;
1890
+
if(this.#activeHonoHttpRequests <0){
1891
+
this.#activeHonoHttpRequests =0;
1892
+
this.#rLog.warn({
1893
+
msg: "active hono requests went below 0, this is a RivetKit bug",
1894
+
...EXTRA_ERROR_LOG,
1895
+
});
1896
+
}
1897
+
this.#resetSleepTimer();
1898
+
}
1899
+
1883
1900
// MARK: Sleep
1884
1901
/**
1885
1902
* Reset timer from the last actor interaction that allows it to be put to sleep.
@@ -1900,6 +1917,7 @@ export class ActorInstance<S, CP, CS, V, I, DB extends AnyDatabaseProvider> {
1900
1917
msg: "resetting sleep timer",
1901
1918
canSleep,
1902
1919
existingTimeout: !!this.#sleepTimeout,
1920
+
timeout: this.#config.options.sleepTimeout,
1903
1921
});
1904
1922
1905
1923
if(this.#sleepTimeout){
@@ -1912,12 +1930,7 @@ export class ActorInstance<S, CP, CS, V, I, DB extends AnyDatabaseProvider> {
1912
1930
1913
1931
if(canSleep){
1914
1932
this.#sleepTimeout =setTimeout(()=>{
1915
-
this._sleep().catch((error)=>{
1916
-
this.#rLog.error({
1917
-
msg: "error during sleep",
1918
-
error: stringifyError(error),
1919
-
});
1920
-
});
1933
+
this._sleep();
1921
1934
},this.#config.options.sleepTimeout);
1922
1935
}
1923
1936
}
@@ -1935,18 +1948,19 @@ export class ActorInstance<S, CP, CS, V, I, DB extends AnyDatabaseProvider> {
1935
1948
if(conn.status==="connected")returnfalse;
1936
1949
}
1937
1950
1938
-
// Do not sleep if raw fetches are in-flight
1939
-
if(this.#activeRawFetchCount>0)returnfalse;
1951
+
// Do not sleep if Hono HTTP requests are in-flight
1952
+
if(this.#activeHonoHttpRequests>0)returnfalse;
1940
1953
1954
+
// TODO: When WS hibernation is ready, update this to only count non-hibernatable websockets
1941
1955
// Do not sleep if there are raw websockets open
1942
1956
if(this.#activeRawWebSockets.size>0)returnfalse;
1943
1957
1944
1958
returntrue;
1945
1959
}
1946
1960
1947
1961
/** Puts an actor to sleep. This should just start the sleep sequence, most shutdown logic should be in _stop (which is called by the ActorDriver when sleeping). */
1948
-
async_sleep(){
1949
-
constsleep=this.#actorDriver.sleep?.bind(
1962
+
_sleep(){
1963
+
constsleep=this.#actorDriver.startSleep?.bind(
1950
1964
this.#actorDriver,
1951
1965
this.#actorId,
1952
1966
);
@@ -1962,11 +1976,11 @@ export class ActorInstance<S, CP, CS, V, I, DB extends AnyDatabaseProvider> {
1962
1976
this.#rLog.info({msg: "actor sleeping"});
1963
1977
1964
1978
// Schedule sleep to happen on the next tick. This allows for any action that calls _sleep to complete.
1965
-
setImmediate(async()=>{
1979
+
setImmediate(()=>{
1966
1980
// The actor driver should call stop when ready to stop
1967
1981
//
1968
1982
// This will call _stop once Pegboard responds with the new status
0 commit comments