Runtime feature switches
You build a runtime feature-switch component that stores named flags and routes each request to a feature handler or fallback. Unknown flags stay enabled, and updates affect subsequent requests.
Ticket
Build a runtime feature-switch component in backend/internal/killswitch. It stores named boolean flags and lets an HTTP handler choose between a feature handler and its fallback on each request.
Search ranking can cause errors or excessive latency while checkout remains healthy. A switch lets you disable search ranking without restarting the application, while keeping checkout available. This exercise is optional and stands alone. The next exercise computes p99 from a histogram scrape and does not depend on these switches.
The tests call this exported API directly:
import "net/http"
type Switches struct { /* unexported state */ }
func New(initial map[string]bool) *Switches
func FromEnv(prefix string, names ...string) *Switches
func (s *Switches) Enabled(name string) bool
func (s *Switches) Set(name string, on bool)
func (s *Switches) Snapshot() map[string]bool
func (s *Switches) Guard(name string, on, off http.Handler) http.HandlerAcceptance criteria
- A flag that has not been configured, such as
search_rankinginNew(nil), is enabled by default. Newrespects the supplied initial values:search_rankingis disabled andcheckoutis enabled when initialized withmap[string]bool{"search_ranking": false, "checkout": true}.FromEnv("W6O1_", "search_ranking")disablessearch_rankingwhenW6O1_SEARCH_RANKINGis set tooff,OFF,false,0ordisabled.- When
W6O1_SEARCH_RANKINGisonandW6O1_RECOMMENDATIONSis unset, bothsearch_rankingandrecommendationsare enabled. Set("search_ranking", false)disables the flag, andSet("search_ranking", true)enables it again.- A handler returned by
Guardservesrankedwhilesearch_rankingis enabled,unrankedafter the flag is disabled, andrankedagain after it is enabled. - Disabling
search_rankingleavescheckoutenabled: the search handler servesunranked, while the checkout handler continues to servecheckout. - Changing
snap["a"]tofalsein a snapshot of switches initialized withmap[string]bool{"a": true}does not change the internal value ofa. - Concurrent
Set,EnabledandSnapshotcalls complete across 50 concurrent writer and reader pairs without data races.
Constraints
- Use the Go standard library. Do not add external services or additional configuration requirements.
- Do not modify the existing tests.
Design direction
Keep the flag state and its operations inside the killswitch package. Have New establish the initial state, FromEnv build that state from environment variables, and Snapshot return a separate copy. Let Guard make the handler-selection decision when a request arrives.
A sync.RWMutex is a good fit because flag checks are reads and runtime changes are writes. Keep the read lock around map access, and use an exclusive lock for updates. Copy the map while holding a read lock so the snapshot cannot race with a concurrent update.
Treat environment variables as initial configuration. Construct each variable name by joining the supplied prefix with the upper-cased flag name. Normalize its value before checking for off, false, 0 or disabled. Use Set for changes after construction.
Unknown flags follow a fail-open policy: they are enabled unless explicitly disabled. That is convenient for optional features, but a production system may need a different default for a feature where enabling it carries a safety or financial risk.
Keep the runtime decision in Guard, rather than capturing the flag value when the handler is created. Any layout that meets the contract passes.
How to check your work
Run the build-tagged tests first, then run the package with the race detector:
cd backend
make test W=6O1
go test -race -tags w6o1 ./internal/killswitch/The first failing assertion identifies the flag or handler behaviour that does not match the contract. Fix that behaviour before investigating failures later in the output.
The reference solution passes every check for this exercise. Try the hints first; reading the answer before you have struggled with the problem teaches much less.
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
- Implement the exported
SwitchesAPI. - Load initial flag values from environment variables.
- Make runtime updates safe for concurrent access.
- Return independent copies from
Snapshot. - Route each request to the enabled or fallback handler.
- Pass all nine tests and the race-detector run.
Commands
cd backend && make test W=6O1cd backend && go test -race -tags w6o1 ./internal/killswitch/Tests
TestW6O1_UnknownFlagsAreEnabledinbackend/internal/killswitch/killswitch_test.goTestW6O1_NewHonoursTheInitialValuesinbackend/internal/killswitch/killswitch_test.goTestW6O1_FromEnvTurnsAFlagOffForOffFalseZeroAndDisabledinbackend/internal/killswitch/killswitch_test.goTestW6O1_FromEnvLeavesOtherValuesAndUnsetFlagsOninbackend/internal/killswitch/killswitch_test.goTestW6O1_SetFlipsAFlagAtRuntimeinbackend/internal/killswitch/killswitch_test.goTestW6O1_GuardServesTheFallbackWhileTheFlagIsOffinbackend/internal/killswitch/killswitch_test.goTestW6O1_OneFlagDoesNotAffectAnotherinbackend/internal/killswitch/killswitch_test.goTestW6O1_SnapshotIsACopyinbackend/internal/killswitch/killswitch_test.goTestW6O1_ConcurrentSetAndEnabledAreSafeinbackend/internal/killswitch/killswitch_test.go