Skip to content

Commit

Permalink
Merge pull request #67 from foodora/sqs-message-receive-count-getter
Browse files Browse the repository at this point in the history
Add received count getter to Message struct
  • Loading branch information
Muharem Ismailov committed Jul 16, 2019
2 parents 0aa606e + 8318618 commit f211fbe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
17 changes: 17 additions & 0 deletions pubsub/awssub/sub.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package awssub

import (
"errors"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/aws/aws-sdk-go/service/sqs/sqsiface"
"github.com/foodora/go-ranger/pubsub"
"strconv"
"sync/atomic"
"time"
)
Expand Down Expand Up @@ -187,6 +189,19 @@ func (m *subscriberMessage) Done() error {
return <-receipt
}

// Returns the number of times a message has been received from the queue but not deleted.
func (m *subscriberMessage) GetReceiveCount() (int, error) {
val, ok := m.message.Attributes[sqs.MessageSystemAttributeNameApproximateReceiveCount]
if !ok || val == nil {
return 0, errors.New("receive count is undefined")
}
n, err := strconv.Atoi(*val)
if err != nil {
return 0, fmt.Errorf("could not parse a string value '%s' to int", *val)
}
return n, nil
}

// Start will start consuming messages on the SQS queue
// and emit any messages to the returned channel.
// If it encounters any issues, it will populate the Err() error
Expand All @@ -209,10 +224,12 @@ func (s *subscriber) Start() <-chan pubsub.Message {
}
s.Logger.Printf("receiving messages")
// get messages
nameApproximateReceiveCount := sqs.MessageSystemAttributeNameApproximateReceiveCount
resp, err = s.sqs.ReceiveMessage(&sqs.ReceiveMessageInput{
MaxNumberOfMessages: aws.Int64(s.cfg.MaxMessages),
QueueUrl: s.queueURL,
WaitTimeSeconds: s.cfg.TimeoutSeconds,
AttributeNames: []*string{&nameApproximateReceiveCount},
})
if err != nil {
// we've encountered a major error
Expand Down
1 change: 1 addition & 0 deletions pubsub/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ type Message interface {
String() string
ExtendDoneDeadline(time.Duration) error
Done() error
GetReceiveCount() (int, error)
GetMessageId() string
}

0 comments on commit f211fbe

Please sign in to comment.