Введён тип QueueStore с индексом accessKey→{queueName}. Мутации очередей идут через
AddQueue/RemoveQueue (create/delete/restore/config/seed/admin), подсчёт и список —
через CountTenantQueues/TenantQueueNames. Устранены O(total) скан-по-префиксу в
countTenantQueues, deleteTenant, listTenantQueues, createTenantQueue. API SyncQueues
(Queues, Lock) сохранён — остальной код не затронут.
50 lines
2.3 KiB
Go
50 lines
2.3 KiB
Go
// Изменено: 2026-04-09
|
|
// Helper-функции для tenant-scoped операций с очередями.
|
|
// Используются всеми SQS handlers для изоляции очередей между тенантами.
|
|
package gosqs
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"shared-sqs/app/auth"
|
|
"shared-sqs/app/models"
|
|
"shared-sqs/app/tenant"
|
|
)
|
|
|
|
// tenantQueueKey — внутренний ключ очереди в SyncQueues в формате "{accessKey}:{queueName}".
|
|
// Такой формат гарантирует изоляцию: тенант видит только очереди с префиксом своего accessKey.
|
|
func tenantQueueKey(tenantAccessKey, queueName string) string {
|
|
return tenantAccessKey + ":" + queueName
|
|
}
|
|
|
|
// getTenantFromContext — извлекает тенанта из request context.
|
|
// Возвращает nil если тенант не найден (не должно быть — auth middleware должен это поймать раньше).
|
|
func getTenantFromContext(r *http.Request) *tenant.Tenant {
|
|
t, _ := r.Context().Value(auth.TenantContextKey).(*tenant.Tenant)
|
|
return t
|
|
}
|
|
|
|
// tenantQueueURL — формирует URL очереди для тенанта.
|
|
// Ловушка #10: QueueUrl ОБЯЗАН содержать tenantID в пути, иначе AWS SDK не сможет send/receive.
|
|
func tenantQueueURL(t *tenant.Tenant, queueName string) string {
|
|
host := models.CurrentEnvironment.Host
|
|
port := models.CurrentEnvironment.Port
|
|
region := models.CurrentEnvironment.Region
|
|
if region != "" {
|
|
return "http://" + region + "." + host + ":" + port + "/" + t.ID + "/" + queueName
|
|
}
|
|
return "http://" + host + ":" + port + "/" + t.ID + "/" + queueName
|
|
}
|
|
|
|
// tenantQueueARN — формирует ARN очереди для тенанта.
|
|
func tenantQueueARN(t *tenant.Tenant, queueName string) string {
|
|
return "arn:aws:sqs:" + models.CurrentEnvironment.Region + ":" + t.ID + ":" + queueName
|
|
}
|
|
|
|
// countTenantQueues — считает количество очередей тенанта (через tenant-индекс, O(1)).
|
|
// Используется для проверки лимита MaxQueues.
|
|
// Вызывать под SyncQueues.RLock()/Lock().
|
|
func countTenantQueues(tenantAccessKey string) int {
|
|
return models.SyncQueues.CountTenantQueues(tenantAccessKey)
|
|
}
|