PeaBox Lab › PeaLang › Learn › Chapter 13 › Lesson 5

Part III — Advanced Systems · Chapter 13 · Lesson 5 of 5 · 3 min read

Chapter 13 Checkpoint: Bounded Concurrent Workflow

Checkpoint level: Advanced

Purpose: Build a concurrent order-pricing workflow that overlaps host lookups, bounds concurrency and time, cancels cleanly, and produces deterministic results.

Release note: async APIs and host registration calls are illustrative. Verify them against your installed v0.63 release.

Checkpoint outcomes

By completing this checkpoint, you will demonstrate that you can:

Scenario

Each order needs a customer tier (host database) and a stock check (host inventory service). The workflow must price a batch of synthetic orders using the familiar reasons GOLD_BULK, GOLD, BULK, NONE, and VALIDATION_ERROR, with a bulk threshold of 10.

13C.1 Requirements

AreaRequirement
ConcurrencyAt most 10 orders in flight
Per-call deadline1 second for tier and stock lookups
Batch deadline60 seconds
Tier fallback"standard" on timeout
Stock failureOrder gets VALIDATION_ERROR
Failure policyCollect-all; one bad order never discards the batch
CancellationRelease stock holds; write no partial result for cancelled orders
OutputSame order and content as input, every run

13C.2 Reference structure

async function price_one(order, token) {
  let tier = "standard"
  try {
    with std.async.deadline(seconds=1.0) {
      tier = await host.lookup_tier(order["customer_id"], token)
    }
  } catch err {
    if err.kind == "Cancelled" { throw err }
  }
  let hold = null
  try {
    with std.async.deadline(seconds=1.0) {
      hold = await host.reserve_stock(order["sku"], order["qty"], token)
    }
  } catch err {
    if err.kind == "Cancelled" { throw err }
    return {"id": order["id"], "reason": "VALIDATION_ERROR"}
  }
  try {
    return {"id": order["id"], "reason": reason_for(tier, order["qty"])}
  } finally {
    await host.release_if_unconfirmed(hold)
  }
}

export async function price_batch(orders, token) {
  with std.async.deadline(seconds=60.0) {
    return await std.async.gather(
      [price_one(o, token) for o in orders],
      limit=10, return_errors=true)
  }
}

reason_for is the synchronous rule function from earlier chapters: GOLD_BULK for gold with quantity of at least 10, GOLD for gold, BULK for quantity of at least 10, otherwise NONE.

13C.3 Required test matrix

CaseFake host behaviourExpected result
AAll lookups fast and successfulCorrect reasons; output in input order
BTier lookup exceeds 1 s for one gold orderThat order uses "standard" (BULK or NONE)
CStock service fails for one orderThat order is VALIDATION_ERROR; others unaffected
D50 orders, observe in-flight countNever more than 10 concurrent
EBatch cancelled after half the ordersNo partial results for cancelled orders; all holds released
FAll lookups slow so 60 s deadline firesTimeout raised; holds released
GRun case A five timesByte-identical output each time
HRun case A on interpreter and VMIdentical output

13C.4 Acceptance criteria

13C.5 Submission package

13C.6 Assessment rubric

AreaEvidence of mastery
Async designWaits overlap; no blocking calls in async code.
Boundary safetyPortable values, safe errors, named capabilities.
StructureAll tasks owned by groups; none leak.
Time controlPer-call and batch deadlines behave as specified.
CancellationCleanup always runs; Cancelled is never swallowed.
DeterminismOutput order and content are stable across runs and engines.

Completion standard: Cases A to H pass, no holds leak, and the design note justifies every limit and deadline.

13C.7 Reflection questions

Chapter 13 summary

You can now write async PeaLang that overlaps waiting work, cross the async host boundary safely, bound work with cancellation and deadlines, and own every task through task groups. The checkpoint showed that these tools produce concurrent programs that are still predictable and testable.

Transition to Chapter 14

Concurrency multiplies what a script can do in a given time. Chapter 14 turns to limiting what it may do at all, beginning with capability-safe execution and least privilege.

Checkpoint status: Ready for review.

← Previous Next →