kit

package
v0.10.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2025 License: MIT, Apache-2.0 Imports: 6 Imported by: 0

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 Cheaprand added in v0.10.5

func Cheaprand() uint32

func NextPowOf2 added in v0.10.5

func NextPowOf2(v uint32) uint32

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) Add added in v0.10.5

func (c *Counter) Add(delta int64)

Add adds the delta to the counter.

func (*Counter) Dec added in v0.10.5

func (c *Counter) Dec()

Dec decrements the counter by 1.

func (*Counter) Inc added in v0.10.5

func (c *Counter) Inc()

Inc increments the counter by 1.

func (*Counter) Load added in v0.10.5

func (c *Counter) Load() int64

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.

func (*Counter) Store added in v0.10.5

func (c *Counter) Store(val int64)

Store sets the counter value to the given newValue. This operation resets all stripes and stores the entire value in the first stripe. Note: This operation is not atomic and should be used with care in concurrent environments.

func (*Counter) Value added in v0.10.5

func (c *Counter) Value() int64

Value returns the current counter value. The returned value may not include all the latest operations in presence of concurrent modifications of the counter.

type RateState

type RateState struct {
	// contains filtered or unexported fields
}

RateState 通过计数增长和时间间隔计算速率

func NewRateState

func NewRateState(seconds ...float64) *RateState

NewRateState 创建速率计算器, 可选设置最小保护时间间隔, 默认 1 秒

func (*RateState) Rate

func (r *RateState) Rate(count uint64) float64

Rate 计算并返回速率(每秒), 返回最近两次触达计算的请求时间间隔之间的计数增长平均速率 非实时计算, 可能返回上一轮速率结果 非精确计算, 当计数增长突发性很大或请求的时间间隔很长时, 速率结果与实际误差较大 若需要得到相对精准的结果, 按指定时间间隔调用 Rate 函数 -1 表示计数器被重置或无效, 0 表示无变化

func (*RateState) RateWithLastCount added in v0.10.3

func (r *RateState) RateWithLastCount(count uint64) (float64, uint64)

RateWithLastCount 获取速率和上次计数

func (*RateState) SetMinSecond added in v0.10.2

func (r *RateState) SetMinSecond(second float64)

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

func (c *UCounter) Add(delta uint64)

Add adds the delta to the counter. It panics if delta is negative.

func (*UCounter) Inc added in v0.10.5

func (c *UCounter) Inc()

Inc increments the counter by 1.

func (*UCounter) Load added in v0.10.5

func (c *UCounter) Load() uint64

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.

func (*UCounter) Store added in v0.10.5

func (c *UCounter) Store(val uint64)

Store sets the counter value to the given newValue. This operation resets all stripes and stores the entire value in the first stripe. Note: This operation is not atomic and should be used with care in concurrent environments.

func (*UCounter) Value added in v0.10.5

func (c *UCounter) Value() uint64

Value returns the current counter value. The returned value may not include all the latest operations in presence of concurrent modifications of the counter.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL