代码拉取完成,页面将自动刷新
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(¤tEvents, 0)
if timesCalled > maxEventsInPeriod {
throttledCallBack(timesCalled - maxEventsInPeriod)
}
}()
}
return func() Valve {
tries := atomic.AddInt32(¤tEvents, 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(¤tEvents, 0)
if timesCalled > maxEventsInPeriod {
throttledCallBack(logger, timesCalled-maxEventsInPeriod)
}
}()
}
return func() Valve {
tries := atomic.AddInt32(¤tEvents, 1)
if tries == 1 {
startTimer(period)
}
if tries == maxEventsInPeriod {
return Closing
} else if tries > maxEventsInPeriod {
return Closed
} else {
return Open
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。