1 Star 0 Fork 0

jack/protoactor-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
random_router.go 1.38 KB
一键复制 编辑 原始数据 按行查看 历史
jack 提交于 2024-11-27 09:43 . 新建自定义分支
package router
import (
"math/rand"
"gitee.com/wujianhai/protoactor-go/actor"
)
type randomGroupRouter struct {
GroupRouter
}
type randomPoolRouter struct {
PoolRouter
}
type randomRouterState struct {
routees *actor.PIDSet
sender actor.SenderContext
}
func (state *randomRouterState) SetSender(sender actor.SenderContext) {
state.sender = sender
}
func (state *randomRouterState) SetRoutees(routees *actor.PIDSet) {
state.routees = routees
}
func (state *randomRouterState) GetRoutees() *actor.PIDSet {
return state.routees
}
func (state *randomRouterState) RouteMessage(message interface{}) {
pid := randomRoutee(state.routees)
state.sender.Send(pid, message)
}
func NewRandomPool(size int, opts ...actor.PropsOption) *actor.Props {
return (&actor.Props{}).
Configure(actor.WithSpawnFunc(spawner(&randomPoolRouter{PoolRouter{PoolSize: size}}))).
Configure(opts...)
}
func NewRandomGroup(routees ...*actor.PID) *actor.Props {
return (&actor.Props{}).Configure(actor.WithSpawnFunc(spawner(&randomGroupRouter{GroupRouter{Routees: actor.NewPIDSet(routees...)}})))
}
func (config *randomPoolRouter) CreateRouterState() State {
return &randomRouterState{}
}
func (config *randomGroupRouter) CreateRouterState() State {
return &randomRouterState{}
}
func randomRoutee(routees *actor.PIDSet) *actor.PID {
l := routees.Len()
r := rand.Intn(l)
pid := routees.Get(r)
return pid
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wujianhai/protoactor-go.git
git@gitee.com:wujianhai/protoactor-go.git
wujianhai
protoactor-go
protoactor-go
99a34e4ec9eb

搜索帮助