Skip to content

Commit 3289720

Browse files
committed
Add routingMode option to GetDurableObjectOptions
1 parent 158df63 commit 3289720

File tree

10 files changed

+46
-13
lines changed

10 files changed

+46
-13
lines changed

src/workerd/api/actor.c++

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ kj::Own<WorkerInterface> GlobalActorOutgoingFactory::newSingleUseClient(
5353
KJ_SWITCH_ONEOF(channelIdOrFactory) {
5454
KJ_CASE_ONEOF(channelId, uint) {
5555
actorChannel = context.getGlobalActorChannel(channelId, id->getInner(),
56-
kj::mv(locationHint), mode, enableReplicaRouting, tracing.span);
56+
kj::mv(locationHint), mode, enableReplicaRouting, kj::mv(routingMode), tracing.span);
5757
}
5858
KJ_CASE_ONEOF(factory, kj::Own<DurableObjectNamespace::ActorChannelFactory>) {
59-
actorChannel = factory->getGlobalActor(
60-
id->getInner(), kj::mv(locationHint), mode, enableReplicaRouting, tracing.span);
59+
actorChannel = factory->getGlobalActor(id->getInner(), kj::mv(locationHint), mode,
60+
enableReplicaRouting, kj::mv(routingMode), tracing.span);
6161
}
6262
}
6363
}
@@ -146,24 +146,31 @@ jsg::Ref<DurableObject> DurableObjectNamespace::getImpl(jsg::Lock& js,
146146
jsg::Optional<GetDurableObjectOptions> options) {
147147
JSG_REQUIRE(idFactory->matchesJurisdiction(id->getInner()), TypeError,
148148
"get called on jurisdictional subnamespace with an ID from a different jurisdiction");
149+
KJ_IF_SOME(o, options) {
150+
KJ_IF_SOME(routingMode, o.routingMode) {
151+
JSG_REQUIRE(routingMode == "primary-only", RangeError, "unknown routingMode: ", routingMode);
152+
}
153+
}
149154

150155
auto& context = IoContext::current();
151-
kj::Maybe<kj::String> locationHint = kj::none;
156+
kj::Maybe<kj::String> locationHint;
157+
kj::Maybe<kj::String> routingMode;
152158
KJ_IF_SOME(o, options) {
153159
locationHint = kj::mv(o.locationHint);
160+
routingMode = kj::mv(o.routingMode);
154161
}
155162

156163
bool enableReplicaRouting = FeatureFlags::get(js).getReplicaRouting();
157164

158165
kj::Own<Fetcher::OutgoingFactory> outgoingFactory;
159166
KJ_SWITCH_ONEOF(channel) {
160167
KJ_CASE_ONEOF(channelId, uint) {
161-
outgoingFactory = kj::heap<GlobalActorOutgoingFactory>(
162-
channelId, id.addRef(), kj::mv(locationHint), mode, enableReplicaRouting);
168+
outgoingFactory = kj::heap<GlobalActorOutgoingFactory>(channelId, id.addRef(),
169+
kj::mv(locationHint), mode, enableReplicaRouting, kj::mv(routingMode));
163170
}
164171
KJ_CASE_ONEOF(channelFactory, IoOwn<ActorChannelFactory>) {
165172
outgoingFactory = kj::heap<GlobalActorOutgoingFactory>(kj::addRef(*channelFactory),
166-
id.addRef(), kj::mv(locationHint), mode, enableReplicaRouting);
173+
id.addRef(), kj::mv(locationHint), mode, enableReplicaRouting, kj::mv(routingMode));
167174
}
168175
}
169176

src/workerd/api/actor.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ class DurableObjectNamespace: public jsg::Object {
156156
kj::Maybe<kj::String> locationHint,
157157
ActorGetMode mode,
158158
bool enableReplicaRouting,
159+
kj::Maybe<kj::String> routingMode,
159160
SpanParent parentSpan) = 0;
160161
};
161162

@@ -195,13 +196,22 @@ class DurableObjectNamespace: public jsg::Object {
195196

196197
struct GetDurableObjectOptions {
197198
jsg::Optional<kj::String> locationHint;
199+
// `routingMode` may be be of interest to applications using Durable Objects replicas. It can be
200+
// one of the following options:
201+
// - none: the default, indicates we will pick for the application.
202+
// - "primary-only": guarantees we route directly to the primary (skip any replicas.)
203+
jsg::Optional<kj::String> routingMode;
198204

199-
JSG_STRUCT(locationHint);
205+
JSG_STRUCT(locationHint, routingMode);
200206

201-
JSG_STRUCT_TS_DEFINE(type DurableObjectLocationHint = "wnam" | "enam" | "sam" | "weur" | "eeur" | "apac" | "oc" | "afr" | "me");
202207
// Possible values from https://developers.cloudflare.com/workers/runtime-apis/durable-objects/#providing-a-location-hint
208+
JSG_STRUCT_TS_DEFINE(
209+
type DurableObjectLocationHint = "wnam" | "enam" | "sam" | "weur" | "eeur" | "apac" | "oc" | "afr" | "me";
210+
type DurableObjectRoutingMode = "primary-only");
211+
203212
JSG_STRUCT_TS_OVERRIDE({
204213
locationHint?: DurableObjectLocationHint;
214+
routingMode?: DurableObjectRoutingMode;
205215
});
206216
};
207217

@@ -270,12 +280,14 @@ class GlobalActorOutgoingFactory final: public Fetcher::OutgoingFactory {
270280
jsg::Ref<DurableObjectId> id,
271281
kj::Maybe<kj::String> locationHint,
272282
ActorGetMode mode,
273-
bool enableReplicaRouting)
283+
bool enableReplicaRouting,
284+
kj::Maybe<kj::String> routingMode)
274285
: channelIdOrFactory(kj::mv(channelIdOrFactory)),
275286
id(kj::mv(id)),
276287
locationHint(kj::mv(locationHint)),
277288
mode(mode),
278-
enableReplicaRouting(enableReplicaRouting) {}
289+
enableReplicaRouting(enableReplicaRouting),
290+
routingMode(kj::mv(routingMode)) {}
279291

