59 lines
1.6 KiB
Go
59 lines
1.6 KiB
Go
package tenant
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"git.ipao.vip/rogee/go-sip/internal/contract"
|
|
)
|
|
|
|
const (
|
|
CommandExchange = "agent-call.commands.v1"
|
|
EventExchange = "agent-call.events.v1"
|
|
CommandQueuePrefix = "agent-call.executor."
|
|
CommandQueueSuffix = ".v1"
|
|
CommandRoutingPrefix = "agent-call.tenant."
|
|
CommandRoutingSuffix = ".call.execute"
|
|
DeadLetterQueueSuffix = ".dlq.v1"
|
|
maxAMQPNameBytes = 255
|
|
)
|
|
|
|
func CommandQueue(tenantKey string) (string, error) {
|
|
if err := contract.ValidateTenantKey(tenantKey); err != nil {
|
|
return "", err
|
|
}
|
|
return boundedQueueName(tenantKey, CommandQueueSuffix)
|
|
}
|
|
|
|
func DeadLetterQueue(tenantKey string) (string, error) {
|
|
if err := contract.ValidateTenantKey(tenantKey); err != nil {
|
|
return "", err
|
|
}
|
|
return boundedQueueName(tenantKey, DeadLetterQueueSuffix)
|
|
}
|
|
|
|
func boundedQueueName(tenantKey, suffix string) (string, error) {
|
|
queue := CommandQueuePrefix + tenantKey + suffix
|
|
if len([]byte(queue)) > maxAMQPNameBytes {
|
|
return "", fmt.Errorf("AMQP queue name exceeds %d UTF-8 bytes", maxAMQPNameBytes)
|
|
}
|
|
return queue, nil
|
|
}
|
|
|
|
func CommandRoutingKey(tenantKey string) (string, error) {
|
|
if err := contract.ValidateTenantKey(tenantKey); err != nil {
|
|
return "", err
|
|
}
|
|
return CommandRoutingPrefix + tenantKey + CommandRoutingSuffix, nil
|
|
}
|
|
|
|
func VerifyCommandRouting(tenantKey, routingKey string) error {
|
|
expected, err := CommandRoutingKey(tenantKey)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if expected != routingKey {
|
|
return fmt.Errorf("routing key does not match tenant_key: expected %q got %q", expected, routingKey)
|
|
}
|
|
return nil
|
|
}
|