fix(gateway): 修复粘性会话预取分组错配并优化并发等待热路径
This commit is contained in:
@@ -604,17 +604,25 @@ func TestGatewayHotpathHelpers_CacheTTLAndStickyContext(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("prefetched_sticky_account_id_from_context", func(t *testing.T) {
|
||||
require.Equal(t, int64(0), prefetchedStickyAccountIDFromContext(context.TODO()))
|
||||
require.Equal(t, int64(0), prefetchedStickyAccountIDFromContext(context.Background()))
|
||||
require.Equal(t, int64(0), prefetchedStickyAccountIDFromContext(context.TODO(), nil))
|
||||
require.Equal(t, int64(0), prefetchedStickyAccountIDFromContext(context.Background(), nil))
|
||||
|
||||
ctx := context.WithValue(context.Background(), ctxkey.PrefetchedStickyAccountID, int64(123))
|
||||
require.Equal(t, int64(123), prefetchedStickyAccountIDFromContext(ctx))
|
||||
ctx = context.WithValue(ctx, ctxkey.PrefetchedStickyGroupID, int64(0))
|
||||
require.Equal(t, int64(123), prefetchedStickyAccountIDFromContext(ctx, nil))
|
||||
|
||||
groupID := int64(9)
|
||||
ctx2 := context.WithValue(context.Background(), ctxkey.PrefetchedStickyAccountID, 456)
|
||||
require.Equal(t, int64(456), prefetchedStickyAccountIDFromContext(ctx2))
|
||||
ctx2 = context.WithValue(ctx2, ctxkey.PrefetchedStickyGroupID, groupID)
|
||||
require.Equal(t, int64(456), prefetchedStickyAccountIDFromContext(ctx2, &groupID))
|
||||
|
||||
ctx3 := context.WithValue(context.Background(), ctxkey.PrefetchedStickyAccountID, "invalid")
|
||||
require.Equal(t, int64(0), prefetchedStickyAccountIDFromContext(ctx3))
|
||||
ctx3 = context.WithValue(ctx3, ctxkey.PrefetchedStickyGroupID, groupID)
|
||||
require.Equal(t, int64(0), prefetchedStickyAccountIDFromContext(ctx3, &groupID))
|
||||
|
||||
ctx4 := context.WithValue(context.Background(), ctxkey.PrefetchedStickyAccountID, int64(789))
|
||||
ctx4 = context.WithValue(ctx4, ctxkey.PrefetchedStickyGroupID, int64(10))
|
||||
require.Equal(t, int64(0), prefetchedStickyAccountIDFromContext(ctx4, &groupID))
|
||||
})
|
||||
|
||||
t.Run("window_cost_from_prefetch_context", func(t *testing.T) {
|
||||
@@ -745,6 +753,7 @@ func TestSelectAccountWithLoadAwareness_StickyReadReuse(t *testing.T) {
|
||||
}
|
||||
|
||||
ctx := context.WithValue(baseCtx, ctxkey.PrefetchedStickyAccountID, account.ID)
|
||||
ctx = context.WithValue(ctx, ctxkey.PrefetchedStickyGroupID, int64(0))
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "sess-hash", "", nil, "")
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
@@ -752,4 +761,26 @@ func TestSelectAccountWithLoadAwareness_StickyReadReuse(t *testing.T) {
|
||||
require.Equal(t, account.ID, result.Account.ID)
|
||||
require.Equal(t, int64(0), cache.getCalls.Load())
|
||||
})
|
||||
|
||||
t.Run("with_prefetch_group_mismatch_reads_cache", func(t *testing.T) {
|
||||
cache := &stickyGatewayCacheHotpathStub{stickyID: account.ID}
|
||||
svc := &GatewayService{
|
||||
accountRepo: repo,
|
||||
cache: cache,
|
||||
cfg: cfg,
|
||||
concurrencyService: concurrency,
|
||||
userGroupRateCache: gocache.New(time.Minute, time.Minute),
|
||||
modelsListCache: gocache.New(time.Minute, time.Minute),
|
||||
modelsListCacheTTL: time.Minute,
|
||||
}
|
||||
|
||||
ctx := context.WithValue(baseCtx, ctxkey.PrefetchedStickyAccountID, int64(999))
|
||||
ctx = context.WithValue(ctx, ctxkey.PrefetchedStickyGroupID, int64(77))
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "sess-hash", "", nil, "")
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.Account)
|
||||
require.Equal(t, account.ID, result.Account.ID)
|
||||
require.Equal(t, int64(1), cache.getCalls.Load())
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user