Table of contents
Part of nurago, a collection of independent Go packages for backend services.
import "github.com/tecnickcom/nurago/pkg/sqs"
Package sqs wraps github.com/aws/aws-sdk-go-v2/service/sqs with an API that covers the common queue workflow: send, receive, decode, acknowledge (delete), and health-check.
How It Works
New creates a Client bound to a queue URL and optional message-group ID.
- queueURL must be a valid absolute URL. For FIFO queues (URL ends with
.fifo), a valid message-group ID is required; for standard queues it must be empty, and a non-empty value is rejected withErrUnexpectedMessageGroupID. Argument validation runs before any AWS configuration is loaded. - The AWS region is derived from the queue URL when it is not set explicitly,
so callers rarely need to repeat it; an explicit region supplied via
WithAWSOptionsstill takes precedence. - For FIFO queues,
Client.SendandClient.SendDatado not set a MessageDeduplicationId, so the queue must have ContentBasedDeduplication enabled; otherwise supply an explicit per-message deduplication ID viaClient.SendWithDeduplicationIDorClient.SendDataWithDeduplicationID. - Long polling and visibility are configurable via
WithWaitTimeSecondsandWithVisibilityTimeout, with defaults ofDefaultWaitTimeSeconds(20s) andDefaultVisibilityTimeout(600s). - Payloads can be sent/received as raw strings (
Client.Send,Client.Receive) or typed data (Client.SendData,Client.ReceiveData) through pluggable encode/decode hooks. - Configuration and argument problems are reported as exported sentinel
errors (see
ErrInvalidQueueURLand the others in this package) that callers can match with errors.Is.
Receive flow behavior:
Client.Receivereturns nil,nil when no message arrives within the long-poll window.Client.ReceiveDatareturns an empty receipt handle when no message is available.- After successful processing, callers should acknowledge via
Client.Deleteusing the receipt handle. - If decode fails,
Client.ReceiveDatastill returns the receipt handle so callers can choose whether to delete or re-queue according to their policy.
Usage
c, err := sqs.New(ctx,
"https://sqs.us-east-1.amazonaws.com/123456789012/my-queue",
"", // non-FIFO queue
sqs.WithWaitTimeSeconds(20),
sqs.WithVisibilityTimeout(300),
)
if err != nil {
return err
}
// Send typed payload
if err := c.SendData(ctx, event); err != nil {
return err
}
// Receive typed payload
var msg Event
receiptHandle, err := c.ReceiveData(ctx, &msg)
if err != nil {
return err
}
if receiptHandle != "" {
_ = c.Delete(ctx, receiptHandle)
}
When To Use
- A worker consumes a queue and acknowledges messages after processing.
- Message bodies are structs and should be encoded and decoded consistently.
Example
// A caller would normally configure a real client via options such as
// WithAWSOptions or WithEndpointMutable (and the region would be derived from
// the queue URL); here an injected client keeps the example self-contained, so
// no AWS configuration is loaded.
c, err := sqs.New(
context.TODO(),
"https://sqs.us-east-1.amazonaws.com/123456789012/my-queue",
"", // standard (non-FIFO) queue: no message group ID
sqs.WithSQSClient(&exampleSQSClient{}),
)
if err != nil {
fmt.Println("error:", err)
return
}
err = c.Send(context.TODO(), "hello world")
if err != nil {
fmt.Println("error:", err)
return
}
msg, err := c.Receive(context.TODO())
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(msg.Body)
// After processing, acknowledge the message by deleting it.
err = c.Delete(context.TODO(), msg.ReceiptHandle)
if err != nil {
fmt.Println("error:", err)
return
}
// Output:
// hello world
Full source is in example_sqs_test.go. More runnable examples are on pkg.go.dev.
Dependencies
Importing this package pulls 15 external modules:
github.com/aws/aws-sdk-go-v2github.com/aws/aws-sdk-go-v2/configgithub.com/aws/aws-sdk-go-v2/credentialsgithub.com/aws/aws-sdk-go-v2/feature/ec2/imdsgithub.com/aws/aws-sdk-go-v2/internal/configsourcesgithub.com/aws/aws-sdk-go-v2/internal/endpoints/v2github.com/aws/aws-sdk-go-v2/internal/v4agithub.com/aws/aws-sdk-go-v2/service/internal/accept-encodinggithub.com/aws/aws-sdk-go-v2/service/internal/presigned-urlgithub.com/aws/aws-sdk-go-v2/service/signingithub.com/aws/aws-sdk-go-v2/service/sqsgithub.com/aws/aws-sdk-go-v2/service/ssogithub.com/aws/aws-sdk-go-v2/service/ssooidcgithub.com/aws/aws-sdk-go-v2/service/stsgithub.com/aws/smithy-go