Documentation
¶
Index ¶
- Constants
- Variables
- func BytesToString(b []byte) string
- func EndOfMonth(date time.Time) time.Time
- func EndOfQuarter(date time.Time) time.Time
- func EndOfWeek(date time.Time) time.Time
- func EndOfYear(date time.Time) time.Time
- func FormatDuration(duration time.Duration) string
- func FormatRecover() ([]byte, any)
- func FullStack(w error) string
- func Hash64FNV1A[T string | []byte](data T) uint64
- func Hash64WY[T string | []byte](data T, seed uint64) uint64
- func Log2Up(x uint32) int
- func MetaDictEncode(m MetaDict[string]) []byte
- func MetaDictEncoder(m MetaDict[string], w io.Writer) error
- func ParseID(id int64) int64
- func PrivateIPv4() ([]string, error)
- func RemoveDuplicates[T cmp.Ordered](s []T) []T
- func Stack(err error) string
- func StartOfMonth(date time.Time) time.Time
- func StartOfQuarter(date time.Time) time.Time
- func StartOfWeek(date time.Time) time.Time
- func StartOfYear(date time.Time) time.Time
- func StringToBytes(s string) (b []byte)
- func Three[T any](boolExpression bool, trueReturnValue, falseReturnValue T) T
- func UniqueWithoutSort[T comparable](s []T) []T
- func WithRetry(operation func() error, baseDelay time.Duration, maxRetries int) error
- func WrapStack(skip int, err error) error
- type Cell
- type CopyOnWriteList
- type IdempotentCache
- type MetaDict
- func (m MetaDict[MetaDictValue]) Del(key string) MetaDict[MetaDictValue]
- func (m MetaDict[MetaDictValue]) Get(key string) (v MetaDictValue, ok bool)
- func (m MetaDict[MetaDictValue]) GetAll() (s []MetaDictValue)
- func (m MetaDict[MetaDictValue]) Len() int
- func (m MetaDict[MetaDictValue]) Set(key string, value MetaDictValue) MetaDict[MetaDictValue]
- type MetaDictValue
- type Pool
- type RollingWindow
- type SnowFlakeID
- type Timing
- type TokenBucketLimiter
- type Universe
- func (u *Universe) Close()
- func (u *Universe) Get(n int) any
- func (u *Universe) GetInt(n int) int
- func (u *Universe) GetString(n int) string
- func (u *Universe) NewComputer(effect func(any), warp func(*Universe) (func() any, []int)) int
- func (u *Universe) NewSignal(v any, effect func(any)) int
- func (u *Universe) Operate(n int, do func(any))
- func (u *Universe) Run()
- func (u *Universe) SetSignal(n int, a any)
Constants ¶
const ( // Bits allocation (标准Snowflake结构) TimestampBits = 41 WorkIDBits = 10 SequenceBits = 12 // Max values MaxWorkID = 1<<WorkIDBits - 1 // 1023 (10 bits) MaxSequence = 1<<SequenceBits - 1 // 4095 (12 bits), 实际使用4000避免边界问题 // Bit shifts WorkIDShift = SequenceBits // 12 TimestampShift = SequenceBits + WorkIDBits // 22 )
const ( StatusUnknown = iota + 10 StatusNil StatusPing StatusPong StatusGoaway StatusRequest StatusResponse StatusNotify StatusCtxCancelFunc StatusSubscribe StatusUnsubscribe StatusBroadcast StatusOnSuccess StatusOnError StatusError )
自定义协议状态码(10-99)
const ( StatusUnknown16 uint16 = iota + 10 StatusNil16 StatusPing16 StatusPong16 StatusGoaway16 StatusRequest16 StatusResponse16 StatusNotify16 StatusCtxCancelFunc16 StatusSubscribe16 StatusUnsubscribe16 StatusBroadcast16 StatusOnSuccess16 StatusOnError16 StatusError16 )
自定义协议状态码(10-99)
const ( StatusContinue = 100 // RFC 7231, 6.2.1 StatusSwitchingProtocols = 101 // RFC 7231, 6.2.2 StatusProcessing = 102 // RFC 2518, 10.1 StatusEarlyHints = 103 // RFC 8297 StatusOK = 200 // RFC 7231, 6.3.1 StatusCreated = 201 // RFC 7231, 6.3.2 StatusAccepted = 202 // RFC 7231, 6.3.3 StatusNonAuthoritativeInfo = 203 // RFC 7231, 6.3.4 StatusNoContent = 204 // RFC 7231, 6.3.5 StatusResetContent = 205 // RFC 7231, 6.3.6 StatusMultiStatus = 207 // RFC 4918, 11.1 StatusAlreadyReported = 208 // RFC 5842, 7.1 StatusIMUsed = 226 // RFC 3229, 10.4.1 StatusMultipleChoices = 300 // RFC 7231, 6.4.1 StatusMovedPermanently = 301 // RFC 7231, 6.4.2 StatusFound = 302 // RFC 7231, 6.4.3 StatusSeeOther = 303 // RFC 7231, 6.4.4 StatusNotModified = 304 // RFC 7232, 4.1 StatusUseProxy = 305 // RFC 7231, 6.4.5 StatusTemporaryRedirect = 307 // RFC 7231, 6.4.7 StatusPermanentRedirect = 308 // RFC 7538, 3 StatusBadRequest = 400 // RFC 7231, 6.5.1 StatusPaymentRequired = 402 // RFC 7231, 6.5.2 StatusForbidden = 403 // RFC 7231, 6.5.3 StatusNotFound = 404 // RFC 7231, 6.5.4 StatusMethodNotAllowed = 405 // RFC 7231, 6.5.5 StatusNotAcceptable = 406 // RFC 7231, 6.5.6 StatusProxyAuthRequired = 407 // RFC 7235, 3.2 StatusRequestTimeout = 408 // RFC 7231, 6.5.7 StatusConflict = 409 // RFC 7231, 6.5.8 StatusGone = 410 // RFC 7231, 6.5.9 StatusLengthRequired = 411 // RFC 7231, 6.5.10 StatusPreconditionFailed = 412 // RFC 7232, 4.2 StatusRequestEntityTooLarge = 413 // RFC 7231, 6.5.11 StatusRequestURITooLong = 414 // RFC 7231, 6.5.12 StatusUnsupportedMediaType = 415 // RFC 7231, 6.5.13 StatusRequestedRangeNotSatisfiable = 416 // RFC 7233, 4.4 StatusExpectationFailed = 417 // RFC 7231, 6.5.14 StatusTeapot = 418 // RFC 7168, 2.3.3 StatusMisdirectedRequest = 421 // RFC 7540, 9.1.2 StatusUnprocessableEntity = 422 // RFC 4918, 11.2 StatusLocked = 423 // RFC 4918, 11.3 StatusFailedDependency = 424 // RFC 4918, 11.4 StatusUpgradeRequired = 426 // RFC 7231, 6.5.15 StatusPreconditionRequired = 428 // RFC 6585, 3 StatusTooManyRequests = 429 // RFC 6585, 4 StatusRequestHeaderFieldsTooLarge = 431 // RFC 6585, 5 StatusInternalServerError = 500 // RFC 7231, 6.6.1 StatusNotImplemented = 501 // RFC 7231, 6.6.2 StatusBadGateway = 502 // RFC 7231, 6.6.3 StatusGatewayTimeout = 504 // RFC 7231, 6.6.5 StatusHTTPVersionNotSupported = 505 // RFC 7231, 6.6.6 StatusVariantAlsoNegotiates = 506 // RFC 2295, 8.1 StatusInsufficientStorage = 507 // RFC 4918, 11.5 StatusLoopDetected = 508 // RFC 5842, 7.2 StatusNotExtended = 510 // RFC 2774, 7 StatusNetworkAuthenticationRequired = 511 // RFC 6585, 6 )
HTTP状态码,参见RFC 2616
const ( StatusContinue16 uint16 = 100 StatusSwitchingProtocols16 uint16 = 101 StatusProcessing16 uint16 = 102 StatusEarlyHints16 uint16 = 103 StatusOK16 uint16 = 200 StatusCreated16 uint16 = 201 StatusAccepted16 uint16 = 202 StatusNonAuthoritativeInfo16 uint16 = 203 StatusNoContent16 uint16 = 204 StatusResetContent16 uint16 = 205 StatusPartialContent16 uint16 = 206 StatusMultiStatus16 uint16 = 207 StatusAlreadyReported16 uint16 = 208 StatusIMUsed16 uint16 = 226 StatusMultipleChoices16 uint16 = 300 StatusMovedPermanently16 uint16 = 301 StatusFound16 uint16 = 302 StatusSeeOther16 uint16 = 303 StatusNotModified16 uint16 = 304 StatusUseProxy16 uint16 = 305 StatusTemporaryRedirect16 uint16 = 307 StatusPermanentRedirect16 uint16 = 308 StatusBadRequest16 uint16 = 400 StatusPaymentRequired16 uint16 = 402 StatusForbidden16 uint16 = 403 StatusNotFound16 uint16 = 404 StatusMethodNotAllowed16 uint16 = 405 StatusNotAcceptable16 uint16 = 406 StatusProxyAuthRequired16 uint16 = 407 StatusRequestTimeout16 uint16 = 408 StatusConflict16 uint16 = 409 StatusGone16 uint16 = 410 StatusLengthRequired16 uint16 = 411 StatusPreconditionFailed16 uint16 = 412 StatusRequestEntityTooLarge16 uint16 = 413 StatusRequestURITooLong16 uint16 = 414 StatusUnsupportedMediaType16 uint16 = 415 StatusRequestedRangeNotSatisfiable16 uint16 = 416 StatusExpectationFailed16 uint16 = 417 StatusTeapot16 uint16 = 418 StatusMisdirectedRequest16 uint16 = 421 StatusUnprocessableEntity16 uint16 = 422 StatusLocked16 uint16 = 423 StatusFailedDependency16 uint16 = 424 StatusUpgradeRequired16 uint16 = 426 StatusPreconditionRequired16 uint16 = 428 StatusTooManyRequests16 uint16 = 429 StatusRequestHeaderFieldsTooLarge16 uint16 = 431 StatusInternalServerError16 uint16 = 500 StatusNotImplemented16 uint16 = 501 StatusBadGateway16 uint16 = 502 StatusGatewayTimeout16 uint16 = 504 StatusHTTPVersionNotSupported16 uint16 = 505 StatusVariantAlsoNegotiates16 uint16 = 506 StatusInsufficientStorage16 uint16 = 507 StatusLoopDetected16 uint16 = 508 StatusNotExtended16 uint16 = 510 StatusNetworkAuthenticationRequired16 uint16 = 511 )
HTTP状态码
Variables ¶
var ErrMachineTimeUnSynchronize = errors.New("机器时钟后跳, timeGen()生成时间戳, 早于SnowFlakeId记录的时间戳")
ErrMachineTimeUnSynchronize 定义错误
Functions ¶
func FormatDuration ¶
FormatDuration 将持续时间格式化字符串 (e.g., "3天 05小时 12分 45秒").
func FormatRecover ¶
func PrivateIPv4 ¶
PrivateIPv4 返回所有本地私有IPv4地址(按RFC 1918和RFC 4193标准)。 如果找不到私有IP,返回空切片和错误信息。
Types ¶
type CopyOnWriteList ¶
type CopyOnWriteList[T any] struct { // contains filtered or unexported fields }
CopyOnWriteList (COW)需要修改的时候拷贝一个副本出来,适用不频繁写的场景 修改时新数据原子替换旧数据地址,旧数据由GC回收。
func NewCopyOnWriteList ¶
func NewCopyOnWriteList[T any]() *CopyOnWriteList[T]
NewCopyOnWriteList 新增
func (*CopyOnWriteList[T]) Remove ¶
func (l *CopyOnWriteList[T]) Remove(predicate func(T) bool)
Remove 移除
type IdempotentCache ¶
IdempotentCache 幂等函数缓存,幂等方法,是指可以使用相同参数重复执行,并能获得相同结果的函数
func (*IdempotentCache[T]) Init ¶
func (ic *IdempotentCache[T]) Init(power, seed uint64, do func(T) any)
Init 初始化 power表示缓存最大数量的的2的次方,seed表示hash的种子,do表示要缓存的幂等函数
type MetaDict ¶
type MetaDict[T MetaDictValue] struct { Key []uint64 Value []T }
MetaDict 非线程安全,key数量超过5个后,效率低于map
type MetaDictValue ¶
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
Pool
func (*Pool) AllocBuffer ¶
func (*Pool) AllocSlice ¶
func (*Pool) FreeBuffer ¶
type RollingWindow ¶
type RollingWindow struct {
// contains filtered or unexported fields
}
RollingWindow 环形滑动窗口统计
func NewRollingWindow ¶
func NewRollingWindow(totalPow, interval, bucketleghthPow int) *RollingWindow
NewRollingWindow
func (*RollingWindow) Sampling ¶
func (r *RollingWindow) Sampling() []int
type SnowFlakeID ¶
type SnowFlakeID struct {
// contains filtered or unexported fields
}
SnowFlakeID 工作站
func NewSnowFlakeID ¶
func NewSnowFlakeID(workerID int64, startupTime int64) *SnowFlakeID
NewSnowFlakeID 工作组
type Timing ¶
type Timing struct {
AddTaskChan chan task
// contains filtered or unexported fields
}
type TokenBucketLimiter ¶
type TokenBucketLimiter struct {
//限流器速率,每秒处理的令牌数
LimitRate int64
//限流器大小,存放令牌的最大值
Capacity int64
//加入的时间间隔
Snippet time.Duration
// contains filtered or unexported fields
}
Limiter 限流器 Token Bucket(令牌桶) 每隔一段时间加入一批令牌,达到上限后,不再增加。
func NewTokenBucketLimiter ¶
func NewTokenBucketLimiter(limitRate, size int64, snippet time.Duration) *TokenBucketLimiter
NewTokenBucketLimiter limitRate, limitSize,snippet数值较小时,准确度低。
func (*TokenBucketLimiter) Run ¶
func (t *TokenBucketLimiter) Run()
func (*TokenBucketLimiter) Take ¶
func (t *TokenBucketLimiter) Take(n int64) bool
Take 等待,申请n个令牌,取不到足够数量时返回失败。
func (*TokenBucketLimiter) Task ¶
func (t *TokenBucketLimiter) Task() time.Duration
Task 用于定时模块 Timing
type Universe ¶
type Universe struct {
// contains filtered or unexported fields
}
func NewUniverse ¶
func NewUniverse() *Universe
func (*Universe) NewComputer ¶
NewComputer 线程不安全