Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to process messages in parallel #2

Merged
merged 3 commits into from
May 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## v1.1 (May 29, 2017)

* Add option to process messages in parallel ([#2](https://github.com/Codigami/gohaqd/pull/2), [@ApsOps](https://github.com/ApsOps))

## v1.0 (May 9, 2017)

* Initial stable release

## v0.3 (December 21, 2016)

* Instantiate SQS object only once ([#5](https://github.com/ApsOps/gohaqd/pull/5), [@ApsOps](https://github.com/ApsOps))
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ It pulls data off a queue, inserts it into the message body, and sends an HTTP P
## Flags:
```
--aws-region string AWS Region for the SQS queue (default "us-east-1")
--parallel int Number of messages to be consumed in parallel (default 1)
-q, --queue-name string queue name to use
--sqs-endpoint string SQS Endpoint for using with fake_sqs
-u, --url string endpoint to send an HTTP POST request with contents of queue message in the body
Expand Down
27 changes: 25 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var queueName string
var url string
var awsRegion string
var sqsEndpoint string
var parallelRequests int

// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Expand All @@ -62,6 +63,7 @@ func init() {
RootCmd.PersistentFlags().StringVarP(&url, "url", "u", "", "endpoint to send an HTTP POST request with contents of queue message in the body")
RootCmd.PersistentFlags().StringVar(&awsRegion, "aws-region", "us-east-1", "AWS Region for the SQS queue")
RootCmd.PersistentFlags().StringVar(&sqsEndpoint, "sqs-endpoint", "", "SQS Endpoint for using with fake_sqs")
RootCmd.PersistentFlags().IntVar(&parallelRequests, "parallel", 1, "Number of messages to be consumed in parallel")
RootCmd.MarkPersistentFlagRequired("queuename")
RootCmd.MarkPersistentFlagRequired("url")

Expand All @@ -72,6 +74,7 @@ func init() {
var svc *sqs.SQS
var msgparams *sqs.ReceiveMessageInput
var httpClient *http.Client
var sem chan *sqs.Message

func startGohaqd(cmd *cobra.Command, args []string) {
var config *aws.Config
Expand All @@ -97,18 +100,37 @@ func startGohaqd(cmd *cobra.Command, args []string) {
QueueUrl: q.QueueUrl,
WaitTimeSeconds: aws.Int64(20),
}

// Create semaphore channel for passing messages to consumers
sem = make(chan *sqs.Message)

// Start multiple goroutines for consumers base on --parallel flag
for i := 0; i < parallelRequests; i++ {
go startConsumer(q.QueueUrl)
}

// Infinitely poll SQS queue for messages
for {
pollSQS(q.QueueUrl)
pollSQS()
}
}

func pollSQS(queueURL *string) {
// Receives messages from SQS queue and adds to semaphore channel
func pollSQS() {
resp, err := svc.ReceiveMessage(msgparams)
if err != nil {
log.Fatalf(err.Error())
}

for _, msg := range resp.Messages {
sem <- msg
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ApsOps Would this be blocking if sem already has a message?
Buffered channel should eliminate direct dependency between pollSqs & startConsumer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ameykpatil We want this to block, like I explained in the above comment.

}
}

// Receives messages from semaphore channel and
// deletes a message from SQS queue is it's consumed successfully
func startConsumer(queueURL *string) {
for msg := range sem {
if sendMessageToURL(*msg.Body) {
_, err := svc.DeleteMessage(&sqs.DeleteMessageInput{
QueueUrl: queueURL,
Expand All @@ -121,6 +143,7 @@ func pollSQS(queueURL *string) {
}
}

// Sends a POST request to consumption endpoint with the SQS message as body
func sendMessageToURL(msg string) bool {
var resp *http.Response
var err error
Expand Down