Bounded ORO LRU+TTL cache, GCRA token-bucket rate limiter, 3-state circuit breaker, jittered backoff, and channel-backed singleflight.
Pure-Veko ORO-native bounded LRU/TTL cache, GCRA & token-bucket rate limiters, 3-state Circuit Breaker, Full-Jitter exponential backoff + hedged retry, and channel-backed thread-safe Singleflight request coalescer (use [res = resilience]).
resilience provides production traffic-shaping, caching, and fault-isolation primitives tailored to Veko's single-owner ORO memory model and shared-nothing spawn concurrency:
new-cache, cache-put, cache-get, cache-delete, cache-sweep, cache-stats) with lazy TTL eviction on read, eager expiration sweeps, and zero reference cycles.new-gcra, allow-gcra?) computing exact IETF RateLimit-* / Retry-After telemetry (allowed, remaining, retry_after_ms, reset_after_ms) alongside monotonic TokenBucket (new-token-bucket, allow-tokens?)..closed -> .open -> .half_open -> .closed state machine (new-breaker, call-with-breaker, breaker-state) that fails fast while .open and probes recovery after reset_timeout_ns.min(max_ms, base_ms * 2^attempt) delay calculation with AWS-style uniform [0, cap] Full Jitter (backoff-delay-ms), retry-with, and hedged-call.new-singleflight, singleflight-do) safe to pass by value across concurrent spawn tasks so thundering-herd cache misses collapse into a single upstream execution.veko pkg add resilience@1.0.0Import the module in .vk source files:
use [res = resilience]For sustained rate R (rate_per_sec) and burst capacity B (burst), each unit cell has emission interval T = 1s / R and maximum Theoretical Arrival Time (TAT) offset L = B * T. For a request arriving at t with previous TAT:
new_tat = max(TAT, t) + cost * T
allow_at = new_tat - B * Tt >= allow_at, the arrival is permitted, TAT advances to new_tat, remaining = (B * T - (new_tat - t)) / T, and reset_after_ms = ceil((new_tat - t) / 1ms).t < allow_at, TAT is left unchanged and retry_after_ms = ceil((allow_at - t) / 1ms).spawn WorkersVeko spawn workers forbid capturing outer mut bindings. Singleflight encapsulates a 1-buffered state channel (chan/new 1) holding a map of in-flight keys to per-waiter 1-slot reply channels. Only the first arriving task executes work_fn (outside the channel lock so distinct keys run concurrently); subsequent concurrent callers for the same key enqueue a reply channel and receive the leader's result (shared = true).
CacheEntry, CacheStats, LruCachenew-cache [max_entries i64, default_ttl_ns i64] LruCachecache-put [mut c LruCache, key str, val any /ttl ttl_ns i64 = -1 /now now_ns i64 = -1] enumcache-get [mut c LruCache, key str /now now_ns i64 = -1] anycache-delete [mut c LruCache, key str] boolcache-sweep [mut c LruCache /now now_ns i64 = -1] i64cache-stats [c LruCache] CacheStatsRateDecision, GcraLimiter, TokenBucketnew-gcra [rate_per_sec i64, burst i64] GcraLimiterallow-gcra? [mut g GcraLimiter, key str /cost cells i64 = 1 /now now_ns i64 = -1] RateDecisionnew-token-bucket [capacity i64, refill_per_sec i64 /now now_ns i64 = -1] TokenBucketallow-tokens? [mut tb TokenBucket, cost i64 /now now_ns i64 = -1] boolCircuitBreakernew-breaker [failure_threshold i64, reset_timeout_ns i64, half_open_successes i64] CircuitBreakerbreaker-state [cb CircuitBreaker /now now_ns i64 = -1] enumcall-with-breaker [mut cb CircuitBreaker, action fn[-> any] /now now_ns i64 = -1] anyRetryPolicybackoff-delay-ms [base_delay_ms i64, max_delay_ms i64, attempt i64 /jitter full_jitter bool = false /seed rng_seed i64 = -1] i64retry-with [policy RetryPolicy, action fn[i64 -> any]] anyhedged-call [primary fn[-> any], backup fn[-> any]] anySingleflight, SingleflightResultnew-singleflight [] Singleflightsingleflight-do [sf Singleflight, key str, work_fn fn[-> any]] SingleflightResultuse [res = resilience]
mut cache = res/new-cache 256 (as-i64 30s)
_ = res/cache-put mut cache 'user:42' { name = 'Ada', tier = 'pro' }
user = res/cache-get/or mut cache 'user:42' [ { name = 'guest' } ]
mut limiter = res/new-gcra 100 20
dec = res/allow-gcra? mut limiter 'ip:203.0.113.9'
if not dec.allowed [
print (str/join ['rate limited; retry in ' (mold dec.retry_after_ms) 'ms'] '')
]use [res = resilience]
mut breaker = res/new-breaker 5 (as-i64 10s) 2
sf = res/new-singleflight
result = res/call-with-breaker/or mut breaker fn [] [
sf_out = res/singleflight-do sf 'catalog:featured' fn [] [
['sku-1' 'sku-2' 'sku-3']
]
sf_out.val
] [
['cached-fallback-sku']
]
print (len result)https://veko-lang.org/pkg/resilience/1.0.0.vkp1d77970cb16e506258d8767b6ad168ba65c8cd786abdf369b822966645fe7c23ebe4c9261af9f50d3ac2d767f5829c9221c214f2cc81224f2a7393db6f121674ed25519:27406074d8681da1d4e6420f5d887cac15c313334c249ac047ba9d4e2df72b70{"name":"resilience","version":"1.0.0","tier":2.0,"tier_label":"Tier 2: Distributed & Wire Protocols","category":"Caching, Rate Limits & Breakers","description":"Bounded ORO LRU+TTL cache, GCRA token-bucket rate limiter, 3-state circuit breaker, jittered backoff, and channel-backed singleflight.","modules":["resilience"],"homepage":"https://veko-lang.org/packages/resilience"}| Bundle Entry Path | Size | Leaf SHA-256 Digest |
|---|---|---|
| README.md | 5 KB | b660d319319ab15053abf7f38b2dfc6e7bfe7692d9c24d4b550f4a562d8ed13d |
| examples/demo.vk | 6 KB | 44bc87e063e96e2dc751475d5ec9b5942c49839f027f898e532286a9234d4220 |
| mod.vk | 15 KB | 59582897a0eaaa990260a819e107b4bd5da928eeba82ce897c59216820501010 |