Skip to content

Commit

Permalink
fixup! Implement and use HibernationManagerImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
MellowYarker committed May 3, 2023
1 parent 610eda0 commit 1d952cb
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 44 deletions.
33 changes: 1 addition & 32 deletions src/workerd/io/hibernation-manager.c++
Original file line number Diff line number Diff line change
Expand Up @@ -95,49 +95,18 @@ kj::Vector<jsg::Ref<api::WebSocket>> HibernationManagerImpl::getWebSockets(
return kj::mv(matches);
}

kj::Array<kj::byte> HibernationManagerImpl::serializeV8Value(
v8::Local<v8::Value> value,
v8::Isolate* isolate) {
jsg::Serializer serializer(isolate, jsg::Serializer::Options {
.version = 15,
.omitHeader = false,
});
serializer.write(value);
auto released = serializer.release();
JSG_REQUIRE(released.data.size() <= MAX_ATTACHMENT_SIZE, Error,
"A WebSocket 'attachment' cannot be larger than ", MAX_ATTACHMENT_SIZE, " bytes");
return kj::mv(released.data);
}

v8::Local<v8::Value> HibernationManagerImpl::deserializeV8Value(
kj::Array<kj::byte> buf,
v8::Isolate* isolate) {
if (buf.size() == 0) {
return v8::Local<v8::Value>();
}
jsg::Deserializer deserializer(isolate, buf.asPtr(), nullptr, nullptr, jsg::Deserializer::Options {
.version = 15,
.readHeader = true,
});

return deserializer.readValue();
}

void HibernationManagerImpl::hibernateWebSockets(Worker::Lock& lock) {
jsg::Lock& js(lock);
v8::HandleScope handleScope(js.v8Isolate);
v8::Context::Scope contextScope(lock.getContext());
for (auto& ws : allWs) {
KJ_IF_MAYBE(active, ws->activeWebSocket) {
// We need to serialize the attachment before hibernating.
KJ_IF_MAYBE(attachment, active->get()->getAttachment(js)) {
ws->attachment = serializeV8Value(*attachment, js.v8Isolate);
}
// Note that we move these properties from api::WebSocket to the HibernatableWebSocket.
auto p = active->get()->buildPackageForHibernation();
ws->url = kj::mv(p.url);
ws->protocol = kj::mv(p.protocol);
ws->extensions = kj::mv(p.extensions);
ws->attachment = kj::mv(p.serializedAttachment);
}
ws->activeWebSocket = nullptr;
}
Expand Down
15 changes: 3 additions & 12 deletions src/workerd/io/hibernation-manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,11 @@ class HibernationManagerImpl final : public Worker::Actor::HibernationManager {
KJ_IF_MAYBE(active, activeWebSocket) {
return active->addRef();
}
auto deserializedAttachment = manager.deserializeV8Value(kj::mv(attachment), js.v8Isolate);
auto a = jsg::Value(js.v8Isolate, deserializedAttachment);
auto active = api::WebSocket::hibernatableFromNative(js, *ws, kj::mv(a),
auto active = api::WebSocket::hibernatableFromNative(js, *ws,
kj::mv(url),
kj::mv(protocol),
kj::mv(extensions));
kj::mv(extensions),
kj::mv(attachment));
activeWebSocket = active.addRef();
return kj::mv(active);
}
Expand Down Expand Up @@ -163,12 +162,6 @@ class HibernationManagerImpl final : public Worker::Actor::HibernationManager {
kj::Promise<void> readLoop(HibernatableWebSocket& hib);
// Like the api::WebSocket readLoop(), but we dispatch different types of events.

kj::Array<kj::byte> serializeV8Value(v8::Local<v8::Value> value, v8::Isolate* isolate);
// We need to serialize the `attachment` for each websocket before hibernating.

v8::Local<v8::Value> deserializeV8Value(kj::Array<kj::byte> buf, v8::Isolate* isolate);
// We need to deserialize the `attachment` when a websocket is unhibernated.

struct TagCollection {
// This struct is held by the `tagToWs` hashmap. The key is a StringPtr to tag, and the value
// is this struct itself.
Expand Down Expand Up @@ -201,8 +194,6 @@ class HibernationManagerImpl final : public Worker::Actor::HibernationManager {
const size_t ACTIVE_CONNECTION_LIMIT = 1024 * 32;
// The maximum number of Hibernatable WebSocket connections a single HibernationManagerImpl
// instance can manage.
const size_t MAX_ATTACHMENT_SIZE = 1024 * 2;
// Maximum size of a WebSocket attachment.

class DisconnectHandler: public kj::TaskSet::ErrorHandler {
public:
Expand Down

0 comments on commit 1d952cb

Please sign in to comment.