Skip to content

Commit

Permalink
Merge pull request #1436 from cloudflare/dominik/autogates2
Browse files Browse the repository at this point in the history
Autogate fixes and improvements.
  • Loading branch information
dom96 authored Nov 24, 2023
2 parents 9b77c55 + 2a3ce25 commit b230f0f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/workerd/server/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ wd_cc_library(
srcs = ["autogate.c++"],
hdrs = ["autogate.h"],
visibility = ["//visibility:public"],
deps = ["@capnp-cpp//src/kj:kj", ":workerd_capnp"],
deps = ["@capnp-cpp//src/kj:kj", ":workerd_capnp", "//src/workerd/util:sentry"],
)

[kj_test(
Expand Down
21 changes: 16 additions & 5 deletions src/workerd/server/autogate.c++
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0
#include "autogate.h"
#include <workerd/util/sentry.h>

namespace workerd::server {

kj::Maybe<Autogate> globalAutogate;

kj::StringPtr KJ_STRINGIFY(AutogateKey key) {
switch (key) {
case AutogateKey::TEST_WORKERD:
Expand All @@ -21,6 +24,7 @@ Autogate::Autogate(capnp::List<config::Config::Autogate, capnp::Kind::STRUCT>::R
}
auto name = autogate.getName();
if (!name.startsWith("workerd-autogate-")) {
LOG_ERROR_ONCE("Autogate configuration includes gate with invalid prefix.");
continue;
}
auto sliced = name.slice(17);
Expand All @@ -35,21 +39,28 @@ Autogate::Autogate(capnp::List<config::Config::Autogate, capnp::Kind::STRUCT>::R
}
}

bool Autogate::isEnabled(AutogateKey key) const {
return gates.find(key).orDefault(false);
bool Autogate::isEnabled(AutogateKey key) {
KJ_IF_SOME(a, globalAutogate) {
return a.gates.find(key).orDefault(false);
}
LOG_ERROR_PERIODICALLY(
kj::str("Autogates not initialised, check for ", key, " will have no effect"));
return false;
}

kj::Maybe<Autogate> globalAutogate;
void initAutogate(config::Config::Reader config) {
void Autogate::initAutogate(config::Config::Reader config) {
if (!config.hasAutogates()) {
return;
}

globalAutogate = Autogate(config.getAutogates());
}

void initAutogate(capnp::List<config::Config::Autogate, capnp::Kind::STRUCT>::Reader autogates) {
void Autogate::initAutogate(capnp::List<config::Config::Autogate,
capnp::Kind::STRUCT>::Reader autogates) {
globalAutogate = Autogate(autogates);
}

void Autogate::deinitAutogate() { globalAutogate = kj::none; }

} // namespace workerd::server
24 changes: 14 additions & 10 deletions src/workerd/server/autogate.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,30 @@ enum class AutogateKey {
// #include <workerd/server/autogate.h>
// Autogate::isEnabled(AutogateKey::YOUR_FEATURE_KEY)
//
// When making structural changes here, ensure you align them with autogate.h in workerd.
// When making structural changes here, ensure you align them with autogate.h in the internal repo.
class Autogate {

public:
Autogate(capnp::List<config::Config::Autogate, capnp::Kind::STRUCT>::Reader autogates);

bool isEnabled(AutogateKey key) const;

static bool isEnabled(AutogateKey key);

// Creates a global Autogate and seeds it with gates that are specified in the config.
//
// This function is not thread safe, it should be called exactly once close to the start of the
// process before any threads are created.
static void initAutogate(config::Config::Reader config);
static void initAutogate(
capnp::List<config::Config::Autogate, capnp::Kind::STRUCT>::Reader autogates);
// Destroys an initialised global Autogate instance. Used only for testing.
static void deinitAutogate();
private:
kj::HashMap<AutogateKey, bool> gates;

Autogate(capnp::List<config::Config::Autogate, capnp::Kind::STRUCT>::Reader autogates);
};

// Retrieves the name of the gate.
//
// When adding a new gate, add it into this method as well.
kj::StringPtr KJ_STRINGIFY(AutogateKey key);

extern kj::Maybe<Autogate> globalAutogate;

void initAutogate(config::Config::Reader config);
void initAutogate(capnp::List<config::Config::Autogate, capnp::Kind::STRUCT>::Reader autogates);

} // namespace workerd::server
2 changes: 1 addition & 1 deletion src/workerd/server/workerd.c++
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ public:
}

KJ_IF_SOME(c, config) {
initAutogate(c);
Autogate::initAutogate(c);
}
}

Expand Down

0 comments on commit b230f0f

Please sign in to comment.