1 Star 0 Fork 0

jack/protoactor-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
throttler.go 2.31 KB
一键复制 编辑 原始数据 按行查看 历史
490689386@qq.com 提交于 2025-05-19 14:50 +08:00 . 初始化
package actor
import (
"log/slog"
"sync/atomic"
"time"
)
type ShouldThrottle func() Valve
type Valve int32
const (
Open Valve = iota
Closing
Closed
)
// NewThrottle
// This has no guarantees that the throttle opens exactly after the period, since it is reset asynchronously
// Throughput has been prioritized over exact re-opening
// throttledCallBack, This will be called with the number of events what was throttled after the period
func NewThrottle(maxEventsInPeriod int32, period time.Duration, throttledCallBack func(int32)) ShouldThrottle {
currentEvents := int32(0)
startTimer := func(duration time.Duration) {
go func() {
// crete ticker to mimic sleep, we do not want to put the goroutine to sleep
// as it will schedule it out of the P making a syscall, we just want it to
// halt for the given period of time
ticker := time.NewTicker(duration)
defer ticker.Stop()
<-ticker.C // wait for the ticker to tick once
timesCalled := atomic.SwapInt32(&currentEvents, 0)
if timesCalled > maxEventsInPeriod {
throttledCallBack(timesCalled - maxEventsInPeriod)
}
}()
}
return func() Valve {
tries := atomic.AddInt32(&currentEvents, 1)
if tries == 1 {
startTimer(period)
}
if tries == maxEventsInPeriod {
return Closing
} else if tries > maxEventsInPeriod {
return Closed
} else {
return Open
}
}
}
func NewThrottleWithLogger(logger *slog.Logger, maxEventsInPeriod int32, period time.Duration, throttledCallBack func(*slog.Logger, int32)) ShouldThrottle {
currentEvents := int32(0)
startTimer := func(duration time.Duration) {
go func() {
// crete ticker to mimic sleep, we do not want to put the goroutine to sleep
// as it will schedule it out of the P making a syscall, we just want it to
// halt for the given period of time
ticker := time.NewTicker(duration)
defer ticker.Stop()
<-ticker.C // wait for the ticker to tick once
timesCalled := atomic.SwapInt32(&currentEvents, 0)
if timesCalled > maxEventsInPeriod {
throttledCallBack(logger, timesCalled-maxEventsInPeriod)
}
}()
}
return func() Valve {
tries := atomic.AddInt32(&currentEvents, 1)
if tries == 1 {
startTimer(period)
}
if tries == maxEventsInPeriod {
return Closing
} else if tries > maxEventsInPeriod {
return Closed
} else {
return Open
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wujianhai/protoactor-go.git
git@gitee.com:wujianhai/protoactor-go.git
wujianhai
protoactor-go
protoactor-go
5633fe2499dd

搜索帮助