def loop

bend-kit-http@0.38.2.0 · admission.bend:205

import bend-kit-http@0.38.2.0/admission.bend as Admission
import Base

205def loop(+inbox: Chan(Command), +running: Bool, +closing: Bool, step: Step) -> IO(Counts):
206  match step:
207    case Waiting{ledger}:
208      do IO<Counts>:
209        got : Maybe<&1, Command> <- Chan.recv(Command, inbox)
210        loop(inbox, running, closing, Received{ledger, got})
211    case Acquired{reply, booked}:
212      match booked:
213        case Broken{}:
214          IO.die(Counts, 1, "Admission: a lease count broke")
215        case Booked{ledger, admitted}:
216          do IO<Counts>:
217            sent : Result<&1, &1, Bool, Unit> <- Chan.send(Bool, reply, admitted)
218            loop(inbox, running, closing, Waiting{ledger})
219    case Released{reply, booked}:
220      match booked:
221        case Broken{}:
222          IO.die(Counts, 1, "Admission.give: no lease of this kind is held")
223        case Booked{ledger, admitted}:
224          do IO<Counts>:
225            sent : Result<&1, &1, Unit, Unit> <- Chan.send(Unit, reply, Unit{})
226            loop(inbox, running, closing, Finishing{idle(closing, counts(ledger))})
227    case Counted{reply, counted}:
228      (snapshot, ledger) = counted
229      do IO<Counts>:
230        sent : Result<&1, &1, Counts, Unit> <- Chan.send(Counts, reply, snapshot)
231        loop(inbox, running, closing, Waiting{ledger})
232    case Finishing{checked}:
233      (done, ledger) = checked
234      match done:
235        case True{}:
236          do IO<Counts>:
237            Chan.close(Command, inbox)
238            return final(ledger)
239        case False{}:
240          loop(inbox, running, closing, Waiting{ledger})
241    case Received{ledger, got}:
242      match got:
243        case None{}:
244          IO.pure(Counts, final(ledger))
245        case Some{command}:
246          match command:
247            case Acquire{kind, reply}:
248              loop(inbox, running, closing, Acquired{reply, acquire.running(running, kind, ledger)})
249            case Release{kind, reply}:
250              loop(inbox, running, closing, Released{reply, release(kind, ledger)})
251            case Snapshot{reply}:
252              loop(inbox, running, closing, Counted{reply, counts(ledger)})
253            case Quiesce{reply}:
254              do IO<Counts>:
255                sent : Result<&1, &1, Bool, Unit> <- Chan.send(Bool, reply, running)
256                loop(inbox, False{}, closing, Waiting{ledger})
257            case Running{reply}:
258              do IO<Counts>:
259                sent : Result<&1, &1, Bool, Unit> <- Chan.send(Bool, reply, running)
260                loop(inbox, running, closing, Waiting{ledger})
261            case Stop{}:
262              loop(inbox, running, True{}, Finishing{idle(True{}, counts(ledger))})

Uses (22)

Used by (2)