gas/queue
queue
import "github.com/gasmod/gas/queue"Package queue defines what every queue store of the module promises: the queues it accepts, the messages it sends, how a message is leased and redelivered, how it honors a context, its lifecycle, and the errors it returns.
The rules are Amazon SQS’s, so that code written against one store runs on another. The stores implement gas.JobQueueProvider and gas.Service, and each lives in its own package:
- gas/queue/sqs: Amazon SQS, or an emulator that speaks its API.
- gas/queue/memory: queues in the process’s memory, for tests and local development.
- gas/queue/queuetest: a mock for tests.
Each store’s New returns a constructor in the shape the gas container expects, so a store is registered as gas.JobQueueProvider:
gas.WithSingletonService[gas.JobQueueProvider](sqs.New())A producer sends, and a worker receives, handles and acknowledges:
err := q.Enqueue(ctx, "orders", []byte(`{"id":42}`), gas.WithJobAttributes(map[string]string{"kind": "created"}))
jobs, err := q.Dequeue(ctx, "orders", 10, 20*time.Second)for _, job := range jobs { if handle(job) == nil { err = q.Ack(ctx, "orders", job) } else { err = q.Nack(ctx, "orders", job) }}After this the queue orders no longer holds the messages that were
handled, and holds the others, visible again.
Contract
Section titled “Contract”Every store, and any gas.JobQueueProvider that wants to be one, keeps the rules of the sections from Queue names to Order of checks.
The module’s conformance tests run every store through these rules, with identical expectations, but for the differences of an emulator from SQS that a test names.
Queue names
Section titled “Queue names”A queue is named by a queue name or a queue URL, as ParseQueue reads it:
- A name is 1 to 80 ASCII letters, digits, hyphens and underscores. A
FIFO queue’s name ends with
.fifo. - A URL is
httporhttps, has a host and no user information, query or fragment, and its path ends with a queue name.
Anything else is ErrInvalidQueue, from every method, and the error never quotes it. A queue that does not exist is ErrQueueNotFound.
Errors name a queue by its name, never by its URL, which holds the AWS account ID.
Delivery
Section titled “Delivery”Delivery is at least once. A message is leased, not removed, when Dequeue returns it. It stays hidden for a visibility timeout and then becomes visible again, unless Ack deletes it first.
A send whose reply is lost, and that the store or the caller retries, can store the message twice. Consumers must be idempotent.
Sending and receiving
Section titled “Sending and receiving”Enqueue checks the message, as CheckMessage does, before it sends anything, and returns the first rule it breaks. A message that fails is never stored.
Dequeue returns up to maxMessages jobs, waiting up to wait for the first:
- maxMessages is 1 to MaxMessages.
- wait is 0 to MaxWait. A wait of 0 is a short poll, which returns at once. The wait is counted in whole seconds, rounded up.
- Values out of bounds are ErrInvalidArgument, never clamped.
- Dequeue returns no jobs, and no error, when none came. A store may return fewer jobs than there are messages.
Ack deletes the job’s message. Nack makes it visible again at once, to be received again. Each counts as done only once it returns nil. A job without a receipt handle is ErrInvalidReceipt.
Contexts
Section titled “Contexts”Every method returns the context’s error, wrapped, when its context has ended before it starts, and honors the context during the call.
Close is the exception: it always closes. A store whose Close context ends while calls are in flight may cut them short, as its package describes.
States
Section titled “States”A store is not initialized until its first successful Init, and closed after Close:
- Before its first successful Init, every method but Init and Close returns ErrNotInitialized.
- Init on an initialized store returns ErrAlreadyInitialized and changes nothing.
- After Close, until Init succeeds again, every method but Init and Close returns ErrClosed.
- Close on a store that is not initialized returns nil.
- Close then Init restarts the store.
Close refuses new calls and returns once every call in flight has returned. A Dequeue in flight when Close begins:
- returns no jobs, no later than its wait ends;
- returns ErrClosed, unless its receive failed first;
- leaves no message hidden: what it received is made visible again before it returns.
Order of checks
Section titled “Order of checks”A method checks, in this order, and returns the first failure:
- Its arguments, in the order of its parameters. The message of Enqueue is the exception: its rules are checked in the order CheckMessage checks them.
- The store’s state.
- The context.
Payloads
Section titled “Payloads”A payload is text: 1 byte or more of valid UTF-8, holding only the characters SQS allows, #x9, #xA, #xD, #x20 to #xD7FF, #xE000 to #xFFFD and #x10000 to #x10FFFF. Anything else is ErrInvalidPayload. Encode binary data, such as with base64, before it is sent.
A message is at most MaxPayloadSize, 1 MiB, counting the payload and,
for each attribute, its name, its value, and the 6 bytes of its type,
String.
Message attributes
Section titled “Message attributes”gas.WithJobAttributes sets the message’s attributes, at most MaxAttributes, 10. A name:
- is 1 to MaxAttributeNameLength, 256, ASCII letters, digits,
_,-and.; - neither starts nor ends with
., nor holds..; - does not start with
AWS.orAmazon., in any case; - is not the name of an SQS system attribute, as IsReservedAttribute reports, so that no producer can pass one off as a system attribute.
The names match exactly, as SQS’s are case-sensitive. A name that
differs only in case, such as sentTimestamp, is a user attribute of
its own, and a job holds it beside the system attribute.
A value is non-empty valid UTF-8 of the characters a payload may hold.
A broken rule is ErrInvalidAttribute. The error quotes neither the name nor the value, but for a system attribute’s name.
gas.WithDelay hides a message for 0 to MaxDelay, 15 minutes, counted in whole seconds, rounded up, so a delay is never shorter than asked. Without one, or with 0, the queue’s own delay applies, which is 0 unless the queue sets one. A delay out of bounds is ErrInvalidArgument.
System attributes
Section titled “System attributes”gas.Job.Attributes holds the message’s attributes, then these system attributes, which take the place of an attribute of the same name that another producer sent:
ApproximateReceiveCount: how many times the message was received, this time included, such as1.ApproximateFirstReceiveTimestamp: when the message was first received, in milliseconds since the Unix epoch.SentTimestamp: when the message was sent, in milliseconds since the Unix epoch.MessageGroupId: the group ID, when the message has one.MessageDeduplicationId: the dedupe ID of a FIFO message.SequenceNumber: the sequence number of a FIFO message.
SystemAttributes lists them. No other system attribute is reported.
A queue whose name ends with .fifo is a FIFO queue. On it:
- gas.WithGroupID is required. A store returns a group’s messages in the order they were sent and, while a message of a group is leased, no later message of the group.
- gas.WithDedupeID gives a dedupe ID. A message whose dedupe ID the queue saw within DedupeWindow, 5 minutes, is accepted and not stored.
- Without a dedupe ID, a queue with content-based deduplication takes the SHA-256 of the payload, and a queue without it refuses the message.
- A message takes no delay of its own.
A group ID and a dedupe ID are 1 to MaxIDLength, 128, ASCII letters, digits and punctuation.
On a standard queue, a group ID names the tenant of a message, as SQS’s fair queues take it, and orders nothing. A dedupe ID is refused.
Every broken rule is ErrInvalidArgument.
Visibility
Section titled “Visibility”Dequeue leases each message for the store’s VisibilityTimeout setting, or, when that is 0, for the queue’s own visibility timeout. Nack ends the lease at once.
The stores also have NackAfter and Extend, which set the lease to end a duration from now, 0 to MaxVisibilityTimeout, 12 hours, rounded up to a whole second:
- NackAfter retries later.
- Extend keeps a long job’s message hidden.
A lease never ends more than 12 hours after the receive. A duration out of bounds is ErrInvalidArgument.
Receipt handles
Section titled “Receipt handles”Nack, NackAfter and Extend need the message to be leased under the job’s receipt handle. Once its lease has ended, or it was received again, the memory store returns ErrNotInFlight, and SQS replies with an error, as the sqs package describes.
Ack with the receipt handle of the latest receive deletes the message, even after its lease ended. Ack with an older handle is not an error:
- On SQS, it may or may not delete the message.
- On the memory store, it deletes nothing.
Lifecycle
Section titled “Lifecycle”Stop the workers that call Dequeue, and let them finish their Ack and Nack, before Close. A call after Close returns ErrClosed, and an Ack refused then leaves the message to come back when its lease ends.
The gas Worker closes services in the reverse order of their Init, so a service that depends on the queue, and runs its workers, is closed before it. A worker that is not such a service is stopped by the app before Shutdown.
Security
Section titled “Security”Payloads, attribute values and receipt handles are application data, and a queue URL holds the AWS account ID.
No error the stores make holds a payload, an attribute name or value, a group or dedupe ID, a receipt handle, a queue URL, an endpoint, or a credential. A store’s errors name the operation and the queue name instead.
Concurrency
Section titled “Concurrency”Every store is safe for concurrent use, as each store’s package describes. The mock is too, except for setting its fields while it is called.
Errors
Section titled “Errors”Every error a store returns starts with, in order:
- its service name, such as
gas/queue/memory; - the operation, such as
enqueue; - the queue name, quoted, when the identifier is valid;
- what failed.
It wraps one of these sentinels, to be tested with errors.Is:
- ErrInvalidQueue: the identifier is neither a queue name nor a queue URL.
- ErrInvalidPayload, ErrInvalidAttribute: as under Payloads and Message attributes.
- ErrInvalidArgument: a delay, an ID, a FIFO rule, maxMessages, a wait or a visibility timeout, as above.
- ErrInvalidReceipt: the job has no receipt handle, or one the store did not issue.
- ErrQueueNotFound: the queue does not exist.
- ErrNotInFlight: the message is not leased under the job’s receipt handle, as under Receipt handles.
- ErrNotInitialized, ErrAlreadyInitialized, ErrClosed: as under States.
- ErrNilLogger: a store’s constructor got a nil logger.
- ErrNilConfig, ErrNoConfigProvider, ErrInvalidConfig: Init could not resolve a valid configuration.
The errors of this package’s checks, as returned, before a store adds its service name, the operation and the queue name:
invalid queue: emptyinvalid queue: not a queue name or URLinvalid payload: emptyinvalid payload: not valid UTF-8invalid payload: a character SQS does not allow, at byte 2invalid payload: 1048577 bytes with the attributes, more than 1048576invalid attribute: 11 attributes, at most 10invalid attribute: a name is empty or longer than 256 bytesinvalid attribute: a name holds a character other than letters, digits, '_', '-' and '.'invalid attribute: a name starts or ends with '.' or holds '..'invalid attribute: a name starts with the reserved prefix AWS. or Amazon., in any caseinvalid attribute: "SentTimestamp" is a system attributeinvalid attribute: a value is emptyinvalid attribute: a value is not valid UTF-8invalid attribute: a value holds a character SQS does not allowinvalid argument: delay must be 0 to 15m0sinvalid argument: a FIFO queue takes no per-message delayinvalid argument: a FIFO queue needs a group IDinvalid argument: group ID must be 1 to 128 ASCII letters, digits and punctuationinvalid argument: a standard queue takes no dedupe IDinvalid argument: dedupe ID must be 1 to 128 ASCII letters, digits and punctuationinvalid argument: maxMessages must be 1 to 10invalid argument: wait must be 0 to 20sinvalid argument: visibility timeout must be 0 to 12h0m0sinvalid receipt handle: emptyAPI reference
Section titled “API reference”Constants
Section titled “Constants”const ( // MaxPayloadSize is the largest size of a message, in bytes: the // payload, plus the name, the type and the value of each attribute. MaxPayloadSize = 1 << 20 // MaxAttributes is the most attributes a message carries. MaxAttributes = 10 // MaxAttributeNameLength is the longest attribute name, in bytes. MaxAttributeNameLength = 256 // MaxQueueNameLength is the longest queue name, in bytes. MaxQueueNameLength = 80 // MaxIDLength is the longest group ID or dedupe ID, in bytes. MaxIDLength = 128 // MaxDelay is the longest delay of a message. MaxDelay = 15 * time.Minute // MaxMessages is the most messages one Dequeue returns. MaxMessages = 10 // MaxWait is the longest wait of one Dequeue. MaxWait = 20 * time.Second // MaxVisibilityTimeout is the longest visibility timeout. MaxVisibilityTimeout = 12 * time.Hour // DedupeWindow is how long a FIFO queue remembers a dedupe ID. DedupeWindow = 5 * time.Minute)The limits every store keeps, which are SQS’s, as described in the package documentation.
const ( // AttributeReceiveCount is how many times the message was received, // this time included. AttributeReceiveCount = "ApproximateReceiveCount" // AttributeFirstReceiveTimestamp is when the message was first // received, in milliseconds since the Unix epoch. AttributeFirstReceiveTimestamp = "ApproximateFirstReceiveTimestamp" // AttributeSentTimestamp is when the message was sent, in // milliseconds since the Unix epoch. AttributeSentTimestamp = "SentTimestamp" // AttributeGroupID is the message's group ID, when it has one. AttributeGroupID = "MessageGroupId" // AttributeDedupeID is the dedupe ID of a FIFO message. AttributeDedupeID = "MessageDeduplicationId" // AttributeSequenceNumber is the sequence number of a FIFO message. AttributeSequenceNumber = "SequenceNumber")The system attributes Dequeue reports in gas.Job.Attributes, as described in the package documentation.
const FIFOSuffix = ".fifo"FIFOSuffix ends the name of every FIFO queue, and only of those.
Variables
Section titled “Variables”var ( // ErrInvalidQueue indicates that a queue identifier is empty, or is // neither a queue name nor a queue URL. ErrInvalidQueue = errors.New("invalid queue") // ErrInvalidPayload indicates that a payload is empty, is not valid // UTF-8, holds a character SQS does not allow, or is too large. ErrInvalidPayload = errors.New("invalid payload") // ErrInvalidAttribute indicates that the attributes of a message are // too many, or that a name or a value breaks the attribute rules. ErrInvalidAttribute = errors.New("invalid attribute") // ErrInvalidArgument indicates that a delay, a group ID, a dedupe ID, // a message count, a wait or a visibility timeout is out of its // bounds or not allowed for the queue. ErrInvalidArgument = errors.New("invalid argument") // ErrInvalidReceipt indicates that a job's receipt handle is empty, or // is not one the store issued. ErrInvalidReceipt = errors.New("invalid receipt handle") // ErrQueueNotFound indicates that the queue does not exist. ErrQueueNotFound = errors.New("queue does not exist") // ErrNotInFlight indicates that a job's visibility cannot change: the // message is no longer leased under that receipt handle. ErrNotInFlight = errors.New("message not in flight") // ErrNotInitialized indicates that a method was called before the // store's first successful Init. ErrNotInitialized = errors.New("service not initialized") // ErrAlreadyInitialized indicates that Init was called on a store that // is initialized. ErrAlreadyInitialized = errors.New("service already initialized") // ErrClosed indicates that a method was called after Close, before // Init succeeded again, or that Close ended the call. ErrClosed = errors.New("service closed") // ErrNilLogger indicates that a store's constructor was given a nil // logger. ErrNilLogger = errors.New("logger is nil") // ErrNilConfig indicates that WithConfig was given a nil // configuration. ErrNilConfig = errors.New("config is nil") // ErrNoConfigProvider indicates that there is neither a configuration // given to WithConfig nor a ConfigProvider to bind one from. ErrNoConfigProvider = errors.New("no config provider") // ErrInvalidConfig indicates that the configuration failed to bind, // failed its validate tags, or broke one of the store's rules. ErrInvalidConfig = errors.New("invalid config"))The sentinel errors every store of the module shares, as described in the package documentation. A store’s errors start with its own service name and the operation, and wrap one of these.
func CheckDequeue
Section titled “func CheckDequeue”func CheckDequeue(maxMessages int, wait time.Duration) errorCheckDequeue returns nil for the arguments of a Dequeue every store accepts, maxMessages 1 to MaxMessages and wait 0 to MaxWait, and otherwise an error wrapping ErrInvalidArgument.
func CheckMessage
Section titled “func CheckMessage”func CheckMessage(q Queue, m Message) errorCheckMessage returns nil for a message every store accepts on q, and otherwise the first rule it breaks, in the order the package documentation lists them: the attributes, wrapping ErrInvalidAttribute; the payload, wrapping ErrInvalidPayload; then the delay, the group ID and the dedupe ID, wrapping ErrInvalidArgument. The error never holds the payload, an attribute name or an attribute value.
func CheckReceipt
Section titled “func CheckReceipt”func CheckReceipt(job gas.Job) errorCheckReceipt returns nil for a job with a receipt handle, and otherwise an error wrapping ErrInvalidReceipt.
func CheckVisibilityTimeout
Section titled “func CheckVisibilityTimeout”func CheckVisibilityTimeout(d time.Duration) errorCheckVisibilityTimeout returns nil for a visibility timeout of NackAfter or Extend every store accepts, 0 to MaxVisibilityTimeout, and otherwise an error wrapping ErrInvalidArgument.
func IsReservedAttribute
Section titled “func IsReservedAttribute”func IsReservedAttribute(name string) boolIsReservedAttribute reports whether name is the name of an SQS message system attribute, which no user attribute may take.
func SystemAttributes
Section titled “func SystemAttributes”func SystemAttributes() []stringSystemAttributes returns the names of the system attributes Dequeue reports, in the order the package documentation lists them.
func WholeSeconds
Section titled “func WholeSeconds”func WholeSeconds(d time.Duration) int32WholeSeconds returns d in whole seconds, rounded up, as SQS counts a delay, a wait and a visibility timeout. It is for a d from 0 to MaxVisibilityTimeout, the largest of those.
type Message
Section titled “type Message”type Message struct { // Attributes are the user attributes, from gas.WithJobAttributes. Attributes map[string]string // GroupID is from gas.WithGroupID. GroupID string // DedupeID is from gas.WithDedupeID. DedupeID string // Payload is the message body. Payload []byte // Delay is from gas.WithDelay. Delay time.Duration}Message is what an Enqueue sends, with its options resolved.
func NewMessage
Section titled “func NewMessage”func NewMessage(payload []byte, opts []gas.EnqueueOption) MessageNewMessage returns the message of payload with opts resolved.
func (Message) Size
Section titled “func (Message) Size”func (m Message) Size() intSize returns the size of m that counts toward MaxPayloadSize: the payload, plus the name, the type and the value of each attribute.
type Queue
Section titled “type Queue”type Queue struct { // ID is the identifier as given. ID string // Name is the queue's name: ID itself, or the last path segment of a // URL. Errors name the queue by it. Name string // FIFO reports whether the name ends with FIFOSuffix. FIFO bool // URL reports whether ID is a queue URL. URL bool}Queue is a queue identifier, parsed.
func ParseQueue
Section titled “func ParseQueue”func ParseQueue(id string) (Queue, error)ParseQueue parses a queue identifier: a queue name, or an http or
https URL with a host, without user information, a query or a
fragment, whose path ends with a queue name. Otherwise it returns an
error wrapping ErrInvalidQueue, which never holds id.