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 2 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
19 changes: 17 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,30 @@ func startGohaqd(cmd *cobra.Command, args []string) {
QueueUrl: q.QueueUrl,
WaitTimeSeconds: aws.Int64(20),
}
sem = make(chan *sqs.Message)
Copy link
Contributor

Choose a reason for hiding this comment

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

@ApsOps This channel will have only one message at a time?
Instead of this approach I think we should go with buffered channel.
sem = make(chan *sqs.Message, parallelRequests)

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 I had taken that approach first, but changed it since it doesn't really help. Consumers are almost always slower than producers here.

And it increases the chances of same messages being consumed buy other instances as well. If messages stay in buffer for longer than the SQS visibility timeout, they'd be picked by other gohaqd instances as well.

Does this make sense?


for i := 0; i < parallelRequests; i++ {
go startConsumer(q.QueueUrl)
}

for {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you put this for loop above the other one?
Looks logically aligned. First poll then process.

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 can't. Since this is an infinite loop, the following part won't be reachable.

	for i := 0; i < parallelRequests; i++ {
		go startConsumer(q.QueueUrl)
	}

pollSQS(q.QueueUrl)
pollSQS()
}
}

func pollSQS(queueURL *string) {
func pollSQS() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Also good to add function level comments.

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 Added some comments.

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.

}
}

func startConsumer(queueURL *string) {
for msg := range sem {
if sendMessageToURL(*msg.Body) {
_, err := svc.DeleteMessage(&sqs.DeleteMessageInput{
QueueUrl: queueURL,
Expand Down