From 735d314a19cbef0ef8531cf8febbf546e4d6f8ff Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Wed, 21 May 2025 13:50:34 -0400 Subject: [PATCH] examples/panic: Make an example to show off aforementioned feature --- examples/panic/main.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 examples/panic/main.go diff --git a/examples/panic/main.go b/examples/panic/main.go new file mode 100644 index 0000000..4e6763e --- /dev/null +++ b/examples/panic/main.go @@ -0,0 +1,35 @@ +// Example panic demonstrates how the environment can restart actors if they +// fail. +package main + +import "log" +import "time" +import "errors" +import "context" +import "math/rand" +import "git.tebibyte.media/sashakoshka/camfish" + +func main() { + camfish.Run("panic", + "Example panic demonstrates how the environment can restart " + + "actors if they fail", + new(actor)) +} + +// actor is an incorrectly implemented actor that panics and errs randomly. +type actor struct { } +var _ camfish.Runnable = new(actor) +func (this *actor) Type() string { return "panic" } + +func (this *actor) Run(ctx context.Context) error { + log.Println("(i) [panic] panicking in 10 seconds") + select { + case <- ctx.Done(): return ctx.Err() + case <- time.After(time.Second * 10): + if rand.Int() % 2 == 0 { + panic("this is a panic") + } else { + return errors.New("this is an error") + } + } +}