fix: convert []byte to *string for JSONB columns in sora_task_repo

pq driver treats []byte as bytea, not jsonb. Convert charJSON and
reqBody to *string before passing to ExecContext for character_info
and request_body JSONB columns.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
erioandClaude Opus 4.6 committed 2026-03-07 19:52:13 +08:00
1 parent a64dca7c41
commit e617dfa009
10 files changed
+140 -11

No files matched your search

@@ -28,12 +28,15 @@ var (
getAffinityClientsLua string
//go:embed lua/get_affinity_clients_with_scores.lua
getAffinityClientsWithScoresLua string
//go:embed lua/clear_account_affinity.lua
clearAccountAffinityLua string
getAffinityScript = redis.NewScript(getAffinityLua)
updateAffinityScript = redis.NewScript(updateAffinityLua)
getAffinityCountScript = redis.NewScript(getAffinityCountLua)
getAffinityClientsScript = redis.NewScript(getAffinityClientsLua)
getAffinityClientsWithScoresScript = redis.NewScript(getAffinityClientsWithScoresLua)
clearAccountAffinityScript = redis.NewScript(clearAccountAffinityLua)
)
type gatewayCache struct {
@@ -269,3 +272,25 @@ func (c *gatewayCache) GetAccountAffinityClientsWithScores(
return result, nil
}
// ClearAccountAffinity 清除指定账号在所有分组的亲和记录(正向+反向索引)。
// 对每个 groupID 执行 Lua 脚本:读取反向索引获取所有客户端,
// 从每个客户端的正向索引中移除该账号,然后删除反向索引。
func (c *gatewayCache) ClearAccountAffinity(ctx context.Context, accountID int64, groupIDs []int64) error {
if len(groupIDs) == 0 {
return nil
}
ensureScriptLoaded(ctx, c.rdb, clearAccountAffinityScript)
pipe := c.rdb.Pipeline()
for _, gID := range groupIDs {
revKey := buildAffinityReverseKey(gID, accountID)
clearAccountAffinityScript.Run(ctx, pipe, []string{revKey}, gID, accountID)
}
_, err := pipe.Exec(ctx)
if err != nil && err != redis.Nil {
return err
}
return nil
}
@@ -0,0 +1,29 @@
-- 清除单个账号在指定分组的所有亲和记录(正向+反向)
-- KEYS[1] = client_affinity_rev:{groupID}:{accountID} (反向索引)
-- ARGV[1] = groupID (用于构建正向 key)
-- ARGV[2] = accountID (正向索引中要移除的成员)
-- 返回: 清理的客户端数量
local rev_key = KEYS[1]
local group_id = ARGV[1]
local account_id = ARGV[2]
-- 获取反向索引中所有客户端 ID
local clients = redis.call('ZRANGE', rev_key, 0, -1)
if #clients == 0 then
return 0
end
-- 从每个客户端的正向索引中移除该账号
for _, client_id in ipairs(clients) do
local fwd_key = 'client_affinity:' .. group_id .. ':' .. client_id
redis.call('ZREM', fwd_key, account_id)
-- 如果正向索引为空,删除 key
if redis.call('ZCARD', fwd_key) == 0 then
redis.call('DEL', fwd_key)
end
end
-- 删除反向索引
redis.call('DEL', rev_key)
return #clients
+15 -11
View File
@@ -17,13 +17,15 @@ func NewSoraTaskRepository(sqlDB *sql.DB) service.SoraTaskRepository {
}
func (r *SoraTaskRepository) Create(ctx context.Context, task *service.SoraTask) error {
charJSON, _ := json.Marshal(task.CharacterInfo)
if task.CharacterInfo == nil {
charJSON = nil
var charStr, reqStr *string
if task.CharacterInfo != nil {
b, _ := json.Marshal(task.CharacterInfo)
s := string(b)
charStr = &s
}
var reqBody []byte
if len(task.RequestBody) > 0 {
reqBody = task.RequestBody
s := string(task.RequestBody)
reqStr = &s
}
_, err := r.db.ExecContext(ctx, `
@@ -36,8 +38,8 @@ func (r *SoraTaskRepository) Create(ctx context.Context, task *service.SoraTask)
task.ID, task.AccountID, task.APIKeyID, task.UpstreamTaskID, task.ObjectType,
task.Model, task.Prompt, task.Status, task.Progress, task.VideoURL,
task.StoredKey, task.StorageType,
task.ShareID, charJSON, task.ErrorMessage, task.ErrorType,
reqBody, task.Seconds, task.Size, task.CreatedAt, task.CompletedAt,
task.ShareID, charStr, task.ErrorMessage, task.ErrorType,
reqStr, task.Seconds, task.Size, task.CreatedAt, task.CompletedAt,
)
return err
}
@@ -56,9 +58,11 @@ func (r *SoraTaskRepository) GetByIDAndAPIKey(ctx context.Context, id string, ap
}
func (r *SoraTaskRepository) Update(ctx context.Context, task *service.SoraTask) error {
charJSON, _ := json.Marshal(task.CharacterInfo)
if task.CharacterInfo == nil {
charJSON = nil
var charStr *string
if task.CharacterInfo != nil {
b, _ := json.Marshal(task.CharacterInfo)
s := string(b)
charStr = &s
}
_, err := r.db.ExecContext(ctx, `
@@ -71,7 +75,7 @@ func (r *SoraTaskRepository) Update(ctx context.Context, task *service.SoraTask)
WHERE id = $1`,
task.ID, task.UpstreamTaskID, task.Status, task.Progress,
task.VideoURL, task.StoredKey, task.StorageType,
task.ShareID, charJSON,
task.ShareID, charStr,
task.ErrorMessage, task.ErrorType,
task.CompletedAt, task.Seconds, task.Size, task.ObjectType,
)