StaffCoder
.NET
.NET.NET 10
12 modules · 48 challengesSign up
Module 9 · Options and Feature Management

Feature Flag Service

medium—No data yet

Problem statement

Complete the implementation of ServiceCollectionExtensions, FeatureFlagService and FlagBroadcaster so that one request always reads the same flags, a long lived reader keeps up with every change, and a flag set carrying a value it is not allowed to carry stops the application from starting.

Sample Data

Configuration ships with a Flags section carrying the flags the application holds under no audience name, and an Audiences section under it carrying one flag set per audience:

{
  "Flags": {
    "Channel": "stable",
    "RolloutPercent": 25,
    "CheckoutEnabled": true,
    "SearchEnabled": false,
    "Audiences": {
      "beta": {
        "Channel": "beta-ring",
        "RolloutPercent": 60,
        "CheckoutEnabled": false,
        "SearchEnabled": true
      },
      "staff": {
        "Channel": "internal",
        "RolloutPercent": 100,
        "CheckoutEnabled": true,
        "SearchEnabled": true
      }
    }
  }
}

FlagSettings is the shape every one of those sections carries: a Channel, a RolloutPercent, a CheckoutEnabled and a SearchEnabled. A set nothing has filled in carries an empty channel, a rollout percent of 0 and both flags off.

A flag set is allowed a Channel of at least 2 characters and a RolloutPercent from 0 to 100 inclusive, and nothing else.

AudienceCatalog.Names holds the audiences this application serves, beta then staff.

Configuration can change while the application is running, so the values in these sections are not fixed for the life of the process.

Service

ServiceCollectionExtensions

Program calls AddFeatureFlags on the service collection while the application is being built. Implement it so that:

  • The flags held under no audience name are bound to Flags, and every audience in AudienceCatalog.Names has flags of its own, held under that audience's own name and bound to that audience's section of Flags:Audiences. Each set therefore carries all four of its own values, and none of another set's.
  • Each of those bindings follows its own section for as long as the application runs, so a later change to one audience's section moves that audience's flags and leaves the other audience's, and the ones held under no name, where they were.
  • Every one of those flag sets, the one held under no name and each audience's, is checked against the allowed values while the application starts. A set outside them stops the application from starting, and the failure names the member that is not allowed. A set at the edge of them, a RolloutPercent of 0 or of 100 or a two character Channel, is allowed, and the application starts and serves it.

FeatureFlagService

One FeatureFlagService serves one request, and Program already registers it that way. Implement Read, which is handed the audience taken from the route and answers a FlagOutcome carrying either flags or an error:

  • A name in AudienceCatalog.Names is answered with the flags the running application holds under that name, rather than with a fresh reading of that audience's configuration section. An application that adjusts one audience's flags after they are bound answers that audience with the adjustment, and every other audience without it.
  • Those flags hold still for the whole of one request. When configuration is reloaded in the middle of a request, that request goes on reading the values it started with, and the request after it reads the new ones.
  • Names are matched exactly, so Beta and STAFF are not audiences, and neither is Audiences.
  • Reading flags under a name nothing was configured under answers a set nothing has filled in, rather than failing, so a name outside AudienceCatalog.Names has to be turned away before any flags are read.
  • Such a name is answered with no flags and this error: Audience '{audience}' is not configured. Configured audiences are {audiences}., where {audience} stands for the name that was asked for and {audiences} stands for AudienceCatalog.Names, in its own order, separated by a comma and a space.

FlagBroadcaster

One FlagBroadcaster serves the whole application, and Program already registers it that way. It follows the flags held under no audience name. Implement it so that:

  • Current answers those flags as they stand now. It starts on the values configuration was carrying when the broadcaster was created, and follows every later change with no restart and no new scope: the one broadcaster that answered the old values answers the new ones, and every scope reads that same broadcaster and the same refresh count.
  • Current is the flag set the application is handing out at that moment, rather than a copy of it.
  • RefreshCount starts at 0 and adds exactly one each time configuration is reloaded, however many audiences the application configures. A reload that carries a key the flags do not use, a reload that republishes the values already in force, and a reload that moves only one audience's section, each add one and leave Current where it was.

GET /flags/{audience} is provided. It reads the audience's flags through the service, then any configuration change that is due is applied, then it reads that audience's flags through the service again. It answers 200 with the two readings under atStart and atEnd, the broadcaster's Current under live and its RefreshCount under refreshCount, and 404 with the error under a message key when the outcome carries no flags.

Sample Requests & Responses

GET /flags/beta, on a freshly started application

Response code: 200

{
  "audience": "beta",
  "atStart": {
    "channel": "beta-ring",
    "rolloutPercent": 60,
    "checkoutEnabled": false,
    "searchEnabled": true
  },
  "atEnd": {
    "channel": "beta-ring",
    "rolloutPercent": 60,
    "checkoutEnabled": false,
    "searchEnabled": true
  },
  "live": {
    "channel": "stable",
    "rolloutPercent": 25,
    "checkoutEnabled": true,
    "searchEnabled": false
  },
  "refreshCount": 0
}

GET /flags/staff answers 200 the same way, carrying internal, 100, true and true, so neither audience reports a value of the other.

Carrying on in that same running application, with a change falling due in the middle of the next request that moves Flags to canary, 40, false and true and Flags:Audiences:beta to beta-wide, 85, true and false:

  • GET /flags/beta answers both its readings as beta-ring, 60, false and true, the values it started the request with, a live of canary, 40, false and true, and a refreshCount of 1.
  • The next GET /flags/beta answers both its readings as beta-wide, 85, true and false, the same live values, and a refreshCount still 1.

GET /flags/gold

Response code: 404

{
  "message": "Audience 'gold' is not configured. Configured audiences are beta, staff."
}

Beta, STAFF and Audiences are answered the same way, each message naming the name it was handed.

These applications do not start, and each failure names the member that is wrong:

  • Flags:RolloutPercent reads 101, and the failure names RolloutPercent.
  • Flags:RolloutPercent reads -1, and the failure names RolloutPercent.
  • Flags:Audiences:beta:RolloutPercent reads 101, and the failure names RolloutPercent.
  • Flags:Channel reads a, and the failure names Channel.
  • Flags:Audiences:staff:Channel is empty, and the failure names Channel.

More in Options and Feature Management

See the module
Reloadable Feature Settings100%1 of 1 solvedmediumNamed Tenant OptionsThis problem covers giving each tenant its own set of delivery settings, read from that tenant's own part of the configuration, so that one tenant's values never replace another's and the settings the application starts with are left alone.—No data yetmediumOptions Change NotificationsThis problem covers keeping a long lived cache in step with settings that change while the application is running, so that what it serves follows the new settings and it reports how many times it has been refreshed.—No data yetmedium

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.