MCPcopy Create free account
hub / github.com/astercloud/aster / runSupervisorDemo

Function runSupervisorDemo

examples/actor/main.go:308–351  ·  view source on GitHub ↗

runSupervisorDemo 监督者策略演示

(ctx context.Context, cmd *cli.Command)

Source from the content-addressed store, hash-verified

306
307// runSupervisorDemo 监督者策略演示
308func runSupervisorDemo(ctx context.Context, cmd *cli.Command) error {
309 fmt.Println("\n🛡️ 监督者策略演示(故障恢复)")
310 fmt.Println(strings.Repeat("=", 50))
311
312 // 使用自定义配置,静默 panic 日志
313 config := actor.DefaultSystemConfig()
314 config.PanicHandler = func(a *actor.PID, msg actor.Message, err any) {
315 // 静默处理,不打印堆栈
316 }
317 system := actor.NewSystemWithConfig("supervisor-demo", config)
318 defer system.Shutdown()
319
320 // 创建不稳定的 Actor(会失败 2 次)
321 unstable := &UnstableActor{name: "Unstable", maxFails: 2}
322
323 // 使用 OneForOne 监督策略(允许 5 次重启)
324 props := &actor.Props{
325 Name: "unstable",
326 MailboxSize: 100,
327 SupervisorStrategy: actor.NewOneForOneStrategy(5, time.Minute, actor.DefaultDecider),
328 }
329 pid := system.SpawnWithProps(unstable, props)
330
331 fmt.Println("\n📤 发送消息,触发故障和自动恢复...")
332
333 // 发送消息触发故障
334 for i := 1; i <= 4; i++ {
335 fmt.Printf("\n--- 第 %d 次尝试 ---\n", i)
336 resp, err := pid.Request(&PingMsg{Count: i}, 2*time.Second)
337 if err != nil {
338 fmt.Printf("⏳ 请求超时(Actor 可能正在重启)\n")
339 time.Sleep(200 * time.Millisecond)
340 continue
341 }
342
343 if pong, ok := resp.(*PongMsg); ok {
344 fmt.Printf("📥 成功收到 Pong(%d) - Actor 已恢复!\n", pong.Count)
345 }
346 }
347
348 fmt.Println("\n✅ 监督者策略演示完成!")
349 fmt.Println(" Actor 在 2 次故障后自动恢复并正常工作")
350 return nil
351}
352
353// runPipelineDemo 流水线处理演示
354func runPipelineDemo(ctx context.Context, cmd *cli.Command) error {

Callers

nothing calls this directly

Calls 7

DefaultSystemConfigFunction · 0.92
NewSystemWithConfigFunction · 0.92
NewOneForOneStrategyFunction · 0.92
PrintfMethod · 0.80
ShutdownMethod · 0.45
SpawnWithPropsMethod · 0.45
RequestMethod · 0.45

Tested by

no test coverage detected