1 Star 0 Fork 0

jack/protoactor-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
strategy_exponential_backoff.go 1.21 KB
一键复制 编辑 原始数据 按行查看 历史
490689386@qq.com 提交于 2025-05-19 14:50 +08:00 . 初始化
package actor
import (
"math/rand"
"time"
)
// NewExponentialBackoffStrategy creates a new Supervisor strategy that restarts a faulting child using an exponential
// back off algorithm:
//
// delay =
func NewExponentialBackoffStrategy(backoffWindow time.Duration, initialBackoff time.Duration) SupervisorStrategy {
return &exponentialBackoffStrategy{
backoffWindow: backoffWindow,
initialBackoff: initialBackoff,
}
}
type exponentialBackoffStrategy struct {
backoffWindow time.Duration
initialBackoff time.Duration
}
var _ SupervisorStrategy = &exponentialBackoffStrategy{}
func (strategy *exponentialBackoffStrategy) HandleFailure(actorSystem *ActorSystem, supervisor Supervisor, child *PID, rs *RestartStatistics, reason interface{}, _ interface{}) {
strategy.setFailureCount(rs)
backoff := rs.FailureCount() * int(strategy.initialBackoff.Nanoseconds())
noise := rand.Intn(500)
dur := time.Duration(backoff + noise)
time.AfterFunc(dur, func() {
logFailure(actorSystem, child, reason, RestartDirective)
supervisor.RestartChildren(child)
})
}
func (strategy *exponentialBackoffStrategy) setFailureCount(rs *RestartStatistics) {
if rs.NumberOfFailures(strategy.backoffWindow) == 0 {
rs.Reset()
}
rs.Fail()
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wujianhai/protoactor-go.git
git@gitee.com:wujianhai/protoactor-go.git
wujianhai
protoactor-go
protoactor-go
5633fe2499dd

搜索帮助