rules

package
v0.7.16 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

Package rules 限速规则:时段限速与突发限速。

规则只在主机判定(和配额、到期一样),命中时给目标用户写一条 LimitState,随快照下发副机; 主机与副机计算限速时把生效中的状态叠加到用户自己的限速上,再交给数据面的限速器。 用户自己的限速是常态,规则是临时接管:条件消失(走出时段 / 惩罚到期 / 规则停用)就退回常态。

Index

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 DaySet

func DaySet(days string) map[int]bool

DaySet "1,2,5" → {1,2,5};空 = 每天(返回空集合,调用方按"不限"处理)。

func Effective

func Effective(ownUp, ownDown int, states []model.LimitState) (up, down int)

Effective 用户自己的限速叠加生效中的规则,返回最终上下行 Mbps(0 = 不限)。 每条状态贡献一个值:方向上填 0 的不参与;开了"只升不降"的贡献 min(规则值, 用户自己的值); 多条同时生效取最小(最严)。没有状态碰这个方向就是用户自己的值。

func HumanBytes

func HumanBytes(n int64) string

HumanBytes 1.2 GB / 300 MB 这样的可读流量。

func InWindow

func InWindow(rule model.Rule, now time.Time, loc *time.Location) bool

InWindow 时段规则此刻是否在窗口内(loc = 面板时区)。 结束早于开始 = 跨午夜,这段按开始那天的星期算:周五 23:00 到 02:00,周六 01:00 也属于周五那段。

func LimitText

func LimitText(up, down int) string

LimitText "上行 20 / 下行 20 Mbps";填 0 的方向写"不改"。

func ParseHM

func ParseHM(s string) int

ParseHM "HH:MM" → 当天第几分钟;解析不了返回 -1。

func ParseIDs

func ParseIDs(raw json.RawMessage) []uint

ParseIDs 解出 json 数组里的 id;解析不了当空。

func Targets

func Targets(rule model.Rule, users []model.User) []model.User

Targets 此刻被规则覆盖的用户:全部 / 指定用户 / 指定代理名下的全部用户 / 指定套餐的用户(后两种是动态的,以后才归入的也算)。 停用的用户不参与:他连不上,限不限无意义,状态也不该留着。

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:

  • 时段规则:算此刻在不在窗口内,只在翻转的那一刻写入 / 删除状态;
  • 突发规则:对目标用户采一次用量样本,滑动窗口内的增量达到阈值就写一条带到期时间的状态,到期删除;
  • 规则停用 / 删除、用户停用 / 删除、不再是目标:它们的状态一并清掉。

状态表是唯一事实,窗口样本只在内存里(进程重启后窗口从空开始,跨过重启那一刻的突发可能漏判一次)。

func (*Engine) Tick

func (e *Engine) Tick(now time.Time) (bool, error)

Tick 判一轮;返回状态表有没有变化(变了调用方要重新下发限速,快照也会随之推给副机)。

Jump to

Keyboard shortcuts

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