Build way-node / Test, Build, Push (push) Failing after 14s
- Go module: github.com/cedar2025/xboard-node → github.com/viaeon/way-node(307 处 import 同步) - cmd/xboard-node/ → cmd/way-node/ - install.sh: APP_NAME / INSTALL_ROOT(/etc/way-node) / BINARY_PATH / SERVICE_NAME 全部更名, 下载源与 Documentaion 指向 git.viaeon.com/admin/way-node - Makefile / Dockerfile 产物与镜像路径更名 - README 全篇重写链接 - 新增 .gitea/workflows/build.yml:容器内跑测试 + 交叉编译 + 构建镜像推 git.viaeon.com/admin/way-node 构建全程在 golang:1.26 容器里,不依赖 runner 宿主装 Go - 禁用 .github/workflows/ci.yml(依赖 ghcr.io 与 GITHUB_TOKEN,Gitea 上跑不通) 保留:xbctl 工具名(不含旧品牌)、go.sum 里上游依赖哈希
85 lines
2.1 KiB
Go
85 lines
2.1 KiB
Go
package limiter
|
|
|
|
import (
|
|
"sync"
|
|
"sync/atomic"
|
|
|
|
"github.com/viaeon/way-node/internal/model"
|
|
)
|
|
|
|
// Limiter enforces per-user device limits and detects removed users.
|
|
type Limiter struct {
|
|
mu sync.RWMutex
|
|
users map[int]model.UserSpec
|
|
|
|
// uuidDeviceLimit provides O(1) UUID→device-limit lookup for the
|
|
// kernel gate-keeping hot path (called per new connection).
|
|
uuidDeviceLimit map[string]int
|
|
|
|
deviceLimitEvents atomic.Uint64
|
|
}
|
|
|
|
func New() *Limiter {
|
|
return &Limiter{
|
|
users: make(map[int]model.UserSpec),
|
|
uuidDeviceLimit: make(map[string]int),
|
|
}
|
|
}
|
|
|
|
// UpdateUsers refreshes the user limit configuration and returns
|
|
// the IDs of users that were present before but are now removed.
|
|
func (l *Limiter) UpdateUsers(users []model.UserSpec) []int {
|
|
l.mu.Lock()
|
|
defer l.mu.Unlock()
|
|
|
|
newUsers := make(map[int]model.UserSpec, len(users))
|
|
for _, u := range users {
|
|
newUsers[u.ID] = u
|
|
}
|
|
|
|
// Find removed users
|
|
var removed []int
|
|
for id := range l.users {
|
|
if _, ok := newUsers[id]; !ok {
|
|
removed = append(removed, id)
|
|
}
|
|
}
|
|
|
|
l.users = newUsers
|
|
|
|
// Rebuild UUID→device-limit index for O(1) lookups.
|
|
idx := make(map[string]int, len(newUsers))
|
|
for _, u := range newUsers {
|
|
if u.DeviceLimit > 0 {
|
|
idx[u.UUID] = u.DeviceLimit
|
|
}
|
|
}
|
|
l.uuidDeviceLimit = idx
|
|
|
|
return removed
|
|
}
|
|
|
|
// LimiterMetrics holds aggregated limiter statistics.
|
|
type LimiterMetrics struct {
|
|
// DeviceLimitEvents is the total number of times a user exceeded
|
|
// the configured device limit (since process start).
|
|
DeviceLimitEvents uint64
|
|
}
|
|
|
|
// SnapshotMetrics returns a snapshot of limiter metrics.
|
|
func (l *Limiter) SnapshotMetrics() LimiterMetrics {
|
|
return LimiterMetrics{
|
|
DeviceLimitEvents: l.deviceLimitEvents.Load(),
|
|
}
|
|
}
|
|
|
|
// GetDeviceLimitByUUID returns the device limit for a user identified by UUID.
|
|
// Returns (limit, true) if a device limit is set, (0, false) otherwise.
|
|
// This is designed to be passed as a function reference to kernel.SetDeviceLimitFunc.
|
|
func (l *Limiter) GetDeviceLimitByUUID(uuid string) (int, bool) {
|
|
l.mu.RLock()
|
|
defer l.mu.RUnlock()
|
|
limit, ok := l.uuidDeviceLimit[uuid]
|
|
return limit, ok
|
|
}
|