Major rewrite of the Go backend to support:
- Three API format imports: openai, anthropic, compatible
- Three protocol conversions: Chat Completions, Responses, Messages
- Hub-and-spoke architecture with Chat as intermediate format
Deleted:
- opencat.go (old entry)
- store/, team/, pkg/team/, pkg/store/ (old data layer)
- internal/model/, internal/consts/ (old types)
- internal/service/team/, internal/controller/team/ (old handlers)
- llm/ (removed LLM client library, pure proxy mode)
- dist/, assets/ (old build artifacts)
Added:
- internal/store/ — 9 GORM models + multi-DB support
- internal/pkg/ — crypto (AES-GCM), apikey, jwt, ratelimit, resp, tokenizer
- internal/channel/ — channel selection, weighted LB, health checks
- internal/proxy/convert/ — 6 protocol conversion functions + SSE streaming
- internal/proxy/ — gateway with request dispatch and upstream selection
- internal/usage/ — async usage recorder with batch writes
- internal/api/ — management API (auth, users, keys, channels, models)
- Makefile for build/test/deploy
Fixed API to match frontend expectations:
- Login response wraps token in { data: { token } }
- GET /api/profile route added
- Profile response wraps user in { code, data }
- Role returned as number (10=admin, 1=user)
123 lines
2.4 KiB
Go
123 lines
2.4 KiB
Go
package ratelimit
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// Limiter 内存限流器
|
|
type Limiter struct {
|
|
mu sync.Mutex
|
|
|
|
// 每用户每秒请求数
|
|
userRPS map[uint64]*tokenBucket
|
|
|
|
// 密钥每日请求计数
|
|
keyDailyReq map[uint64]*dailyCounter
|
|
|
|
// 密钥每日 token 计数
|
|
keyDailyTokens map[uint64]*dailyCounter
|
|
}
|
|
|
|
type tokenBucket struct {
|
|
tokens float64
|
|
maxTokens float64
|
|
refillRate float64
|
|
lastRefill time.Time
|
|
}
|
|
|
|
type dailyCounter struct {
|
|
date string
|
|
count int64
|
|
}
|
|
|
|
func New() *Limiter {
|
|
return &Limiter{
|
|
userRPS: make(map[uint64]*tokenBucket),
|
|
keyDailyReq: make(map[uint64]*dailyCounter),
|
|
keyDailyTokens: make(map[uint64]*dailyCounter),
|
|
}
|
|
}
|
|
|
|
// AllowRequest 检查用户级每秒请求限制
|
|
func (l *Limiter) AllowRequest(userID uint64, rps int) bool {
|
|
if rps <= 0 {
|
|
return true
|
|
}
|
|
l.mu.Lock()
|
|
defer l.mu.Unlock()
|
|
|
|
bucket, ok := l.userRPS[userID]
|
|
if !ok {
|
|
bucket = &tokenBucket{
|
|
tokens: float64(rps),
|
|
maxTokens: float64(rps),
|
|
refillRate: float64(rps),
|
|
lastRefill: time.Now(),
|
|
}
|
|
l.userRPS[userID] = bucket
|
|
}
|
|
|
|
now := time.Now()
|
|
elapsed := now.Sub(bucket.lastRefill).Seconds()
|
|
bucket.tokens += elapsed * bucket.refillRate
|
|
if bucket.tokens > bucket.maxTokens {
|
|
bucket.tokens = bucket.maxTokens
|
|
}
|
|
bucket.lastRefill = now
|
|
|
|
if bucket.tokens < 1 {
|
|
return false
|
|
}
|
|
bucket.tokens--
|
|
return true
|
|
}
|
|
|
|
// AllowRequestDaily 检查密钥每日请求配额
|
|
func (l *Limiter) AllowRequestDaily(keyID uint64, quota int) bool {
|
|
if quota <= 0 {
|
|
return true
|
|
}
|
|
l.mu.Lock()
|
|
defer l.mu.Unlock()
|
|
|
|
today := time.Now().UTC().Format("2006-01-02")
|
|
counter, ok := l.keyDailyReq[keyID]
|
|
if !ok || counter.date != today {
|
|
l.keyDailyReq[keyID] = &dailyCounter{date: today, count: 1}
|
|
return true
|
|
}
|
|
if counter.count >= int64(quota) {
|
|
return false
|
|
}
|
|
counter.count++
|
|
return true
|
|
}
|
|
|
|
// TokensUsed 返回密钥今日 token 用量
|
|
func (l *Limiter) TokensUsed(keyID uint64) int64 {
|
|
l.mu.Lock()
|
|
defer l.mu.Unlock()
|
|
|
|
today := time.Now().UTC().Format("2006-01-02")
|
|
counter, ok := l.keyDailyTokens[keyID]
|
|
if !ok || counter.date != today {
|
|
return 0
|
|
}
|
|
return counter.count
|
|
}
|
|
|
|
// AddTokens 累加密钥今日 token 用量
|
|
func (l *Limiter) AddTokens(keyID uint64, tokens int64) {
|
|
l.mu.Lock()
|
|
defer l.mu.Unlock()
|
|
|
|
today := time.Now().UTC().Format("2006-01-02")
|
|
counter, ok := l.keyDailyTokens[keyID]
|
|
if !ok || counter.date != today {
|
|
l.keyDailyTokens[keyID] = &dailyCounter{date: today, count: tokens}
|
|
return
|
|
}
|
|
counter.count += tokens
|
|
}
|