counter

package
v1.59.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package counter 提供高并发在线计数:直播间人数/峰值/时段统计。 内存计数(Inc/Dec 高频安全)+ 峰值追踪;按房间注册表。 场景:直播在线人数、房间热度、峰值记录;持久化由业务定时快照(如每 30s 落库)。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Counter

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

Counter 单实例计数器(线程安全)。

func New

func New() *Counter

New 创建计数器。

func (*Counter) Dec

func (c *Counter) Dec() int64

Dec 自减(下限 0),返回当前值。

func (*Counter) Inc

func (c *Counter) Inc() int64

Inc 自增,返回当前值。

func (*Counter) Peak

func (c *Counter) Peak() int64

Peak 返回历史峰值。

func (*Counter) ResetPeak

func (c *Counter) ResetPeak() int64

ResetPeak 重置峰值(新的统计周期),返回重置前的峰值。

func (*Counter) Snapshot

func (c *Counter) Snapshot() (value, peak int64)

Snapshot 快照(当前值 + 峰值)。

func (*Counter) Value

func (c *Counter) Value() int64

Value 返回当前值。

type Registry

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

Registry 按房间(键)管理的计数器注册表。 典型用法:直播间在线人数,key = 房间号。

func NewRegistry

func NewRegistry() *Registry

NewRegistry 创建注册表。

func (*Registry) Dec

func (r *Registry) Dec(key string) int64

Dec 自减指定键的计数器,返回当前值。

func (*Registry) Inc

func (r *Registry) Inc(key string) int64

Inc 自增指定键的计数器,返回当前值。

func (*Registry) Keys

func (r *Registry) Keys() []string

Keys 返回全部已注册键。

func (*Registry) Peak

func (r *Registry) Peak(key string) int64

Peak 返回指定键的峰值(不存在为 0)。

func (*Registry) Remove

func (r *Registry) Remove(key string)

Remove 移除指定键的计数器。

func (*Registry) Value

func (r *Registry) Value(key string) int64

Value 返回指定键的当前值(不存在为 0)。

Jump to

Keyboard shortcuts

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