Skip to content

Commit

Permalink
Merge pull request #2621 from cloudflare/milan/replica-routing-compat…
Browse files Browse the repository at this point in the history
…-flag

Add `replicaRouting` compat flag
  • Loading branch information
MellowYarker authored Aug 29, 2024
2 parents ea718b8 + 619b393 commit 38ea945
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 6 deletions.
15 changes: 10 additions & 5 deletions src/workerd/api/actor.c++
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ public:
GlobalActorOutgoingFactory(uint channelId,
jsg::Ref<DurableObjectId> id,
kj::Maybe<kj::String> locationHint,
ActorGetMode mode)
ActorGetMode mode,
bool enableReplicaRouting)
: channelId(channelId),
id(kj::mv(id)),
locationHint(kj::mv(locationHint)),
mode(mode) {}
mode(mode),
enableReplicaRouting(enableReplicaRouting) {}

kj::Own<WorkerInterface> newSingleUseClient(kj::Maybe<kj::String> cfStr) override {
auto& context = IoContext::current();
Expand All @@ -71,7 +73,7 @@ public:
// Lazily initialize actorChannel
if (actorChannel == kj::none) {
actorChannel = context.getGlobalActorChannel(
channelId, id->getInner(), kj::mv(locationHint), mode, span);
channelId, id->getInner(), kj::mv(locationHint), mode, enableReplicaRouting, span);
}

return KJ_REQUIRE_NONNULL(actorChannel)
Expand All @@ -87,6 +89,7 @@ private:
jsg::Ref<DurableObjectId> id;
kj::Maybe<kj::String> locationHint;
ActorGetMode mode;
bool enableReplicaRouting;
kj::Maybe<kj::Own<IoChannelFactory::ActorChannel>> actorChannel;
};

Expand Down Expand Up @@ -147,8 +150,10 @@ jsg::Ref<DurableObject> DurableObjectNamespace::getImpl(jsg::Lock& js,
locationHint = kj::mv(o.locationHint);
}

auto outgoingFactory = context.addObject<Fetcher::OutgoingFactory>(
kj::heap<GlobalActorOutgoingFactory>(channel, id.addRef(), kj::mv(locationHint), mode));
bool enableReplicaRouting = FeatureFlags::get(js).getReplicaRouting();
auto outgoingFactory =
context.addObject<Fetcher::OutgoingFactory>(kj::heap<GlobalActorOutgoingFactory>(
channel, id.addRef(), kj::mv(locationHint), mode, enableReplicaRouting));
auto requiresHost = FeatureFlags::get(js).getDurableObjectFetchRequiresSchemeAuthority()
? Fetcher::RequiresHostAndProtocol::YES
: Fetcher::RequiresHostAndProtocol::NO;
Expand Down
6 changes: 6 additions & 0 deletions src/workerd/io/compatibility-date.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -588,4 +588,10 @@ struct CompatibilityFlags @0x8f8c1b68151b6cef {
# Enables node:zlib implementation while it is in-development.
# Once the node:zlib implementation is complete, this will be automatically enabled when
# nodejs_compat is enabled.

replicaRouting @60 :Bool
$compatEnableFlag("replica_routing")
$experimental;
# Enables routing to a replica on the client-side.
# Doesn't mean requests *will* be routed to a replica, only that they can be.
}
1 change: 1 addition & 0 deletions src/workerd/io/io-channels.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ class IoChannelFactory {
const ActorIdFactory::ActorId& id,
kj::Maybe<kj::String> locationHint,
ActorGetMode mode,
bool enableReplicaRouting,
SpanParent parentSpan) = 0;

// Get an actor stub from the given namespace for the actor with the given name.
Expand Down
3 changes: 2 additions & 1 deletion src/workerd/io/io-context.h
Original file line number Diff line number Diff line change
Expand Up @@ -721,9 +721,10 @@ class IoContext final: public kj::Refcounted, private kj::TaskSet::ErrorHandler
const ActorIdFactory::ActorId& id,
kj::Maybe<kj::String> locationHint,
ActorGetMode mode,
bool enableReplicaRouting,
SpanParent parentSpan) {
return getIoChannelFactory().getGlobalActor(
channel, id, kj::mv(locationHint), mode, kj::mv(parentSpan));
channel, id, kj::mv(locationHint), mode, enableReplicaRouting, kj::mv(parentSpan));
}
kj::Own<IoChannelFactory::ActorChannel> getColoLocalActorChannel(
uint channel, kj::StringPtr id, SpanParent parentSpan) {
Expand Down
2 changes: 2 additions & 0 deletions src/workerd/server/server.c++
Original file line number Diff line number Diff line change
Expand Up @@ -2205,9 +2205,11 @@ private:
const ActorIdFactory::ActorId& id,
kj::Maybe<kj::String> locationHint,
ActorGetMode mode,
bool enableReplicaRouting,
SpanParent parentSpan) override {
JSG_REQUIRE(mode == ActorGetMode::GET_OR_CREATE, Error,
"workerd only supports GET_OR_CREATE mode for getting actor stubs");
JSG_REQUIRE(!enableReplicaRouting, Error, "workerd does not support replica routing.");
auto& channels =
KJ_REQUIRE_NONNULL(ioChannels.tryGet<LinkedIoChannels>(), "link() has not been called");

Expand Down
1 change: 1 addition & 0 deletions src/workerd/tests/test-fixture.c++
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ struct DummyIoChannelFactory final: public IoChannelFactory {
const ActorIdFactory::ActorId& id,
kj::Maybe<kj::String> locationHint,
ActorGetMode mode,
bool enableReplicaRouting,
SpanParent parentSpan) override {
KJ_FAIL_REQUIRE("no actor channels");
}
Expand Down

0 comments on commit 38ea945

Please sign in to comment.