github.com/qiuhoude/go-web@v0.0.0-20220223060959-ab545e78f20d/prepare/23_proto_actor/routing/main.go (about) 1 package main 2 3 import ( 4 "log" 5 "strconv" 6 "time" 7 8 console "github.com/AsynkronIT/goconsole" 9 "github.com/AsynkronIT/protoactor-go/actor" 10 "github.com/AsynkronIT/protoactor-go/router" 11 ) 12 13 type myMessage struct{ i int } 14 15 func (m *myMessage) Hash() string { 16 return strconv.Itoa(m.i) 17 } 18 19 func main() { 20 log.Println("Round robin routing:") 21 22 rootContext := actor.EmptyRootContext 23 act := func(context actor.Context) { 24 switch msg := context.Message().(type) { 25 case *myMessage: 26 log.Printf("%v got message %d", context.Self(), msg.i) 27 } 28 } 29 30 pid := rootContext.Spawn(router.NewRoundRobinPool(5).WithFunc(act)) 31 for i := 0; i < 10; i++ { 32 rootContext.Send(pid, &myMessage{i}) 33 } 34 time.Sleep(1 * time.Second) 35 log.Println("Random routing:") 36 pid = rootContext.Spawn(router.NewRandomPool(5).WithFunc(act)) 37 for i := 0; i < 10; i++ { 38 rootContext.Send(pid, &myMessage{i}) 39 } 40 time.Sleep(1 * time.Second) 41 log.Println("ConsistentHash routing:") 42 pid = rootContext.Spawn(router.NewConsistentHashPool(5).WithFunc(act)) 43 for i := 0; i < 10; i++ { 44 rootContext.Send(pid, &myMessage{i}) 45 } 46 time.Sleep(1 * time.Second) 47 log.Println("BroadcastPool routing:") 48 pid = rootContext.Spawn(router.NewBroadcastPool(5).WithFunc(act)) 49 for i := 0; i < 10; i++ { 50 rootContext.Send(pid, &myMessage{i}) 51 } 52 console.ReadLine() 53 }