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:

go
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.Handler

Acceptance criteria

  • A flag that has not been configured, such as search_ranking in New(nil), is enabled by default.
  • New respects the supplied initial values: search_ranking is disabled and checkout is enabled when initialized with map[string]bool{"search_ranking": false, "checkout": true}.
  • FromEnv("W6O1_", "search_ranking") disables search_ranking when W6O1_SEARCH_RANKING is set to off, OFF, false, 0 or disabled.
  • When W6O1_SEARCH_RANKING is on and W6O1_RECOMMENDATIONS is unset, both search_ranking and recommendations are enabled.
  • Set("search_ranking", false) disables the flag, and Set("search_ranking", true) enables it again.
  • A handler returned by Guard serves ranked while search_ranking is enabled, unranked after the flag is disabled, and ranked again after it is enabled.
  • Disabling search_ranking leaves checkout enabled: the search handler serves unranked, while the checkout handler continues to serve checkout.
  • Changing snap["a"] to false in a snapshot of switches initialized with map[string]bool{"a": true} does not change the internal value of a.
  • Concurrent Set, Enabled and Snapshot calls 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:

bash
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.

Finished Exercise 6.15?

Implementation

Run locally

Write the code in your editor inside zomato-prep, then work through this list.

  1. Implement the exported Switches API.
  2. Load initial flag values from environment variables.
  3. Make runtime updates safe for concurrent access.
  4. Return independent copies from Snapshot.
  5. Route each request to the enabled or fallback handler.
  6. Pass all nine tests and the race-detector run.

Commands

terminal
cd backend && make test W=6O1
terminal
cd backend && go test -race -tags w6o1 ./internal/killswitch/

Tests

  • TestW6O1_UnknownFlagsAreEnabled in backend/internal/killswitch/killswitch_test.go
  • TestW6O1_NewHonoursTheInitialValues in backend/internal/killswitch/killswitch_test.go
  • TestW6O1_FromEnvTurnsAFlagOffForOffFalseZeroAndDisabled in backend/internal/killswitch/killswitch_test.go
  • TestW6O1_FromEnvLeavesOtherValuesAndUnsetFlagsOn in backend/internal/killswitch/killswitch_test.go
  • TestW6O1_SetFlipsAFlagAtRuntime in backend/internal/killswitch/killswitch_test.go
  • TestW6O1_GuardServesTheFallbackWhileTheFlagIsOff in backend/internal/killswitch/killswitch_test.go
  • TestW6O1_OneFlagDoesNotAffectAnother in backend/internal/killswitch/killswitch_test.go
  • TestW6O1_SnapshotIsACopy in backend/internal/killswitch/killswitch_test.go
  • TestW6O1_ConcurrentSetAndEnabledAreSafe in backend/internal/killswitch/killswitch_test.go