// Act, returns a value of true to signal that it is // done and should not be scheduled again. func (a *actor) Act(g grid.Grid, exit <-chan bool) bool { tx, err := grid.NewSender(g.Nats(), 100) if err != nil { log.Printf("%v: error: %v", a.ID(), err) } defer tx.Close() rx, err := grid.NewReceiver(g.Nats(), a.ID(), 1, 0) if err != nil { log.Printf("%v: error: %v", a.ID(), err) } defer rx.Close() ticker := time.NewTicker(2 * time.Second) defer ticker.Stop() for { select { case <-exit: return true case now := <-ticker.C: if a.ID() == Leader { tx.Send(Follower, &Msg{Time: now, From: a.ID()}) } case m := <-rx.Msgs(): log.Printf("%v: received: %v", a.ID(), m) } } }
func (a *ConsumerActor) Act(g grid.Grid, exit <-chan bool) bool { tx, err := grid.NewSender(g.Nats(), 100) if err != nil { log.Fatalf("%v: error: %v", a.ID(), err) } defer tx.Close() rx, err := grid.NewReceiver(g.Nats(), a.ID(), 4, 0) if err != nil { log.Fatalf("%v: error: %v", a.ID(), err) } defer rx.Close() a.tx = tx a.rx = rx a.grid = g a.exit = exit a.chaos = NewChaos(a.ID()) defer a.chaos.Stop() d := dfa.New() d.SetStartState(Starting) d.SetTerminalStates(Exiting, Terminating) d.SetTransitionLogger(func(state dfa.State) { log.Printf("%v: switched to state: %v", a, state) }) d.SetTransition(Starting, EverybodyStarted, Running, a.Running) d.SetTransition(Starting, EverybodyFinished, Terminating, a.Terminating) d.SetTransition(Starting, Failure, Exiting, a.Exiting) d.SetTransition(Starting, Exit, Exiting, a.Exiting) d.SetTransition(Running, SendFailure, Resending, a.Resending) d.SetTransition(Running, EverybodyFinished, Finishing, a.Finishing) d.SetTransition(Running, Failure, Exiting, a.Exiting) d.SetTransition(Running, Exit, Exiting, a.Exiting) d.SetTransition(Resending, SendSuccess, Running, a.Running) d.SetTransition(Resending, SendFailure, Resending, a.Resending) d.SetTransition(Resending, Failure, Exiting, a.Exiting) d.SetTransition(Resending, Exit, Exiting, a.Exiting) d.SetTransition(Finishing, EverybodyFinished, Terminating, a.Terminating) d.SetTransition(Finishing, Failure, Exiting, a.Exiting) d.SetTransition(Finishing, Exit, Exiting, a.Exiting) final, _ := d.Run(a.Starting) if final == Terminating { return true } return false }