← Back to Demos & Examples

09_concurrent_worker_pool.vk

Raw .vk
// Example 09: Shared-Nothing Concurrent Worker Pool, Select Multiplexing & Isolation Guards (M6)
// Track A (ORO VM) : veko run examples/09_concurrent_worker_pool.vk
// Track A (.vko)   : veko compile --vko examples/09_concurrent_worker_pool.vk -o dist/09.vko ; veko run dist/09.vko
// Track B (Wasm 3) : veko run --wasm examples/09_concurrent_worker_pool.vk
// Track C (Native) : veko compile --native examples/09_concurrent_worker_pool.vk -o dist/09.exe ; ./dist/09.exe
use [bytes, str]

// Immutable config & top-level pure transforms are safely visible inside every `spawn` worker
worker-cfg = { factor = 10, offset = 3 }

transform-job = fn [job] [
	score = job.value * worker-cfg.factor + worker-cfg.offset
	{ worker = job.lane, id = job.id, score = score }
]

// 1. Fan-out / fan-in over bounded ownership channels + channel multiplexing (`chan/select`)
fast-q = chan/new 4
slow-q = chan/new 4
out-q  = chan/new 4

w1 = spawn [
	// Workers maintain their own isolated local state without sharing heaps
	mut processed = 0
	forever [
		item = chan/recv/or fast-q [break]
		chan/send out-q (transform-job item)
		processed = processed + 1
	]
	processed
]

w2 = spawn [
	mut processed = 0
	forever [
		item = chan/recv/or slow-q [break]
		chan/send out-q (transform-job item)
		processed = processed + 1
	]
	processed
]

// Non-blocking drain check on empty channel returns clean `/or` fallback
println (chan/try-recv/or out-q ['queue-empty'])

// Dispatch jobs (immutable maps move cleanly across threads)
job1 = { lane = 'fast', id = 1, value = 4 }
job2 = { lane = 'slow', id = 2, value = 7 }
chan/send fast-q job1
chan/send slow-q job2

// Wait deterministically for both workers to publish to `out-q`
mut total-score = 0
mut seen = 0
while seen < 2 [
	res = chan/recv out-q
	total-score = total-score + res.score
	seen = seen + 1
]
println (str/append 'total-score=' (mold total-score))

// Close queues cleanly so workers finish their loops and return counts via `task/await`
chan/close fast-q
chan/close slow-q
c1 = task/await w1
c2 = task/await w2
println (str/append 'workers-done=' (mold (c1 + c2)))

// 2. Priority lane selection & timeout handling (`chan/select`, `task/await/timeout/or`)
lane-a = chan/new 2
lane-b = chan/new 2

_notifier = spawn [
	chan/send lane-b 'priority-alert'
	.ok
]
picked = chan/select [lane-a lane-b]
println (str/join ['selected-lane=' (mold picked[0]) ':' picked[1]] '')

// Deadline check on blocked worker unblocks cleanly without leaking tasks
barrier = chan/new 1
deferred-task = spawn [
	msg = chan/recv barrier
	msg
]
deadline-hit = task/await/timeout/or deferred-task 10ms ['deadline-exceeded']
println deadline-hit
chan/send barrier 'completed-after-deadline'
println (task/await deferred-task)

// 3. Zero-trust channel transfer firewall: `.secret`, `mut` loans, and closures cannot leak across tasks
mut tok = bytes/with-secret 32b
bytes/push-all (mut tok) (bytes/from-str 'ephemeral-token')
sec-guard = chan/send/or out-q tok ['secret-blocked']
println sec-guard

mut counter = 9
mut-guard = chan/send/or out-q (mut counter) ['mut-loan-blocked']
println mut-guard

cls-guard = chan/send/or out-q (fn [x] [x + 1]) ['closure-blocked']
println cls-guard

chan/close out-q
chan/close lane-a
chan/close lane-b
chan/close barrier