Fix the shutdown drain plan for a Go service
Your task
Your Go service gets a fixed time to shut down before the orchestrator kills it. An AI's first graceful shutdown only drained the HTTP server, and a worker pool kept running. The new plan lists every component that holds work in flight, in shutdown order, and checks what fits in the budget.
An assistant wrote plan_shutdown_drain(components, drain_ms, budget_ms). It is in the editor. It has a bug. Find it and fix it.
Input and output:
components: the components, in the order they shut down, such ashttp,workers,metrics,db.drain_ms: how long each one takes to drain, at the same position.budget_ms: the total time for the whole shutdown.- Return one
name=statusstring per component, in order. The status isdrained,cutorabandoned.
Rules the tests check:
- If the lists differ in length, use only the positions both have.
- Components drain one after another, and all of them share the one budget. Keep the time used so far, starting at 0.
- For each component, the time left is
budget_msminus the time used so far:- If no time is left (0 or less):
abandoned. - If its drain time is at most the time left:
drained, and its drain time is added to the time used. - Otherwise:
cut. The deadline fires during its drain, so the time used becomes all ofbudget_ms.
- If no time is left (0 or less):
Examples
plan_shutdown_drain(components: list[str], drain_ms: list[int], budget_ms: int) -> list[str]Example 1
Input
plan_shutdown_drain(["http", "workers", "metrics", "db"], [2000, 15000, 500, 300], 25000)Output
["http=drained", "workers=drained", "metrics=drained", "db=drained"]Example 2
Input
plan_shutdown_drain(["http", "workers", "metrics", "db"], [2000, 15000, 500, 300], 5000)Output
["http=drained", "workers=cut", "metrics=abandoned", "db=abandoned"]Example 3
Input
plan_shutdown_drain(["http"], [2000], 5000)Output
["http=drained"]