Skip to content

Resolve topics and queues your own way

By default the adapter finds a queue with GetQueueUrl on its derived name and a topic by paging ListTopics until an ARN ends in : plus its derived name. Settings.Topology replaces both.

Replace it when:

  • the account has many topics. The default pages ListTopics once per subject, per session;
  • you already hold the ARNs and URLs, for example from infrastructure-as-code output;
  • the queue is in another account or has a name the derivation cannot produce.

The interface

type Topology interface {
    QueueURL(ctx context.Context, clients sqs.Clients, name string, spec messaging.SourceSpec) (string, error)
    TopicARN(ctx context.Context, clients sqs.Clients, name string, subject string) (string, error)
}

type Clients struct {
    SQS *awssqs.Client
    SNS *awssns.Client
}

name is the name the adapter derived, which for a FIFO backend already ends .fifo. QueueURL also receives the subscription's spec; TopicARN also receives the subject. Return the queue URL or the topic ARN.

Return sqs.ErrNoQueue or sqs.ErrNoTopic, wrapped with the name, when the resource is not there, so callers can match on them. The default does.

An example

type fixed struct {
    queues map[string]string // derived name -> queue URL
    topics map[string]string // derived name -> topic ARN
}

func (f fixed) QueueURL(_ context.Context, _ sqs.Clients, name string, _ messaging.SourceSpec) (string, error) {
    if u, ok := f.queues[name]; ok {
        return u, nil
    }

    return "", errors.Wrapf(sqs.ErrNoQueue, "queue %q", name)
}

func (f fixed) TopicARN(_ context.Context, _ sqs.Clients, name, _ string) (string, error) {
    if a, ok := f.topics[name]; ok {
        return a, nil
    }

    return "", errors.Wrapf(sqs.ErrNoTopic, "topic %q", name)
}

backend := sqs.Standard(sqs.Settings{AWS: src, Topology: fixed{queues: queues, topics: topics}})

What the adapter still does

It still reads the queue's VisibilityTimeout and RedrivePolicy after your QueueURL returns, and still refuses a mismatched maxReceiveCount. The result of TopicARN is remembered per subject for the life of the session.

A Topology may create

The module creates nothing through the default, but a Topology that does is an operator's tool, and the adapter's own test fixture is one: it creates each topic, queue, dead-letter queue and subscription the first time it is asked. That is a test convenience, and why the default does not is the reason to think twice before shipping one.