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
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestQueryBudgetExcludesLongLivedAndStreamingRequests(t *testing.T) {
|
||||
cases := []struct {
|
||||
method string
|
||||
path string
|
||||
route string
|
||||
want time.Duration
|
||||
}{
|
||||
{method: http.MethodGet, path: "/api/orders", route: "/api/orders", want: defaultAPIQueryBudget},
|
||||
{method: http.MethodGet, path: "/api/admin/dashboard", route: "/api/admin/dashboard", want: 8 * time.Second},
|
||||
{method: http.MethodGet, path: "/api/admin/chats", route: "/api/admin/chats", want: 8 * time.Second},
|
||||
{method: http.MethodPost, path: "/api/files/access/batch", route: "/api/files/access/batch", want: 5 * time.Second},
|
||||
{method: http.MethodPost, path: "/api/payments/lakala/notify", route: "/api/payments/lakala/notify", want: 30 * time.Second},
|
||||
{method: http.MethodPost, path: "/api/admin/chats/qrcodes/ocr-group-name", route: "/api/admin/chats/qrcodes/ocr-group-name", want: 60 * time.Second},
|
||||
{method: http.MethodPost, path: "/api/open/listing-uploads", route: "/api/open/listing-uploads", want: 60 * time.Second},
|
||||
{method: http.MethodPost, path: "/api/admin/payment-configs/1/test-orders", route: "/api/admin/payment-configs/:id/test-orders", want: 30 * time.Second},
|
||||
{method: http.MethodGet, path: "/api/files/object", route: "/api/files/object", want: 0},
|
||||
{method: http.MethodPost, path: "/api/admin/files/upload", route: "/api/admin/files/upload", want: 0},
|
||||
{method: http.MethodGet, path: "/api/chats/events", route: "/api/chats/events", want: 0},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
if got := queryBudgetForRequest(tc.method, tc.path, tc.route); got != tc.want {
|
||||
t.Fatalf("queryBudgetForRequest(%s, %s) = %s, want %s", tc.method, tc.path, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHighCostAdminReadClassification(t *testing.T) {
|
||||
cases := []struct {
|
||||
path string
|
||||
route string
|
||||
want bool
|
||||
}{
|
||||
{path: "/api/admin/dashboard", route: "/api/admin/dashboard", want: true},
|
||||
{path: "/api/admin/chats/counts", route: "/api/admin/chats/counts", want: true},
|
||||
{path: "/api/admin/chats/events", route: "/api/admin/chats/events", want: false},
|
||||
{path: "/api/admin/finance/details", route: "/api/admin/finance/details", want: true},
|
||||
{path: "/api/admin/orders/123", route: "/api/admin/orders/:id", want: true},
|
||||
{path: "/api/admin/users", route: "/api/admin/users", want: false},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
if got := isHighCostAdminRead(tc.path, tc.route); got != tc.want {
|
||||
t.Fatalf("isHighCostAdminRead(%s) = %v, want %v", tc.path, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTenantConcurrencyLimiterRejectsWhenSaturated(t *testing.T) {
|
||||
limiter := &tenantConcurrencyLimiter{
|
||||
sites: make(map[uint64]*tenantConcurrencyState),
|
||||
limit: 1,
|
||||
wait: 10 * time.Millisecond,
|
||||
}
|
||||
release, ok := limiter.acquire(context.Background(), 101)
|
||||
if !ok {
|
||||
t.Fatal("first acquire failed")
|
||||
}
|
||||
if _, ok := limiter.acquire(context.Background(), 101); ok {
|
||||
t.Fatal("second acquire unexpectedly succeeded")
|
||||
}
|
||||
release()
|
||||
releaseAgain, ok := limiter.acquire(context.Background(), 101)
|
||||
if !ok {
|
||||
t.Fatal("acquire after release failed")
|
||||
}
|
||||
releaseAgain()
|
||||
}
|
||||
|
||||
func TestQueryBudgetUsesResolvedRouteBeforeHandler(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
engine := gin.New()
|
||||
engine.Use(QueryBudget())
|
||||
engine.GET("/api/admin/dashboard", func(c *gin.Context) {
|
||||
if _, ok := c.Request.Context().Deadline(); !ok {
|
||||
t.Fatal("high-cost route missing query deadline")
|
||||
}
|
||||
c.Status(http.StatusNoContent)
|
||||
})
|
||||
engine.GET("/api/files/object", func(c *gin.Context) {
|
||||
if _, ok := c.Request.Context().Deadline(); ok {
|
||||
t.Fatal("streaming route unexpectedly has query deadline")
|
||||
}
|
||||
c.Status(http.StatusNoContent)
|
||||
})
|
||||
|
||||
for _, path := range []string{"/api/admin/dashboard", "/api/files/object"} {
|
||||
recorder := httptest.NewRecorder()
|
||||
request := httptest.NewRequest(http.MethodGet, path, nil)
|
||||
engine.ServeHTTP(recorder, request)
|
||||
if recorder.Code != http.StatusNoContent {
|
||||
t.Fatalf("%s status = %d, want 204", path, recorder.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user