withSlot
Suspend until fair share grants leaf a slot, run block with its lease, and charge the lease the time the block took, in whole cost units rounded up. Answers what block returned.
The charge is made however the block ends: when it returns, when it throws, and when it is cancelled part-way, and the throwable or the cancellation then goes on to the caller. Work that failed or was cancelled still held the slot, and a lane whose work fails fast must not get the farm for free. The clock starts when the block starts, so the moment between the grant and the caller resuming is never charged. A caller cancelled before its block starts is handed back with nothing charged, as at acquire.
A block may settle its own lease with complete or cancel; the charge at its end then finds the lease closed, answers LeaseOutcome.Unknown, and changes nothing.
Parameters
the cost the caller expects the work to take, as at acquire.
Throws
where acquire would answer SlotOutcome.Refused: no lease can be granted to leaf, and block never runs.
when reservation is not in 1..LocalHeddle.MAX_RESERVATION, before anything waits.
the pool's failure, when it fails while the caller is queued: whatever the engine threw, or an IllegalStateException for an answer the engine's contract rules out (see Failures, above). block never runs.
Samples
val farm = Topology("farm", listOf(Topology.Lane("web", Weight.of(3)), Topology.Lane("mobile")))
val web = farm.groupId("web")
val mobile = farm.groupId("mobile")
val (heddle, booted) = LocalHeddle.fromTopology(farm, LocalPolicy(defaultReservation = 3L), epoch = 0L)
check(booted.refusals.isEmpty())
val pool = FairSharePool(heddle, slots = 1, TimeSource.Monotonic, costUnit = 1.minutes)
val built = mutableListOf<String>()
coroutineScope {
// One agent: whichever lane's turn comes second waits for the first build to finish.
launch { built += pool.withSlot(web) { "web build" } }
launch { built += pool.withSlot(mobile) { "mobile build" } }
}
check(built.sorted() == listOf("mobile build", "web build"))
// A name the farm does not declare is refused before anything runs.
val refused = try {
pool.withSlot(GroupId("nightly")) { "never runs" }
} catch (e: SlotRefusedException) {
"refused: ${e.reason}"
}
check(refused.startsWith("refused"))