Documentation
¶
Overview ¶
Package kit Reference: https://github.com/puzpuzpuz/xsync/blob/main/counter.go A Counter is a striped int64 counter inspired by the j.u.c.a.LongAdder class from the Java standard library. Works better in comparison with a single atomically updated int64 counter in high contention scenarios.
Package kit Reference: https://github.com/puzpuzpuz/xsync/blob/main/util.go
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CalcThreshold ¶ added in v0.10.6
CalcThreshold 计算阈值, 始终向上取整且最小为: 1
func NextPowOf2 ¶ added in v0.10.5
NextPowOf2 computes the next highest power of 2 of 32-bit v. Source: https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
func Parallelism ¶ added in v0.10.5
func Parallelism() uint32
Types ¶
type Counter ¶ added in v0.10.5
type Counter struct {
// contains filtered or unexported fields
}
Counter is a striped int64 counter that supports negative values.
Should be preferred over a single atomically updated int64 counter in high contention scenarios.
A Counter must not be copied after first use.
func NewCounter ¶ added in v0.10.5
func NewCounter() *Counter
NewCounter creates a new Counter instance.
func (*Counter) Load ¶ added in v0.10.5
Load returns the current counter value. It is equivalent to Value(), added for API consistency with atomic types.
func (*Counter) Reset ¶ added in v0.10.5
func (c *Counter) Reset()
Reset resets the counter to zero. This method should only be used when it is known that there are no concurrent modifications of the counter.
type RateState ¶
type RateState struct {
// contains filtered or unexported fields
}
RateState 通过计数增长和时间间隔计算速率
func NewRateState ¶
NewRateState 创建速率计算器, 可选设置最小保护时间间隔, 默认 1 秒
func (*RateState) Rate ¶
Rate 计算并返回速率(每秒), 返回最近两次触达计算的请求时间间隔之间的计数增长平均速率 非实时计算, 可能返回上一轮速率结果 非精确计算, 当计数增长突发性很大或请求的时间间隔很长时, 速率结果与实际误差较大 若需要得到相对精准的结果, 按指定时间间隔调用 Rate 函数 -1 表示计数器被重置或无效, 0 表示无变化
func (*RateState) RateWithLastCount ¶ added in v0.10.3
RateWithLastCount 获取速率和上次计数
func (*RateState) SetMinSecond ¶ added in v0.10.2
SetMinSecond 设置最小时间间隔 (秒)
type UCounter ¶ added in v0.10.5
type UCounter struct {
// contains filtered or unexported fields
}
UCounter is a striped uint64 counter that only supports non-negative values.
Should be preferred over a single atomically updated uint64 counter in high contention scenarios.
A UCounter must not be copied after first use.
func NewUCounter ¶ added in v0.10.5
func NewUCounter() *UCounter
NewUCounter creates a new UCounter instance.
func (*UCounter) Add ¶ added in v0.10.5
Add adds the delta to the counter. It panics if delta is negative.
func (*UCounter) Load ¶ added in v0.10.5
Load returns the current counter value. It is equivalent to Value(), added for API consistency with atomic types.
func (*UCounter) Reset ¶ added in v0.10.5
func (c *UCounter) Reset()
Reset resets the counter to zero. This method should only be used when it is known that there are no concurrent modifications of the counter.