Skip to content

Commit

Permalink
add s.noMessageCallback function for SQS receiveMessage to callback w…
Browse files Browse the repository at this point in the history
…hen no message is received from the queue
  • Loading branch information
bpeng committed Nov 8, 2024
1 parent e1d8d60 commit af8ee29
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion aws/sqs/sqs.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ type Raw struct {
}

type SQS struct {
client *sqs.Client
client *sqs.Client
noMessageCallback func() //callback function to call when no message is reveived from the queue
}

// New returns an SQS struct which wraps an SQS client using the default AWS credentials chain.
Expand Down Expand Up @@ -89,6 +90,11 @@ func (s *SQS) Ready() bool {
return s.client != nil
}

// Setter method for emptyMessageCallback
func (s *SQS) SetNoMessageCallback(callback func()) {
s.noMessageCallback = callback
}

// Receive receives a raw message or error from the queue.
// After a successful receive the message will be in flight
// until it is either deleted or the visibility timeout expires
Expand Down Expand Up @@ -135,6 +141,9 @@ func (s *SQS) receiveMessage(ctx context.Context, input *sqs.ReceiveMessageInput
switch {
case r == nil || len(r.Messages) == 0:
// no message received
if s.noMessageCallback != nil {
s.noMessageCallback()
}
continue
case len(r.Messages) == 1:
raw := r.Messages[0]
Expand Down

0 comments on commit af8ee29

Please sign in to comment.