Documentation
¶
Overview ¶
Package sqsworker implements a SQS consumer that can process sqs messages from a SQS queue and optionally send the results to a result queue.
Overview ¶
The inenteded use of this package is for multiple consumers reading from the same queue. Consumers are represeted by concurrent handler functions that are managed by the Worker type. This package only does long-polling based sqs receives.
To use his package, first define a handler function. This can also be a closure:
var handlerFunction = func(ctx context.Context, m *sqs.Message) ([]byte, error) {
return []byte(strings.ToLower(*m.Body)), nil
}
The function must match the following type definition:
type Handler func(context.Context, *sqs.Message) ([]byte, error)
A Worker Struct can be initialized with the NewWorker method, and you may optionally define an outbound queue, and number of concurrent workers. If the number of workers is not set, the number of workers defaults to runtime.NumCPU().
sess := session.New(&aws.Config{Region: aws.String("us-east-1")})
w := sqsworker.NewWorker(sess, sqsworker.WorkerConfig{
QueueIn: "https://sqs.us-east-1.amazonaws.com/88888888888/In",
QueueOut: "https://sqs.us-east-1.amazonaws.com/88888888888/Out",
Workers: 4,
Handler: handlerFunction,
Name: "TestApp",
})
w.Run()
The worker will send messages to the QueuOut queue on successful runs.
Concurrency ¶
Handler function will be called concurrently by multiple workers depending on the configuration. It is best to ensure that handler functions can be executed concurrently, especially if they are closures and share state.
Index ¶
Examples ¶
Constants ¶
const DefaultMaxNumberOfMessages = 10
DefaultMaxNumberOfMessages amount of messages received by each SQS request
const DefaultVisibilityTimeout = 60
DefaultVisibilityTimeout SQS visibility Timeout
const DefaultWaitTimeSeconds = 20
DefaultWaitTimeSeconds Long-polling interval for SQS
const DefaultWorkers = 1
DefaultWorkers Number of worker goroutines to spawn, each runs the handler function
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Worker ¶
type Worker struct {
QueueInURL string
QueueOutURL string
Queue sqsiface.SQSAPI
Session *session.Session
Consumers int
Logger *zap.Logger
Handler Handler
Callback Callback
Name string
// contains filtered or unexported fields
}
Worker encapsulates the SQS consumer
Example ¶
package main
import (
"context"
"github.com/ajbeach2/sqsworker"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sqs"
"strings"
)
func main() {
var handlerFunction = func(ctx context.Context, m *sqs.Message) ([]byte, error) {
return []byte(strings.ToLower(*m.Body)), nil
}
sess := session.New(&aws.Config{Region: aws.String("us-east-1")})
w := sqsworker.NewWorker(sess, sqsworker.WorkerConfig{
QueueIn: "https://sqs.us-east-1.amazonaws.com/88888888888/In",
QueueOut: "https://sqs.us-east-1.amazonaws.com/88888888888/Out",
Workers: 1,
Handler: handlerFunction,
Name: "TestApp",
})
w.Run()
}
Output:
func NewWorker ¶
func NewWorker(sess *session.Session, wc WorkerConfig) *Worker
NewWorker constructor for SQS Worker
type WorkerConfig ¶
type WorkerConfig struct {
QueueIn string
QueueOut string
// If the number of workers is 0, the number of workers defaults to runtime.NumCPU()
Workers int
Handler Handler
Callback Callback
Name string
Logger *zap.Logger
}
WorkerConfig settings for Worker to be passed in NewWorker Contstuctor