Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Application-level auto-response for hibernatable web sockets #456

Merged
merged 1 commit into from
Jun 22, 2023

Conversation

jqmmes
Copy link
Contributor

@jqmmes jqmmes commented Mar 17, 2023

When a specific request is set for hiberntabale web socket, they must respond with response, store the timestamp and if currently hibernating, should stay hibernated.
This PR adds a new DurableObjectState::setWebSocketAutoresponse(request: string, response: string); and WebSocket::getAutoresponseTimestamp();.

@jqmmes jqmmes force-pushed the joaquim/ws-autoresponse branch 5 times, most recently from eadf6e3 to 74eba04 Compare March 17, 2023 16:44
@jqmmes jqmmes force-pushed the joaquim/ws-autoresponse branch 2 times, most recently from 60e4aef to 466ddd7 Compare April 28, 2023 10:38
@jqmmes jqmmes force-pushed the joaquim/ws-autoresponse branch 2 times, most recently from e310b8f to 5e0129e Compare May 10, 2023 13:17
src/workerd/api/actor-state.c++ Outdated Show resolved Hide resolved
src/workerd/api/actor-state.h Show resolved Hide resolved
src/workerd/io/hibernation-manager.c++ Outdated Show resolved Hide resolved
src/workerd/api/web-socket.h Outdated Show resolved Hide resolved
src/workerd/io/hibernation-manager.c++ Outdated Show resolved Hide resolved
@jqmmes jqmmes changed the title [WIP] Application-level auto-response for hibernatable web sockets Application-level auto-response for hibernatable web sockets May 10, 2023
@jqmmes jqmmes marked this pull request as ready for review May 10, 2023 16:33
JSG_RESOURCE_TYPE(DurableObjectState, CompatibilityFlags::Reader flags) {
JSG_METHOD(waitUntil);
JSG_READONLY_INSTANCE_PROPERTY(id, getId);
JSG_READONLY_INSTANCE_PROPERTY(storage, getStorage);
JSG_METHOD(blockConcurrencyWhile);
JSG_METHOD(acceptWebSocket);
JSG_METHOD(getWebSockets);
JSG_METHOD(setWebSocketAutoresponse);
JSG_METHOD(unsetWebSocketAutoresponse);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I suggest using a property getter/setter instead of a pair of methods?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the getter, do you think it should return a pair<request, response> or should there be two getters, one for request and one for response?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If they always go together, a pair. If they are independent, separate getters/setters

@jqmmes jqmmes force-pushed the joaquim/ws-autoresponse branch 2 times, most recently from f253264 to 5039bed Compare May 31, 2023 18:21
Copy link
Contributor

@MellowYarker MellowYarker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice cleanup! Glad to see it wasn't too much of a hassle to avoid extending the websocket API

src/workerd/api/actor-state.c++ Outdated Show resolved Hide resolved
src/workerd/api/actor-state.c++ Outdated Show resolved Hide resolved
src/workerd/api/actor-state.h Outdated Show resolved Hide resolved
src/workerd/api/actor-state.h Outdated Show resolved Hide resolved
src/workerd/api/actor-state.h Outdated Show resolved Hide resolved
src/workerd/io/hibernation-manager.c++ Outdated Show resolved Hide resolved
}

kj::Maybe<jsg::Ref<api::WebSocketRequestResponsePair>> HibernationManagerImpl::getWebSocketAutoResponse() {
KJ_IF_MAYBE(ar, autoResponseRequestResponsePair) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this might become a 1 liner if you return autoResponsePair.map(...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That doesn't seem to work in this case :(

src/workerd/io/hibernation-manager.c++ Outdated Show resolved Hide resolved
src/workerd/io/hibernation-manager.c++ Outdated Show resolved Hide resolved
src/workerd/io/hibernation-manager.c++ Outdated Show resolved Hide resolved
@jqmmes jqmmes force-pushed the joaquim/ws-autoresponse branch 2 times, most recently from be9989d to e610f11 Compare June 1, 2023 16:02
@jqmmes jqmmes force-pushed the joaquim/ws-autoresponse branch 2 times, most recently from ab280ae to 01fc6ff Compare June 1, 2023 19:12
@jqmmes jqmmes force-pushed the joaquim/ws-autoresponse branch 2 times, most recently from 9f8b8eb to 6687ec0 Compare June 2, 2023 09:09
Copy link
Contributor

@MellowYarker MellowYarker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Joaquim!

@@ -340,6 +340,28 @@ class ActorState: public jsg::Object {
kj::Maybe<jsg::Ref<DurableObjectStorage>> persistent;
};

class WebSocketRequestResponsePair: public jsg::Object {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think this class should have a brief comment describing what it's for:

// A (`RequestMessage`, `ResponseMessage`) pair that's set on a Durable Object for 
// Hibernatable WebSocket auto-response. The `RequestMessage` is automatically 
// replied to with `ResponseMessage` without forcing the Durable Object to wake from hibernation.

@jqmmes jqmmes force-pushed the joaquim/ws-autoresponse branch 3 times, most recently from 26b1cfb to f65bfa0 Compare June 7, 2023 18:44
@jqmmes
Copy link
Contributor Author

jqmmes commented Jun 7, 2023

Updated and rebased to pass windows tests

@jqmmes jqmmes force-pushed the joaquim/ws-autoresponse branch 2 times, most recently from af58280 to 2263ee8 Compare June 19, 2023 17:30
This commit add a new method for hibernatable web sockets that enables
a ping/pong application autoresponse, storing the last received pong
timestamp. There's also a method to access the last received timestamp.
@jqmmes jqmmes merged commit e1799c7 into main Jun 22, 2023
7 checks passed
@jqmmes jqmmes deleted the joaquim/ws-autoresponse branch June 27, 2023 16:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants