M3 收尾: 限流/配额 + 前端骨架屏 + 单端口托管前端
- ratelimit(内存计数): 密钥级每日请求数/Token 配额、用户级每秒速率 (OT_RATELIMIT_USER_RPS), 超限返回 429 - 网关 Auth 前置配额/限流检查, finishUsage 累计密钥 token 用量 - 前端 Skeleton 组件 + Dashboard/管理总览加载态 - Go 服务托管 web/dist 静态资源(SPA 回退), 单端口即可访问前后端 Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
// Package ratelimit 内存限流与配额(MVP 起步,Redis 后置)。
|
||||
// 覆盖:密钥级每日请求数 / 每日 token 数配额、用户级每秒速率。
|
||||
package ratelimit
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type dayCounter struct {
|
||||
date string
|
||||
n int64
|
||||
}
|
||||
|
||||
type hitWindow struct {
|
||||
times []time.Time
|
||||
limit int
|
||||
window time.Duration
|
||||
}
|
||||
|
||||
// Limiter 内存计数器。并发安全。
|
||||
type Limiter struct {
|
||||
mu sync.Mutex
|
||||
reqDaily map[uint64]*dayCounter // 密钥每日请求数
|
||||
tokDaily map[uint64]*dayCounter // 密钥每日 token 用量
|
||||
userHits map[uint64]*hitWindow // 用户速率窗口
|
||||
}
|
||||
|
||||
func New() *Limiter {
|
||||
return &Limiter{
|
||||
reqDaily: map[uint64]*dayCounter{},
|
||||
tokDaily: map[uint64]*dayCounter{},
|
||||
userHits: map[uint64]*hitWindow{},
|
||||
}
|
||||
}
|
||||
|
||||
func today() string { return time.Now().UTC().Format("2006-01-02") }
|
||||
|
||||
// AllowRequestDaily 检查并计数密钥每日请求配额;无配额(limit<=0)时仅计数。
|
||||
// 返回 false 表示超过配额。
|
||||
func (l *Limiter) AllowRequestDaily(keyID uint64, limit int) bool {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
d := today()
|
||||
c, ok := l.reqDaily[keyID]
|
||||
if !ok || c.date != d {
|
||||
c = &dayCounter{date: d}
|
||||
l.reqDaily[keyID] = c
|
||||
}
|
||||
c.n++
|
||||
if limit > 0 && c.n > int64(limit) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// AddTokens 累计密钥今日 token 用量(请求结束后记账)。
|
||||
func (l *Limiter) AddTokens(keyID uint64, n int64) {
|
||||
if n <= 0 {
|
||||
return
|
||||
}
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
d := today()
|
||||
c, ok := l.tokDaily[keyID]
|
||||
if !ok || c.date != d {
|
||||
c = &dayCounter{date: d}
|
||||
l.tokDaily[keyID] = c
|
||||
}
|
||||
c.n += n
|
||||
}
|
||||
|
||||
// TokensUsed 返回密钥今日已用 token。
|
||||
func (l *Limiter) TokensUsed(keyID uint64) int64 {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
c, ok := l.tokDaily[keyID]
|
||||
if !ok || c.date != today() {
|
||||
return 0
|
||||
}
|
||||
return c.n
|
||||
}
|
||||
|
||||
// AllowUserRate 用户级每秒请求速率限制(滑动窗口);limit<=0 不限制。
|
||||
func (l *Limiter) AllowUserRate(userID uint64, limit int) bool {
|
||||
if limit <= 0 {
|
||||
return true
|
||||
}
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
now := time.Now()
|
||||
win := time.Second
|
||||
w, ok := l.userHits[userID]
|
||||
if !ok || w.limit != limit || w.window != win {
|
||||
w = &hitWindow{limit: limit, window: win}
|
||||
l.userHits[userID] = w
|
||||
}
|
||||
// 清理窗口外的时间戳
|
||||
cutoff := now.Add(-win)
|
||||
keep := w.times[:0]
|
||||
for _, t := range w.times {
|
||||
if t.After(cutoff) {
|
||||
keep = append(keep, t)
|
||||
}
|
||||
}
|
||||
w.times = keep
|
||||
if len(w.times) >= limit {
|
||||
return false
|
||||
}
|
||||
w.times = append(w.times, now)
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package ratelimit
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestAllowRequestDaily(t *testing.T) {
|
||||
l := New()
|
||||
if !l.AllowRequestDaily(1, 2) {
|
||||
t.Fatal("first request should be allowed")
|
||||
}
|
||||
if !l.AllowRequestDaily(1, 2) {
|
||||
t.Fatal("second request should be allowed")
|
||||
}
|
||||
if l.AllowRequestDaily(1, 2) {
|
||||
t.Fatal("third request should be blocked")
|
||||
}
|
||||
// 另一个 key 不受影响
|
||||
if !l.AllowRequestDaily(2, 2) {
|
||||
t.Fatal("other key should be allowed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTokensDaily(t *testing.T) {
|
||||
l := New()
|
||||
l.AddTokens(1, 100)
|
||||
l.AddTokens(1, 50)
|
||||
if got := l.TokensUsed(1); got != 150 {
|
||||
t.Fatalf("tokens = %d, want 150", got)
|
||||
}
|
||||
if got := l.TokensUsed(2); got != 0 {
|
||||
t.Fatalf("other key tokens = %d, want 0", got)
|
||||
}
|
||||
// 配额检查语义:已达 150 再设配额 150 应拒绝
|
||||
l.AddTokens(3, 150)
|
||||
if l.TokensUsed(3) >= 150 {
|
||||
// 注意:>= 表示已达上限,Auth 层据此拒绝
|
||||
}
|
||||
}
|
||||
|
||||
func TestAllowUserRate(t *testing.T) {
|
||||
l := New()
|
||||
// limit=1:立即第二次应被拒绝
|
||||
if !l.AllowUserRate(9, 1) {
|
||||
t.Fatal("first should be allowed")
|
||||
}
|
||||
if l.AllowUserRate(9, 1) {
|
||||
t.Fatal("second in same second should be blocked")
|
||||
}
|
||||
// limit<=0 不限制
|
||||
if !l.AllowUserRate(9, 0) {
|
||||
t.Fatal("limit 0 should always allow")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user