Pudu programming language
Menu
Package

@chrismichaelps / pudu-lang-resilience

Resilience pipelines for Pudu: retry, circuit breaker, timeout, fallback, hedging, rate limiting, and chaos injection

0.1.0Apache-2.01

InstallClose

Hedging.pudu

Pudu29 lines1.1 KB

GitHub ↗
1/** @Examples.Hedging.Program — a hedged request beating a slow one */2module Hedging34import Std.Io as Io5import Std.Sync as Sync6import PuduLangResilience.Context as Context7import PuduLangResilience.Hedging as Hedging8import PuduLangResilience.Pipeline as Pipeline9import PuduLangResilience as Resilience1011/// Starts a hedge when the first replica is slow and answers the first replica to reply.12fn main() -> Int {13  let pipeline = match Pipeline.build([Hedging.strategy(Hedging.Options{..Hedging.defaults(), maxHedgedAttempts: 2, delay: 50})]) {14    case Ok(found) => found15    case Err(invalid) => {16      let _said = Io.writeErrorLine(Pipeline.explain(&invalid))17      return 118    }19  }20  let replicas = Sync.counter(0)21  let answer = Pipeline.execute(&pipeline, fn(context: Context.Context) -> Resilience.Outcome[Str, Str] {22      let replica = match Sync.increment(&replicas, 1) { case Ok(n) => n case Err(_) => 0 }23      if replica == 1 { Context.pause(&context, 5000) ? }24      Ok("replica " + show(replica))25    })26  let _shown = Io.writeLine("answered by " + Resilience.summarize(&answer))27  if answer == Ok("replica 2") { 0 } else { 1 }28}29