Saltar al contenido

Patrones Prácticos

En esta página

Con los bloques primitivos de duración y agendamiento, varios patrones recurrentes en sistemas reales se vuelven directos de expresar en Zolo.

Cinco patrones listos para copiar: heartbeat con conteo máximo, cálculo de TTL restante, escalonamiento exponencial de backoff, bandas de aviso/error por tiempo transcurrido, y calentamiento agendado combinado con bucle periódico.

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 usa every con un contador y break al alcanzar el máximo. TTL es una resta simple: restante = ttl - (ahora - inicio). El backoff exponencial multiplica la base por potencias de 2. Las bandas de alerta son if/else que comparan elapsed contra umbrales en milisegundos. El patrón de calentamiento + bucle combina after (fire-and-forget) con every para mantener la tarea periódica en ejecución mientras el setup finaliza en paralelo.

Buscar en Zolo

9 resultados

enespt-br