perf(files): 优化私有文件鉴权与高成本查询隔离
This commit is contained in:
@@ -0,0 +1,197 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
"hfb_sys/backend/pkg/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const defaultAPIQueryBudget = 15 * time.Second
|
||||
|
||||
// QueryBudget 为普通 API 设置请求级数据库上下文预算。
|
||||
// SSE、上传和对象流不设置总时限,避免长连接或客户端下载时间被误算成 SQL 超时。
|
||||
func QueryBudget() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
timeout := queryBudgetForRequest(c.Request.Method, c.Request.URL.Path, c.FullPath())
|
||||
if timeout <= 0 {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(c.Request.Context(), timeout)
|
||||
defer cancel()
|
||||
c.Request = c.Request.WithContext(ctx)
|
||||
c.Next()
|
||||
if errors.Is(ctx.Err(), context.DeadlineExceeded) && !c.Writer.Written() {
|
||||
response.Error(c, http.StatusServiceUnavailable, "query_timeout", "请求处理超时,请稍后重试")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func queryBudgetForRequest(method, path, route string) time.Duration {
|
||||
if isUnboundedRequest(path, route) {
|
||||
return 0
|
||||
}
|
||||
if isPaymentNotifyPath(path) {
|
||||
return 30 * time.Second
|
||||
}
|
||||
if route == "/api/files/access/batch" || route == "/api/admin/files/access/batch" {
|
||||
return 5 * time.Second
|
||||
}
|
||||
if strings.Contains(route, "/qrcodes/ocr-group-name") || route == "/api/open/listing-uploads" {
|
||||
return 60 * time.Second
|
||||
}
|
||||
if strings.Contains(route, "/payment-configs") || strings.HasSuffix(route, "/sync-accounts") {
|
||||
return 30 * time.Second
|
||||
}
|
||||
if method == http.MethodGet {
|
||||
switch {
|
||||
case route == "/api/admin/dashboard",
|
||||
strings.HasPrefix(route, "/api/admin/chats"),
|
||||
strings.HasPrefix(route, "/api/admin/finance"),
|
||||
strings.HasPrefix(route, "/api/admin/orders"),
|
||||
strings.HasPrefix(route, "/api/admin/listings"),
|
||||
strings.HasPrefix(route, "/api/admin/withdrawals"),
|
||||
strings.HasPrefix(route, "/api/admin/manual-entries"):
|
||||
return 8 * time.Second
|
||||
}
|
||||
}
|
||||
return defaultAPIQueryBudget
|
||||
}
|
||||
|
||||
func isUnboundedRequest(path, route string) bool {
|
||||
if isSSEPath(path, route) {
|
||||
return true
|
||||
}
|
||||
switch path {
|
||||
case "/api/files/object",
|
||||
"/api/admin/files/object",
|
||||
"/api/public/files/object",
|
||||
"/api/files/upload",
|
||||
"/api/admin/files/upload":
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type tenantConcurrencyState struct {
|
||||
slots chan struct{}
|
||||
refs int
|
||||
}
|
||||
|
||||
type tenantConcurrencyLimiter struct {
|
||||
mu sync.Mutex
|
||||
sites map[uint64]*tenantConcurrencyState
|
||||
limit int
|
||||
wait time.Duration
|
||||
}
|
||||
|
||||
// TenantConcurrencyLimit 隔离高成本后台读请求,避免单租户拖垮全站数据库连接池。
|
||||
func TenantConcurrencyLimit(limit int, wait time.Duration) gin.HandlerFunc {
|
||||
if limit < 1 {
|
||||
limit = 1
|
||||
}
|
||||
limiter := &tenantConcurrencyLimiter{
|
||||
sites: make(map[uint64]*tenantConcurrencyState),
|
||||
limit: limit,
|
||||
wait: wait,
|
||||
}
|
||||
return func(c *gin.Context) {
|
||||
if c.Request.Method != http.MethodGet || !isHighCostAdminRead(c.Request.URL.Path, c.FullPath()) {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
tenantID := tenancy.IDFromContext(c.Request.Context())
|
||||
release, ok := limiter.acquire(c.Request.Context(), tenantID)
|
||||
if !ok {
|
||||
c.Header("Retry-After", "1")
|
||||
response.Error(c, http.StatusTooManyRequests, "high_cost_query_busy", "当前查询繁忙,请稍后重试")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
defer release()
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func isHighCostAdminRead(path, route string) bool {
|
||||
if path == "/api/admin/chats/events" {
|
||||
return false
|
||||
}
|
||||
switch {
|
||||
case route == "/api/admin/dashboard",
|
||||
strings.HasPrefix(route, "/api/admin/chats"),
|
||||
strings.HasPrefix(route, "/api/admin/finance"),
|
||||
strings.HasPrefix(route, "/api/admin/orders"),
|
||||
strings.HasPrefix(route, "/api/admin/listings"),
|
||||
strings.HasPrefix(route, "/api/admin/withdrawals"),
|
||||
strings.HasPrefix(route, "/api/admin/manual-entries"):
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (l *tenantConcurrencyLimiter) acquire(ctx context.Context, tenantID uint64) (func(), bool) {
|
||||
l.mu.Lock()
|
||||
state := l.sites[tenantID]
|
||||
if state == nil {
|
||||
state = &tenantConcurrencyState{slots: make(chan struct{}, l.limit)}
|
||||
l.sites[tenantID] = state
|
||||
}
|
||||
state.refs++
|
||||
l.mu.Unlock()
|
||||
|
||||
if !tryAcquireTenantSlot(ctx, state.slots, l.wait) {
|
||||
l.releaseRef(tenantID)
|
||||
return nil, false
|
||||
}
|
||||
var once sync.Once
|
||||
return func() {
|
||||
once.Do(func() {
|
||||
<-state.slots
|
||||
l.releaseRef(tenantID)
|
||||
})
|
||||
}, true
|
||||
}
|
||||
|
||||
func (l *tenantConcurrencyLimiter) releaseRef(tenantID uint64) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
state := l.sites[tenantID]
|
||||
if state == nil {
|
||||
return
|
||||
}
|
||||
state.refs--
|
||||
if state.refs == 0 {
|
||||
delete(l.sites, tenantID)
|
||||
}
|
||||
}
|
||||
|
||||
func tryAcquireTenantSlot(ctx context.Context, slots chan struct{}, wait time.Duration) bool {
|
||||
select {
|
||||
case slots <- struct{}{}:
|
||||
return true
|
||||
default:
|
||||
}
|
||||
if wait <= 0 {
|
||||
return false
|
||||
}
|
||||
timer := time.NewTimer(wait)
|
||||
defer timer.Stop()
|
||||
select {
|
||||
case slots <- struct{}{}:
|
||||
return true
|
||||
case <-ctx.Done():
|
||||
return false
|
||||
case <-timer.C:
|
||||
return false
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user