StaffCoder
.NET
.NET.NET 10
12 modules · 48 challengesSign up
Module 5 · Middleware

Maintenance Mode Switch

easy—No data yet

Problem statement

During a deployment the API should answer every caller with a friendly unavailable response instead of letting requests flow to the endpoints. Complete MaintenanceModeMiddleware and MaintenanceModeExtensions so that a request is turned away with a 503 and a Retry-After header while the maintenance switch is on, and reaches its handler untouched while the switch is off.

Service

The Maintenance Stage

MaintenanceModeMiddleware is handed the next stage of the pipeline and an IMaintenanceSwitch. The switch reports whether maintenance is on through IsEnabled and the retry window through RetryAfterSeconds, and is turned on and off through Enable and Disable. It starts off with a retry window of 120 seconds and can be flipped at any moment while the application is running, so the stage reads it on every request and the very next request follows the setting it finds.

Every handler records its own request in IDownstreamCallLog before it does anything else, as the method and the path it serves, so the log is the record of which handlers actually ran. Submitted orders are held by IOrderStore, which starts empty and numbers each stored order from 1 upwards.

MaintenanceModeExtensions

  • UseMaintenanceMode:
    • Add MaintenanceModeMiddleware to the application pipeline and return the same builder it was handed, so the rest of the pipeline is built on it. Program calls it before mapping the controllers, so the stage sits in front of everything that follows, routing included.

MaintenanceModeMiddleware

  • InvokeAsync handles one request, reading the switch the class was handed and deciding for itself whether to pass that request on or answer it.
    • While the switch is off, pass the request to the next stage and leave its answer untouched: the status code, the body and the headers of the handler all reach the caller unchanged, no Retry-After header is added, and the stage writes nothing of its own. A path no endpoint serves is still answered 404, the request reaching routing as usual.
    • While the switch is on, answer the request without ever invoking the next stage, whatever its path or method, and for every request in a row rather than only the first after the switch was turned on. Nothing downstream runs, so no handler records an entry in the call log and no order is stored, and the order listing is still empty once maintenance ends.
    • Answer a turned-away request with 503 (Service Unavailable), a Retry-After header holding the switch's current retry window as a whole number of seconds, read from the switch on each request rather than fixed in the stage, and a MaintenanceNotice body served as application/json carrying status of maintenance, message of The service is temporarily unavailable for planned maintenance., and retryAfterSeconds holding that same window.
Sample Requests & Responses

While the switch is off, every call is answered by its own handler and the stage adds nothing.

GET /api/status

Response code: 200

X-Handled-By: status-endpoint
{
  "service": "orders",
  "status": "ok"
}
  • POST /api/orders with a body of { "sku": "SKU-1234", "quantity": 5 } answers 201 with a Location of /api/orders/1 and the stored order, the first one being numbered 1.
  • GET /api/orders afterwards answers 200 with every stored order, ordered by orderId.
  • GET / answers 200, and GET /no-such-path answers 404, routing answering it as usual.

While the switch is on, every call is answered by the stage itself, whatever its path or method.

GET /api/status with a retry window of 120 seconds

The handler never runs, so the call log stays empty and the header the handler would have set is absent.

Response code: 503

Retry-After: 120
{
  "status": "maintenance",
  "message": "The service is temporarily unavailable for planned maintenance.",
  "retryAfterSeconds": 120
}
  • POST /api/orders is answered the same way, and the order is not stored, so a listing taken once maintenance ends comes back empty.
  • GET /no-such-path with a retry window of 45 seconds is answered the same way with a Retry-After of 45 and a retryAfterSeconds of 45, the stage running before routing.
  • GET /api/status once maintenance has been turned off again answers 200 as before.

More in Middleware

See the module
Request Timing LoggerThis problem covers timing every request as it passes through the application and leaving one log entry for it, recording the method, the path, the status it ended with and how long it took, without changing the answer itself.0%0 of 1 solvedeasyPipeline Order RepairThis problem covers putting the stages a request passes through into the right order, so failures anywhere are caught and turned into a proper answer, and a protected endpoint is guarded before it ever runs.—No data yeteasyCorrelation ID PipelineThis problem covers giving every request one id that follows it all the way through the application, so each stage and each log line reports the same one, and it comes back on the response whatever happened to the request.—No data yeteasy

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.