280292
kj::Own<WorkerInterface> newSingleUseClient(kj::Maybe<kj::String> cfStr) override;
281293

@@ -285,6 +297,7 @@ class GlobalActorOutgoingFactory final: public Fetcher::OutgoingFactory {
285297
kj::Maybe<kj::String> locationHint;
286298
ActorGetMode mode;
287299
bool enableReplicaRouting;
300+
kj::Maybe<kj::String> routingMode;
288301
kj::Maybe<kj::Own<IoChannelFactory::ActorChannel>> actorChannel;
289302
};
290303

src/workerd/io/io-channels.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ class IoChannelFactory {
208208
kj::Maybe<kj::String> locationHint,
209209
ActorGetMode mode,
210210
bool enableReplicaRouting,
211+
kj::Maybe<kj::String> routingMode,
211212
SpanParent parentSpan) = 0;
212213

213214
// Get an actor stub from the given namespace for the actor with the given name.

src/workerd/io/io-context.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -894,9 +894,10 @@ class IoContext final: public kj::Refcounted, private kj::TaskSet::ErrorHandler
894894
kj::Maybe<kj::String> locationHint,
895895
ActorGetMode mode,
896896
bool enableReplicaRouting,
897+
kj::Maybe<kj::String> routingMode,
897898
SpanParent parentSpan) {
898-
return getIoChannelFactory().getGlobalActor(
899-
channel, id, kj::mv(locationHint), mode, enableReplicaRouting, kj::mv(parentSpan));
899+
return getIoChannelFactory().getGlobalActor(channel, id, kj::mv(locationHint), mode,
900+
enableReplicaRouting, kj::mv(routingMode), kj::mv(parentSpan));
900901
}
901902
kj::Own<IoChannelFactory::ActorChannel> getColoLocalActorChannel(
902903
uint channel, kj::StringPtr id, SpanParent parentSpan) {

src/workerd/server/server.c++

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3276,10 +3276,12 @@ class Server::WorkerService final: public Service,
32763276
kj::Maybe<kj::String> locationHint,
32773277
ActorGetMode mode,
32783278
bool enableReplicaRouting,
3279+
kj::Maybe<kj::String> routingMode,
32793280
SpanParent parentSpan) override {
32803281
JSG_REQUIRE(mode == ActorGetMode::GET_OR_CREATE, Error,
32813282
"workerd only supports GET_OR_CREATE mode for getting actor stubs");
32823283
JSG_REQUIRE(!enableReplicaRouting, Error, "workerd does not support replica routing.");
3284+
JSG_REQUIRE(routingMode == kj::none, Error, "workerd does not support replica routing.");
32833285
auto& channels =
32843286
KJ_REQUIRE_NONNULL(ioChannels.tryGet<LinkedIoChannels>(), "link() has not been called");
32853287

src/workerd/tests/test-fixture.c++

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ struct DummyIoChannelFactory final: public IoChannelFactory {
104104
kj::Maybe<kj::String> locationHint,
105105
ActorGetMode mode,
106106
bool enableReplicaRouting,
107+
kj::Maybe<kj::String> routingMode,
107108
SpanParent parentSpan) override {
108109
KJ_FAIL_REQUIRE("no actor channels");
109110
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,8 +641,10 @@ type DurableObjectLocationHint =
641641
| "oc"
642642
| "afr"
643643
| "me";
644+
type DurableObjectRoutingMode = "primary-only";
644645
interface DurableObjectNamespaceGetDurableObjectOptions {
645646
locationHint?: DurableObjectLocationHint;
647+
routingMode?: DurableObjectRoutingMode;
646648
}
647649
interface DurableObjectClass<
648650
_T extends Rpc.DurableObjectBranded | undefined = undefined,

types/generated-snapshot/experimental/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,8 +646,10 @@ export type DurableObjectLocationHint =
646646
| "oc"
647647
| "afr"
648648
| "me";
649+
export type DurableObjectRoutingMode = "primary-only";
649650
export interface DurableObjectNamespaceGetDurableObjectOptions {
650651
locationHint?: DurableObjectLocationHint;
652+
routingMode?: DurableObjectRoutingMode;
651653
}
652654
export interface DurableObjectClass<
653655
_T extends Rpc.DurableObjectBranded | undefined = undefined,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,8 +619,10 @@ type DurableObjectLocationHint =
619619
| "oc"
620620
| "afr"
621621
| "me";
622+
type DurableObjectRoutingMode = "primary-only";
622623
interface DurableObjectNamespaceGetDurableObjectOptions {
623624
locationHint?: DurableObjectLocationHint;
625+
routingMode?: DurableObjectRoutingMode;
624626
}
625627
interface DurableObjectClass<
626628
_T extends Rpc.DurableObjectBranded | undefined = undefined,

types/generated-snapshot/latest/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,8 +624,10 @@ export type DurableObjectLocationHint =
624624
| "oc"
625625
| "afr"
626626
| "me";
627+
export type DurableObjectRoutingMode = "primary-only";
627628
export interface DurableObjectNamespaceGetDurableObjectOptions {
628629
locationHint?: DurableObjectLocationHint;
630+
routingMode?: DurableObjectRoutingMode;
629631
}
630632
export interface DurableObjectClass<
631633
_T extends Rpc.DurableObjectBranded | undefined = undefined,

0 commit comments

Comments
 (0)