File
Blob: src/workerd/util/autogate.h
| 1 | // Copyright (c) 2017-2023 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | #pragma once |
| 5 | |
| 6 | #include <workerd/util/strong-bool.h> |
| 7 | |
| 8 | #include <capnp/blob.h> |
| 9 | #include <capnp/list.h> |
| 10 | #include <kj/string.h> |
| 11 | |
| 12 | #include <initializer_list> |
| 13 | |
| 14 | namespace workerd::util { |
| 15 | |
| 16 | // When YES, initAutogate() ignores the WORKERD_ALL_AUTOGATES environment variable and uses only |
| 17 | // the gates list that was explicitly passed in. This is used by the production server, which has |
| 18 | // its own autogate test infrastructure that manages the all-autogates behavior and builds |
| 19 | // selective gate configs (with forceOff / toggle support). |
| 20 | WD_STRONG_BOOL(IgnoreAllAutogatesEnv); |
| 21 | |
| 22 | // Workerd-specific list of autogate keys (can also be used in internal repo). |
| 23 | enum class AutogateKey { |
| 24 | TEST_WORKERD, |
| 25 | // Defers TCP socket connect() to wait for DO output gate, preventing |
| 26 | // network outputs while storage writes are pending. |
| 27 | TCP_SOCKET_CONNECT_OUTPUT_GATE, |
| 28 | V8_FAST_API, |
| 29 | // Enables support for the streaming tail worker. Note that this is currently also guarded behind |
| 30 | // an experimental compat flag. |
| 31 | STREAMING_TAIL_WORKER, |
| 32 | // Enable refactor used to consolidate the different tail worker stream implementations. |
| 33 | TAIL_STREAM_REFACTOR, |
| 34 | // Enable Rust-backed Node.js DNS implementation |
| 35 | RUST_BACKED_NODE_DNS, |
| 36 | // Use ExternalPusher instead of StreamSink to handle streams in RPC. |
| 37 | RPC_USE_EXTERNAL_PUSHER, |
| 38 | // Enable the WebAssembly.instantiate shim that detects modules exporting __instance_signal / |
| 39 | // __instance_terminated and registers them for receiving the CPU-limit shutdown signal. |
| 40 | WASM_SHUTDOWN_SIGNAL_SHIM, |
| 41 | // Enable fast TextEncoder implementation using simdutf |
| 42 | ENABLE_FAST_TEXTENCODER, |
| 43 | // Enable draining read on standard streams |
| 44 | ENABLE_DRAINING_READ_ON_STANDARD_STREAMS, |
| 45 | // Make SqlStorage::isAllowedName case-insensitive and enforce it on virtual tables (FTS5). |
| 46 | SQL_RESTRICT_RESERVED_NAMES, |
| 47 | // Increase the SQLite hard heap limit from 512 MiB to 8 GiB. |
| 48 | INCREASE_SQLITE_HARD_HEAP_LIMIT, |
| 49 | // Enable user span context propagation across worker-to-worker subrequests. |
| 50 | USER_SPAN_CONTEXT_PROPAGATION, |
| 51 | // Apply an updated default autoAllocateChunkSize for ReadableStreams |
| 52 | UPDATED_AUTO_ALLOCATE_CHUNK_SIZE, |
| 53 | // Call abortIsolate() when a Python worker encounters a fatal error. |
| 54 | PYTHON_ABORT_ISOLATE_ON_FATAL_ERROR, |
| 55 | // `jsRpcSession()` returns a session handle instead of having the call itself hang until the |
| 56 | // session is complete. |
| 57 | JSRPC_SESSION_HANDLE, |
| 58 | NumOfKeys // Reserved for iteration. |
| 59 | }; |
| 60 | |
| 61 | // This class allows code changes to be rolled out independent of full binary releases. It enables |
| 62 | // specific code paths to be gradually rolled out via our internal tooling. |
| 63 | // See the equivalent file in our internal repo for more details. |
| 64 | // |
| 65 | // Workerd-specific gates can be added here. |
| 66 | // |
| 67 | // Usage: |
| 68 | // |
| 69 | // #include <workerd/util/autogate.h> |
| 70 | // Autogate::isEnabled(AutogateKey::YOUR_FEATURE_KEY) |
| 71 | // |
| 72 | // When making structural changes here, ensure you align them with autogate.h in the internal repo. |
| 73 | class Autogate { |
| 74 | |
| 75 | public: |
| 76 | static bool isEnabled(AutogateKey key); |
| 77 | |
| 78 | // Creates a global Autogate and seeds it with gates that are specified in the config. |
| 79 | // |
| 80 | // This function is not thread safe, it should be called exactly once close to the start of the |
| 81 | // process before any threads are created. |
| 82 | static void initAutogate(capnp::List<capnp::Text>::Reader autogates, |
| 83 | IgnoreAllAutogatesEnv ignoreEnv = IgnoreAllAutogatesEnv::NO); |
| 84 | |
| 85 | // Convenience method for bin-tests to invoke initAutogate() with an appropriate config. |
| 86 | static void initAutogateNamesForTest(std::initializer_list<kj::StringPtr> gateNames); |
| 87 | |
| 88 | // Initializes all autogates to true. Used for testing with the --all-autogates flag. |
| 89 | static void initAllAutogates(); |
| 90 | |
| 91 | // Destroys an initialized global Autogate instance. Used only for testing. |
| 92 | static void deinitAutogate(); |
| 93 | |
| 94 | private: |
| 95 | bool gates[static_cast<unsigned long>(AutogateKey::NumOfKeys)] = {}; |
| 96 | |
| 97 | Autogate() = default; |
| 98 | Autogate(capnp::List<capnp::Text>::Reader autogates); |
| 99 | }; |
| 100 | |
| 101 | // Retrieves the name of the gate. |
| 102 | // |
| 103 | // When adding a new gate, add it into this method as well. |
| 104 | kj::StringPtr KJ_STRINGIFY(AutogateKey key); |
| 105 | |
| 106 | } // namespace workerd::util |