feat(step3): тенанты read-through Redis (индекс accessKey→tenant, fallback в auth, бэкфилл на старте)

This commit is contained in:
Repinoid
2026-10-10 16:09:52 +03:00
parent 1862f058be
commit 8e22bc4b9b
3 changed files with 50 additions and 0 deletions
+27
View File
@@ -48,6 +48,8 @@ var (
const (
// redisHashTenants — HASH: tenantID → JSON тенанта
redisHashTenants = "ssq:tenants"
// redisHashTenantAK — HASH: accessKey → JSON тенанта (индекс read-through в auth)
redisHashTenantAK = "ssq:tenant_ak"
// redisHashQueues — HASH: queueKey → JSON метаданных очереди (без Messages с v2)
redisHashQueues = "ssq:queues"
// redisMsgHashPrefix — префикс для per-message HASH: ssq:msg:{queueKey} → uuid → JSON(SqsMessage)
@@ -519,6 +521,11 @@ func SaveTenantRaw(id string, jsonData []byte) {
// Копируем bytes — вызывающий может переиспользовать буфер
dataCopy := make([]byte, len(jsonData))
copy(dataCopy, jsonData)
// Извлекаем accessKey для индекса read-through (без импорта пакета tenant)
var meta struct {
AccessKey string
}
_ = json.Unmarshal(dataCopy, &meta)
writeSeq := nextTenantWriteSeq(id)
asyncWrite(func() {
if !isLatestTenantWriteSeq(id, writeSeq) {
@@ -529,9 +536,29 @@ func SaveTenantRaw(id string, jsonData []byte) {
if err := Client.HSet(ctx, redisHashTenants, id, string(dataCopy)).Err(); err != nil {
log.Errorf("persistence: HSet tenant %q: %v", id, err)
}
if meta.AccessKey != "" {
if err := Client.HSet(ctx, redisHashTenantAK, meta.AccessKey, string(dataCopy)).Err(); err != nil {
log.Errorf("persistence: HSet tenant_ak %q: %v", meta.AccessKey, err)
}
}
})
}
// LoadTenantByAccessKey — read-through тенанта по accessKey (для auth-middleware).
// Возвращает сырой JSON; десериализацию делает вызывающий.
func LoadTenantByAccessKey(accessKey string) ([]byte, bool) {
if Client == nil || accessKey == "" {
return nil, false
}
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
v, err := Client.HGet(ctx, redisHashTenantAK, accessKey).Result()
if err != nil {
return nil, false
}
return []byte(v), true
}
// DeleteTenant — удаляет тенанта из Redis асинхронно.
func DeleteTenant(id string) {
if Client == nil {