1 Star 0 Fork 0

李文建/protoactor-go

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
passivation.go 1.32 KB
一键复制 编辑 原始数据 按行查看 历史
yangdiangzb 提交于 2019-06-19 11:23 +08:00 . fix data race problems
package plugin
import (
"log"
"sync/atomic"
"time"
"github.com/AsynkronIT/protoactor-go/actor"
)
type PassivationAware interface {
Init(*actor.PID, time.Duration)
Reset(time.Duration)
Cancel()
}
type PassivationHolder struct {
timer *time.Timer
done int32
}
func (state *PassivationHolder) Reset(duration time.Duration) {
if state.timer == nil {
log.Fatalf("Cannot reset passivation of a non-started actor")
}
if atomic.LoadInt32(&state.done) == 0 {
state.timer.Reset(duration)
}
}
func (state *PassivationHolder) Init(pid *actor.PID, duration time.Duration) {
state.timer = time.NewTimer(duration)
state.done = 0
go func() {
select {
case <-state.timer.C:
actor.EmptyRootContext.Stop(pid)
atomic.StoreInt32(&state.done, 1)
break
}
}()
}
func (state *PassivationHolder) Cancel() {
if state.timer != nil {
state.timer.Stop()
}
}
type PassivationPlugin struct {
Duration time.Duration
}
func (pp *PassivationPlugin) OnStart(ctx actor.ReceiverContext) {
if a, ok := ctx.Actor().(PassivationAware); ok {
a.Init(ctx.Self(), pp.Duration)
}
}
func (pp *PassivationPlugin) OnOtherMessage(ctx actor.ReceiverContext, env *actor.MessageEnvelope) {
if p, ok := ctx.Actor().(PassivationAware); ok {
switch env.Message.(type) {
case *actor.Stopped:
p.Cancel()
default:
p.Reset(pp.Duration)
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/lwj8507/protoactor-go.git
git@gitee.com:lwj8507/protoactor-go.git
lwj8507
protoactor-go
protoactor-go
v0.0.1

搜索帮助