Drain a fixed backlog of arrivals against a bounded depth and turn the remaining depth into a drain, steady, or throttle remediation action.

backlog depth The backlog depth and arrivals are scalars, so the remaining capacity that drives the remediation action stays deterministic without draining a live async queue.

Async Queue Remediation Report

arrivals
async_queue_remediation.js
Replay: real traced execution (multi-file project)
const arrivals = 1;
const depth = 4;

function remediate(count) {
    let drained = count;
    if (drained > depth) {
        drained = depth;
    }
    const remaining = depth - drained;
    let action = "steady";
    if (drained <= 1) {
        action = "drain";
    } else if (remaining === 0) {
        action = "throttle";
    }
    console.log(`arrivals=${count} drained=${drained} remaining=${remaining} ${action}`);
}

remediate(arrivals);
const arrivals = 3;
const depth = 4;

function remediate(count) {
    let drained = count;
    if (drained > depth) {
        drained = depth;
    }
    const remaining = depth - drained;
    let action = "steady";
    if (drained <= 1) {
        action = "drain";
    } else if (remaining === 0) {
        action = "throttle";
    }
    console.log(`arrivals=${count} drained=${drained} remaining=${remaining} ${action}`);
}

remediate(arrivals);
const arrivals = 5;
const depth = 4;

function remediate(count) {
    let drained = count;
    if (drained > depth) {
        drained = depth;
    }
    const remaining = depth - drained;
    let action = "steady";
    if (drained <= 1) {
        action = "drain";
    } else if (remaining === 0) {
        action = "throttle";
    }
    console.log(`arrivals=${count} drained=${drained} remaining=${remaining} ${action}`);
}

remediate(arrivals);
  1. arrivals ← 1, depth ← 4

    1const arrivals→ 1 = 1;  //@arrivals=3, 52const depth→ 4 = 4;34function remediate(count) {5    let drained = count;6    if (drained > depth) {7        drained = depth;8    }9    const remaining = depth - drained;10    let action = "steady";11    if (drained <= 1) {12        action = "drain";13    } else if (remaining === 0) {14        action = "throttle";15    }16    console.log(`arrivals=${count} drained=${drained} remaining=${remaining} ${action}`);17}1819remediate(arrivals1);
  2. drained ← 1, count ← 1, remaining ← 3, depth ← 4, action ← steady

    1const arrivals = 1;  //@arrivals=3, 52const depth = 4;34function remediate(count1) {5    let drained→ 1 = count→ 1;6    if (drained > depth) {7        drained = depth;8    }9    const remaining→ 3 = depth→ 4 - drained1;10    let action→ steady = "steady";11    if (drained <= 1) {
  3. if (drained <= 1)

    9const remaining = depth - drained;10let action = "steady";11if (drained1 <= 1) {12    action = "drain";13} else if (remaining === 0) {
  4. console.log(`arrivals=${count} drained=${drained} remaining=${remainin…

    14        action = "throttle";15    }16    console.log(`arrivals=${count1} drained=${drained1} remaining=${remaining3} ${actiondrain}`);17}
    outputarrivals=1 drained=1 remaining=3 drain
  5. remediate(arrivals);

    16    console.log(`arrivals=${count} drained=${drained} remaining=${remaining} ${action}`);17}1819remediate(arrivals1);
  1. arrivals ← 3, depth ← 4

    1const arrivals→ 3 = 3;2const depth→ 4 = 4;34function remediate(count) {5    let drained = count;6    if (drained > depth) {7        drained = depth;8    }9    const remaining = depth - drained;10    let action = "steady";11    if (drained <= 1) {12        action = "drain";13    } else if (remaining === 0) {14        action = "throttle";15    }16    console.log(`arrivals=${count} drained=${drained} remaining=${remaining} ${action}`);17}1819remediate(arrivals3);
  2. drained ← 3, count ← 3, remaining ← 1, depth ← 4, action ← steady

    1const arrivals = 3;2const depth = 4;34function remediate(count3) {5    let drained→ 3 = count→ 3;6    if (drained > depth) {7        drained = depth;8    }9    const remaining→ 1 = depth→ 4 - drained3;10    let action→ steady = "steady";11    if (drained <= 1) {12        action = "drain";13    } else if (remaining === 0) {14        action = "throttle";15    }16    console.log(`arrivals=${count3} drained=${drained3} remaining=${remaining1} ${actionsteady}`);17}
    outputarrivals=3 drained=3 remaining=1 steady
  3. remediate(arrivals);

    16    console.log(`arrivals=${count} drained=${drained} remaining=${remaining} ${action}`);17}1819remediate(arrivals3);
  1. arrivals ← 5, depth ← 4

    1const arrivals→ 5 = 5;2const depth→ 4 = 4;34function remediate(count) {5    let drained = count;6    if (drained > depth) {7        drained = depth;8    }9    const remaining = depth - drained;10    let action = "steady";11    if (drained <= 1) {12        action = "drain";13    } else if (remaining === 0) {14        action = "throttle";15    }16    console.log(`arrivals=${count} drained=${drained} remaining=${remaining} ${action}`);17}1819remediate(arrivals5);
  2. drained ← 5, count ← 5

    1const arrivals = 5;2const depth = 4;34function remediate(count5) {5    let drained→ 5 = count→ 5;6    if (drained > depth) {
  3. if (drained > depth)

    4function remediate(count) {5    let drained = count;6    if (drained5 > depth4) {7        drained = depth4;8    }
  4. remaining ← 0, depth ← 4, action ← steady

    7    drained = depth;8}9const remaining→ 0 = depth→ 4 - drained4;10let action→ steady = "steady";11if (drained <= 1) {
  5. if (remaining === 0)

    12    action = "drain";13} else if (remaining0 === 0) {14    action = "throttle";15}
  6. console.log(`arrivals=${count} drained=${drained} remaining=${remainin…

    14        action = "throttle";15    }16    console.log(`arrivals=${count5} drained=${drained4} remaining=${remaining0} ${actionthrottle}`);17}
    outputarrivals=5 drained=4 remaining=0 throttle
  7. remediate(arrivals);

    16    console.log(`arrivals=${count} drained=${drained} remaining=${remaining} ${action}`);17}1819remediate(arrivals5);