github.com/qiuhoude/go-web@v0.0.0-20220223060959-ab545e78f20d/prepare/23_proto_actor/limitconcurrency/main.go (about) 1 package main 2 3 import ( 4 "log" 5 6 console "github.com/AsynkronIT/goconsole" 7 "github.com/AsynkronIT/protoactor-go/actor" 8 "github.com/AsynkronIT/protoactor-go/router" 9 ) 10 11 type workItem struct{ i int } 12 13 const maxConcurrency = 5 14 15 func doWork(ctx actor.Context) { 16 if msg, ok := ctx.Message().(*workItem); ok { 17 // this is guaranteed to only execute with a max concurrency level of `maxConcurrency` 18 log.Printf("%v got message %d", ctx.Self(), msg.i) 19 } 20 } 21 22 func main() { 23 rootContext := actor.EmptyRootContext 24 pid := rootContext.Spawn(router.NewRoundRobinPool(maxConcurrency).WithFunc(doWork)) 25 for i := 0; i < 1000; i++ { 26 rootContext.Send(pid, &workItem{i}) 27 } 28 console.ReadLine() 29 }