Skip to content

Commit df2bb8e

Browse files
committed
Allow null name when creating dynamic workers
1 parent a14eccb commit df2bb8e

File tree

9 files changed

+31
-16
lines changed

9 files changed

+31
-16
lines changed

src/workerd/api/worker-loader-test.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,21 @@ export let isolateUniqueness = {
510510
assert.strictEqual(await anonymousAgain.increment(), 2);
511511
assert.strictEqual(await anonymousAgain.increment(), 3);
512512

513-
assert.strictEqual(loadCount, 5);
513+
// Test that null/undefined name parameter creates unique isolates each time
514+
// Each call should generate a unique name internally.
515+
let noName1 = env.loader.get(null, loadCodeCallback).getEntrypoint();
516+
let noName2 = env.loader.get(undefined, loadCodeCallback).getEntrypoint();
517+
let noName3 = env.loader.get(undefined, loadCodeCallback).getEntrypoint();
518+
519+
// Each should be a different isolate
520+
assert.strictEqual(await noName1.increment(), 0);
521+
assert.strictEqual(await noName1.increment(), 1);
522+
assert.strictEqual(await noName2.increment(), 0);
523+
assert.strictEqual(await noName2.increment(), 1);
524+
assert.strictEqual(await noName3.increment(), 0);
525+
assert.strictEqual(await noName3.increment(), 1);
526+
527+
assert.strictEqual(loadCount, 8);
514528
},
515529
};
516530

src/workerd/api/worker-loader.c++

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ jsg::Ref<DurableObjectClass> WorkerStub::getDurableObjectClass(jsg::Lock& js,
5959
}
6060

6161
jsg::Ref<WorkerStub> WorkerLoader::get(
62-
jsg::Lock& js, kj::String name, jsg::Function<jsg::Promise<WorkerCode>()> getCode) {
62+
jsg::Lock& js, kj::Maybe<kj::String> name, jsg::Function<jsg::Promise<WorkerCode>()> getCode) {
6363
auto& ioctx = IoContext::current();
6464

6565
auto reenterAndGetCode = ioctx.makeReentryCallback(

src/workerd/api/worker-loader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class WorkerLoader: public jsg::Object {
125125
};
126126

127127
jsg::Ref<WorkerStub> get(
128-
jsg::Lock& js, kj::String name, jsg::Function<jsg::Promise<WorkerCode>()> getCode);
128+
jsg::Lock& js, kj::Maybe<kj::String> name, jsg::Function<jsg::Promise<WorkerCode>()> getCode);
129129

130130
JSG_RESOURCE_TYPE(WorkerLoader) {
131131
JSG_METHOD(get);

src/workerd/io/io-channels.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,11 @@ class IoChannelFactory {
245245
KJ_UNIMPLEMENTED("Only implemented by single-tenant workerd runtime");
246246
}
247247

248-
// Use a dynamic Worker loader binding to obtain an Worker by name. If the named Worker
249-
// doesn't already exist, the callback will be called to fetch the source code from which the
250-
// Worker should be created.
248+
// Use a dynamic Worker loader binding to obtain an Worker by name. If name is undefined, or if
249+
// the named Worker doesn't already exist, the callback will be called to fetch the source code
250+
// from which the Worker should be created.
251251
virtual kj::Own<WorkerStubChannel> loadIsolate(uint loaderChannel,
252-
kj::String name,
252+
kj::Maybe<kj::String> name,
253253
kj::Function<kj::Promise<DynamicWorkerSource>()> fetchSource) {
254254
JSG_FAIL_REQUIRE(Error, "Dynamic worker loading is not supported by this runtime.");
255255
}

src/workerd/server/server.c++

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3321,7 +3321,7 @@ class Server::WorkerService final: public Service,
33213321
}
33223322

33233323
kj::Own<WorkerStubChannel> loadIsolate(uint loaderChannel,
3324-
kj::String name,
3324+
kj::Maybe<kj::String> name,
33253325
kj::Function<kj::Promise<DynamicWorkerSource>()> fetchSource) override;
33263326

33273327
// ---------------------------------------------------------------------------
@@ -3865,15 +3865,16 @@ class Server::WorkerLoaderNamespace: public kj::Refcounted {
38653865
}
38663866

38673867
kj::Own<WorkerStubChannel> loadIsolate(
3868-
kj::String name, kj::Function<kj::Promise<DynamicWorkerSource>()> fetchSource) {
3868+
kj::Maybe<kj::String> name, kj::Function<kj::Promise<DynamicWorkerSource>()> fetchSource) {
3869+
auto actualName = kj::mv(name).orDefault([] { return randomUUID(kj::none); });
38693870
return isolates
3870-
.findOrCreate(name,
3871+
.findOrCreate(actualName,
38713872
[&]() -> decltype(isolates)::Entry {
38723873
// This name isn't actually used in any maps nor is it ever revealed back to the app, but it
38733874
// may be used in error logs.
38743875
auto isolateName = kj::str(namespaceName, ':', name);
38753876

3876-
return {.key = kj::mv(name),
3877+
return {.key = kj::mv(actualName),
38773878
.value = kj::rc<WorkerStubImpl>(server, kj::mv(isolateName), kj::mv(fetchSource))};
38783879
})
38793880
.addRef()
@@ -4147,7 +4148,7 @@ void Server::unlinkWorkerLoaders() {
41474148
}
41484149

41494150
kj::Own<WorkerStubChannel> Server::WorkerService::loadIsolate(uint loaderChannel,
4150-
kj::String name,
4151+
kj::Maybe<kj::String> name,
41514152
kj::Function<kj::Promise<DynamicWorkerSource>()> fetchSource) {
41524153
auto& channels =
41534154
KJ_REQUIRE_NONNULL(ioChannels.tryGet<LinkedIoChannels>(), "link() has not been called");

types/generated-snapshot/experimental/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4112,7 +4112,7 @@ interface WorkerStubEntrypointOptions {
41124112
}
41134113
interface WorkerLoader {
41144114
get(
4115-
name: string,
4115+
name: string | null,
41164116
getCode: () => WorkerLoaderWorkerCode | Promise<WorkerLoaderWorkerCode>,
41174117
): WorkerStub;
41184118
}

types/generated-snapshot/experimental/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4125,7 +4125,7 @@ export interface WorkerStubEntrypointOptions {
41254125
}
41264126
export interface WorkerLoader {
41274127
get(
4128-
name: string,
4128+
name: string | null,
41294129
getCode: () => WorkerLoaderWorkerCode | Promise<WorkerLoaderWorkerCode>,
41304130
): WorkerStub;
41314131
}

types/generated-snapshot/latest/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3832,7 +3832,7 @@ interface WorkerStubEntrypointOptions {
38323832
}
38333833
interface WorkerLoader {
38343834
get(
3835-
name: string,
3835+
name: string | null,
38363836
getCode: () => WorkerLoaderWorkerCode | Promise<WorkerLoaderWorkerCode>,
38373837
): WorkerStub;
38383838
}

types/generated-snapshot/latest/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3843,7 +3843,7 @@ export interface WorkerStubEntrypointOptions {
38433843
}
38443844
export interface WorkerLoader {
38453845
get(
3846-
name: string,
3846+
name: string | null,
38473847
getCode: () => WorkerLoaderWorkerCode | Promise<WorkerLoaderWorkerCode>,
38483848
): WorkerStub;
38493849
}

0 commit comments

Comments
 (0)