代码拉取完成,页面将自动刷新
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)
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。