Files
SQS-service/app/gosqs/tenant_helpers.go
T
Naeel c3ba2dcae4 chore: initial import from sless/shared-sqs (v0.1.14)
- Standalone SQS-service repository
- Multi-tenant message queue service, AWS SQS compatible
- Based on GoAws, with mutable tenants, auth, WebUI, Redis persistence
- Ready for independent development and deployment
- See doc/ and README.md for architecture and usage
2026-04-10 16:47:27 +03:00

57 lines
2.4 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 — считает количество очередей тенанта в SyncQueues.
// Используется для проверки лимита MaxQueues.
// Вызывать под SyncQueues.RLock().
func countTenantQueues(tenantAccessKey string) int {
prefix := tenantAccessKey + ":"
count := 0
for key := range models.SyncQueues.Queues {
if len(key) > len(prefix) && key[:len(prefix)] == prefix {
count++
}
}
return count
}