fix(tenant): 修复生产备份迁移与未读查询性能

This commit is contained in:
yml
2026-11-16 19:50:49 +08:00
parent fed950ea5a
commit ac9717a5d8
5 changed files with 341 additions and 3 deletions
@@ -2,6 +2,7 @@ package middleware
import (
"context"
"errors"
"strings"
"hfb_sys/backend/internal/config"
@@ -62,6 +63,10 @@ func TenantContext(resolver TenantResolver, appEnv string, logger *zap.Logger, d
scope, err = resolver.ResolveCode(c.Request.Context(), defaultTenantCode)
}
if err != nil {
if errors.Is(err, context.Canceled) {
c.Abort()
return
}
if logger != nil {
logger.Error("经营租户解析失败,拒绝按旧站点上下文继续",
zap.String("host", host),
@@ -127,6 +127,36 @@ func TestTenantContextFailsClosedOnResolveError(t *testing.T) {
}
}
func TestTenantContextIgnoresClientCancellation(t *testing.T) {
gin.SetMode(gin.TestMode)
engine := gin.New()
engine.Use(TenantContext(
&fakeTenantResolver{hostErr: context.Canceled},
"production",
nil,
))
handlerCalled := false
engine.GET("/probe", func(c *gin.Context) {
handlerCalled = true
c.Status(http.StatusNoContent)
})
req := httptest.NewRequest(http.MethodGet, "/probe", nil)
req.Host = "jieyou.example.com"
ctx, cancel := context.WithCancel(req.Context())
cancel()
req = req.WithContext(ctx)
rec := httptest.NewRecorder()
engine.ServeHTTP(rec, req)
if handlerCalled {
t.Fatal("客户端取消后业务处理器不应继续执行")
}
if rec.Code == http.StatusServiceUnavailable {
t.Fatalf("客户端取消不应返回 503, body=%s", rec.Body.String())
}
}
func TestTenantContextSkipsLegacyDistributionFallback(t *testing.T) {
tenantResolver := &fakeTenantResolver{
hostScope: &tenancy.Scope{TenantID: 202, TenantCode: "jieyou", TenantName: "解忧商行"},