Documentation
¶
Overview ¶
Package rules 限速规则:时段限速与突发限速。
规则只在主机判定(和配额、到期一样),命中时给目标用户写一条 LimitState,随快照下发副机; 主机与副机计算限速时把生效中的状态叠加到用户自己的限速上,再交给数据面的限速器。 用户自己的限速是常态,规则是临时接管:条件消失(走出时段 / 惩罚到期 / 规则停用)就退回常态。
Index ¶
- Constants
- func Active(states []model.LimitState, now int64) []model.LimitState
- func DaySet(days string) map[int]bool
- func Effective(ownUp, ownDown int, states []model.LimitState) (up, down int)
- func HumanBytes(n int64) string
- func InWindow(rule model.Rule, now time.Time, loc *time.Location) bool
- func LimitText(up, down int) string
- func ParseHM(s string) int
- func ParseIDs(raw json.RawMessage) []uint
- func Targets(rule model.Rule, users []model.User) []model.User
- type Engine
Constants ¶
View Source
const ( KindSchedule = "schedule" // 时段限速 KindBurst = "burst" // 突发限速 )
Variables ¶
This section is empty.
Functions ¶
func Active ¶
func Active(states []model.LimitState, now int64) []model.LimitState
Active 只留此刻仍生效的状态(时段状态 Until 为 0;突发状态到期即失效,副机据此在主机失联时也能自行放开)。
func Effective ¶
func Effective(ownUp, ownDown int, states []model.LimitState) (up, down int)
Effective 用户自己的限速叠加生效中的规则,返回最终上下行 Mbps(0 = 不限)。 每条状态贡献一个值:方向上填 0 的不参与;开了"只升不降"的贡献 min(规则值, 用户自己的值); 多条同时生效取最小(最严)。没有状态碰这个方向就是用户自己的值。
Types ¶
type Engine ¶
type Engine struct {
DB *gorm.DB
Location func() *time.Location // 面板时区(时段规则按它算);nil = 本机
Notify func(text string) // 突发触发时的通知;nil = 不通知
// contains filtered or unexported fields
}
Engine 主机上的规则判定器。每轮(统计任务之后,10 秒一次)调用 Tick:
- 时段规则:算此刻在不在窗口内,只在翻转的那一刻写入 / 删除状态;
- 突发规则:对目标用户采一次用量样本,滑动窗口内的增量达到阈值就写一条带到期时间的状态,到期删除;
- 规则停用 / 删除、用户停用 / 删除、不再是目标:它们的状态一并清掉。
状态表是唯一事实,窗口样本只在内存里(进程重启后窗口从空开始,跨过重启那一刻的突发可能漏判一次)。
Click to show internal directories.
Click to hide internal directories.