From b67481375663e14789461c81326cc7acb5482662 Mon Sep 17 00:00:00 2001 From: Jaroslavs Samcuks Date: Sun, 17 Mar 2024 21:12:44 +0100 Subject: [PATCH] examples/restarts: avoid using global variables --- examples/restarts/main.go | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/examples/restarts/main.go b/examples/restarts/main.go index 8b370b9..b3d0525 100644 --- a/examples/restarts/main.go +++ b/examples/restarts/main.go @@ -12,13 +12,7 @@ type message struct { data string } -var wg sync.WaitGroup - -type foo struct{} - -func newFoo() actor.Receiver { - return &foo{} -} +type foo struct{ done func() } func (f *foo) Receive(ctx *actor.Context) { switch msg := ctx.Message().(type) { @@ -28,8 +22,8 @@ func (f *foo) Receive(ctx *actor.Context) { if msg.data == "failed" { panic("I failed processing this message") } - wg.Done() fmt.Println("I restarted and processed the next one perfectly:", msg.data) + f.done() } } @@ -38,8 +32,11 @@ func main() { if err != nil { panic(err) } - pid := engine.Spawn(newFoo, "foo", actor.WithMaxRestarts(3)) + var wg sync.WaitGroup wg.Add(1) + pid := engine.Spawn( + func() actor.Receiver { return &foo{done: wg.Done} }, + "foo", actor.WithMaxRestarts(3)) engine.Send(pid, &message{data: "failed"}) time.Sleep(time.Millisecond) engine.Send(pid, &message{data: "hello world!"})