-
Notifications
You must be signed in to change notification settings - Fork 296
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1426 from cloudflare/dominik/autogates
Implements autogate framework.
- Loading branch information
Showing
5 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright (c) 2017-2023 Cloudflare, Inc. | ||
// Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
// https://opensource.org/licenses/Apache-2.0 | ||
#include "autogate.h" | ||
|
||
namespace workerd::server { | ||
|
||
kj::StringPtr KJ_STRINGIFY(AutogateKey key) { | ||
switch (key) { | ||
case AutogateKey::TEST_WORKERD: | ||
return "test-workerd"_kj; | ||
case AutogateKey::NumOfKeys: | ||
KJ_FAIL_ASSERT("NumOfKeys should not be used in getName"); | ||
} | ||
} | ||
|
||
Autogate::Autogate(capnp::List<config::Config::Autogate, capnp::Kind::STRUCT>::Reader autogates) { | ||
for (auto autogate : autogates) { | ||
if (!autogate.hasName()) { | ||
continue; | ||
} | ||
auto name = autogate.getName(); | ||
if (!name.startsWith("workerd-autogate-")) { | ||
continue; | ||
} | ||
auto sliced = name.slice(17); | ||
|
||
// Parse the gate name into a AutogateKey. | ||
for (AutogateKey i = AutogateKey(0); i < AutogateKey::NumOfKeys; i = AutogateKey((int)i + 1)) { | ||
if (kj::str(i) == sliced) { | ||
gates.insert(i, autogate.getEnabled()); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
bool Autogate::isEnabled(AutogateKey key) const { | ||
return gates.find(key).orDefault(false); | ||
} | ||
|
||
kj::Maybe<Autogate> globalAutogate; | ||
void 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) { | ||
globalAutogate = Autogate(autogates); | ||
} | ||
|
||
} // namespace workerd::server |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) 2017-2023 Cloudflare, Inc. | ||
// Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
// https://opensource.org/licenses/Apache-2.0 | ||
#pragma once | ||
|
||
#include "kj/debug.h" | ||
#include <kj/map.h> | ||
#include <workerd/server/workerd.capnp.h> | ||
|
||
namespace workerd::server { | ||
|
||
// Workerd-specific list of autogate keys (can also be used in internal repo). | ||
enum class AutogateKey { | ||
TEST_WORKERD, | ||
NumOfKeys // Reserved for iteration. | ||
}; | ||
|
||
// This class allows code changes to be rolled out independent of full binary releases. It enables | ||
// specific code paths to be gradually rolled out via our internal tooling. | ||
// See the equivalent file in our internal repo for more details. | ||
// | ||
// Workerd-specific gates can be added here. | ||
// | ||
// Usage: | ||
// | ||
// #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. | ||
class Autogate { | ||
|
||
public: | ||
Autogate(capnp::List<config::Config::Autogate, capnp::Kind::STRUCT>::Reader autogates); | ||
|
||
bool isEnabled(AutogateKey key) const; | ||
|
||
private: | ||
kj::HashMap<AutogateKey, bool> gates; | ||
}; | ||
|
||
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters