Skip to content

Practical Patterns

With the primitive duration and scheduling blocks, several recurring patterns found in real systems become straightforward to express in Zolo.

Five copy-ready patterns: heartbeat with a maximum count, remaining TTL calculation, exponential backoff scaling, warning/error bands by elapsed time, and a scheduled warm-up combined with a periodic loop.

08-practical-patterns.zolo
Playground
// Feature: practical patterns with durations

// Syntax: combinations of `every`, `after`, arithmetic, comparison

// When to use: copy-paste templates for real cases.


// === Pattern 1: heartbeat with a max count ===

var beats = 0
let max_beats = 3
every 30ms {
  beats = beats + 1
  print("heartbeat {beats}")
  if beats >= max_beats {
    break
  }
}

// expected: heartbeat 1 / 2 / 3


// === Pattern 2: TTL/expiration ===

let ttl_start = 0
let ttl = 3600000 + 1800000  // 5_400_000ms

let now = 1800000  // 1_800_000ms

let used = now - ttl_start
let remaining = ttl - used
print("TTL remaining: {remaining}ms")

// expected: TTL remaining: 3600000ms


// === Pattern 3: exponential backoff (durations) ===

let base = 100
let try1 = base
let try2 = base * 2
let try3 = base * 4
let try4 = base * 8
print("backoff: {try1} / {try2} / {try3} / {try4}")

// expected: backoff: 100 / 200 / 400 / 800


// === Pattern 4: time bands (warning, error) ===

let elapsed = 7000
let warn_at = 5000
let error_at = 10000
if elapsed > error_at {
  print("ERROR: too slow")
} else {
  if elapsed > warn_at {
    print("WARN: starting to slow down")
  } else {
    print("OK: within limit")
  }
}

// expected: WARN: starting to slow down


// === Pattern 5: schedule warm-up + heartbeat ===

print("scheduling")
after 30ms {
  print("warm-up done")
}
var hb = 0
every 50ms {
  hb = hb + 1
  if hb >= 2 { break }
}
print("finished")
// expected:

// scheduling

// warm-up done

// finished

Heartbeat uses every with a counter and break when the maximum is reached. TTL is simple subtraction: remaining = ttl - (now - start). Exponential backoff multiplies the base by powers of 2. Alert bands are if/else comparing elapsed against thresholds in milliseconds. The warm-up + loop pattern combines after (fire-and-forget) with every to keep the periodic task running while the setup finishes in parallel.

enespt-br