-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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{ | ||
|
@@ -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(¶llelRequests, "parallel", 1, "Number of messages to be consumed in parallel") | ||
RootCmd.MarkPersistentFlagRequired("queuename") | ||
RootCmd.MarkPersistentFlagRequired("url") | ||
|
||
|
@@ -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 | ||
|
@@ -97,18 +100,30 @@ func startGohaqd(cmd *cobra.Command, args []string) { | |
QueueUrl: q.QueueUrl, | ||
WaitTimeSeconds: aws.Int64(20), | ||
} | ||
sem = make(chan *sqs.Message) | ||
|
||
for i := 0; i < parallelRequests; i++ { | ||
go startConsumer(q.QueueUrl) | ||
} | ||
|
||
for { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you put this There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also good to add function level comments. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ApsOps Would this be blocking if sem already has a message? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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?