Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/calm-servers-hibernate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"partyserver": patch
---

Inherit hibernation settings when a Server subclass declares partial static options.
2 changes: 2 additions & 0 deletions packages/partyserver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ A connection is a standard WebSocket with the following additional properties:

You can enable [hibernation](https://developers.cloudflare.com/durable-objects/reference/websockets/#websocket-hibernation) by setting a static `options` property on your Server class. This allows the server to hibernate when not in use and wake up when a new connection is established. All lifecycle hooks will be called as expected when the server wakes up.

Subclasses inherit the nearest explicitly configured `hibernate` value. Declaring partial static options on a child class does not disable hibernation inherited from its parent.

```ts
export class MyServer extends Server {
static options = {
Expand Down
24 changes: 22 additions & 2 deletions packages/partyserver/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,24 @@ Did you forget to add a durable object binding to the class ${namespace[0].toUpp
}
}

type ServerOptions = { hibernate?: boolean };

function resolveServerOptions(serverClass: {
options?: ServerOptions;
}): Required<ServerOptions> {
let current: { options?: ServerOptions } | null = serverClass;
while (current) {
const hibernate = current.options?.hibernate;
if (hibernate !== undefined) {
return { hibernate };
}
current = Object.getPrototypeOf(current) as {
options?: ServerOptions;
} | null;
}
return { hibernate: false };
}

export class Server<
Env extends Cloudflare.Env = Cloudflare.Env,
Props extends Record<string, unknown> = Record<string, unknown>
Expand All @@ -598,7 +616,9 @@ export class Server<

#ParentClass: typeof Server = Object.getPrototypeOf(this).constructor;

#connectionManager: ConnectionManager = this.#ParentClass.options.hibernate
#options = resolveServerOptions(this.#ParentClass);

#connectionManager: ConnectionManager = this.#options.hibernate
? new HibernatingConnectionManager(this.ctx)
: new InMemoryConnectionManager();

Expand Down Expand Up @@ -709,7 +729,7 @@ export class Server<
// Accept the websocket connection
connection = this.#connectionManager.accept(connection, { tags });

if (!this.#ParentClass.options.hibernate) {
if (!this.#options.hibernate) {
this.#attachSocketEventHandlers(connection);
}
await this.onConnect(connection, ctx);
Expand Down
22 changes: 22 additions & 0 deletions packages/partyserver/src/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
createExecutionContext,
runInDurableObject,
runDurableObjectAlarm
// waitOnExecutionContext
} from "cloudflare:test";
Expand Down Expand Up @@ -68,6 +69,27 @@ describe("Server", () => {
return promise;
});

it("inherits hibernation when a child declares partial static options", async () => {
const name = crypto.randomUUID();
const response = await worker.fetch(
new Request(`http://example.com/parties/stateful/${name}`, {
headers: { Upgrade: "websocket" }
}),
env,
createExecutionContext()
);
const websocket = response.webSocket!;
websocket.accept();

const acceptedWebSockets = await runInDurableObject(
env.Stateful.getByName(name),
(_instance, ctx) => ctx.getWebSockets().length
);

expect(acceptedWebSockets).toBe(1);
websocket.close();
});

it("calls onStart only once, and does not process messages or requests until it is resolved", async () => {
const ctx = createExecutionContext();

Expand Down
8 changes: 6 additions & 2 deletions packages/partyserver/src/tests/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ export class BinaryTypeProbe extends Server {
}
}

export class Stateful extends Server {
static options = {
class HibernatingServer extends Server {
static options: typeof Server.options = {
hibernate: true
};
}

export class Stateful extends HibernatingServer {
static options = {};

onConnect(
connection: Connection,
Expand Down
Loading