view
The pool as it stands: each leaf's queued reservations, the open count and the slot count, copied under the pool's lock. Use it where the engine is not needed, say a gauge of the open count; for a question the engine answers, read the view withEngine hands its block, so the answer and the view are of the same moment.
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 = 2, TimeSource.Monotonic, costUnit = 1.minutes)
val running = listOf(pool.acquire(web), pool.acquire(mobile)).map { (it as SlotOutcome.Granted).lease }
coroutineScope {
// Five builds queue behind the two running, each with its estimate in minutes.
val builds = listOf(web to 3L, web to 12L, mobile to 3L, web to 3L, mobile to 3L)
builds.forEach { (lane, minutes) ->
launch(start = CoroutineStart.UNDISPATCHED) { pool.withSlot(lane, reservation = minutes) { } }
}
// A gauge needs no engine: each lane's queued estimates, head first, and the slots in use.
val view = pool.view()
check(view.queues == mapOf(web to listOf(3L, 12L, 3L), mobile to listOf(3L, 3L)))
check(view.open == 2 && view.slots == 2)
// Who goes next is a question for the engine, about the pool's own queues: ask it inside
// withEngine, so the view and the answer are of the same moment.
val next = pool.withEngine { engine, queued -> engine.previewPicks(queued.queues, queued.waiting) }
check(next == listOf(web, mobile, web, mobile, web))
running.forEach { pool.complete(it.id, actualCost = it.reserved) }
}
check(pool.view() == PoolView(emptyMap(), open = 0, slots = 2))Content copied to clipboard