Skip to content

Commit 8acfe26

Browse files
committed
Add timeout into healthcheck
1 parent 2f9c87a commit 8acfe26

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

func.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ func GetQueueUrl(client *sqs.SQS, queueName string) (string, error) {
1515
return *result.QueueUrl, err
1616
}
1717

18-
func MapToAttributes(messageAttributes map[string]string) map[string]*sqs.MessageAttributeValue {
19-
attributes := make(map[string]*sqs.MessageAttributeValue)
20-
if messageAttributes != nil {
21-
for k, v := range messageAttributes {
18+
func MapToAttributes(attributes map[string]string) map[string]*sqs.MessageAttributeValue {
19+
attrs := make(map[string]*sqs.MessageAttributeValue)
20+
if attributes != nil {
21+
for k, v := range attributes {
2222
x := sqs.MessageAttributeValue{
2323
DataType: aws.String("String"),
2424
StringValue: aws.String(v),
2525
}
26-
attributes[k] = &x
26+
attrs[k] = &x
2727
}
2828
}
29-
return attributes
29+
return attrs
3030
}

health_checker.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@ package sqs
33
import (
44
"context"
55
"github.com/aws/aws-sdk-go/service/sqs"
6+
"time"
67
)
78

89
type HealthChecker struct {
910
Client *sqs.SQS
1011
QueueName *string
1112
Service string
13+
Timeout time.Duration
1214
}
1315

14-
func NewHealthChecker(client *sqs.SQS, queueName string, options...string) *HealthChecker {
16+
func NewHealthChecker(client *sqs.SQS, queueName string, options ...string) *HealthChecker {
1517
var name string
1618
if len(options) > 0 && len(options[0]) > 0 {
1719
name = options[0]
@@ -20,8 +22,14 @@ func NewHealthChecker(client *sqs.SQS, queueName string, options...string) *Heal
2022
}
2123
return NewSQSHealthChecker(client, name, queueName)
2224
}
23-
func NewSQSHealthChecker(client *sqs.SQS, name string, queueName string) *HealthChecker {
24-
return &HealthChecker{Client: client, QueueName: &queueName, Service: name}
25+
func NewSQSHealthChecker(client *sqs.SQS, name string, queueName string, options ...time.Duration) *HealthChecker {
26+
var timeout time.Duration
27+
if len(options) >= 1 && options[0] > 0 {
28+
timeout = options[0]
29+
} else {
30+
timeout = 4 * time.Second
31+
}
32+
return &HealthChecker{Client: client, QueueName: &queueName, Service: name, Timeout: timeout}
2533
}
2634

2735
func (h *HealthChecker) Name() string {

sender.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ func NewSender(client *sqs.SQS, queueURL string, delaySeconds *int64) *Sender {
2424
return &Sender{Client: client, QueueURL: &queueURL, DelaySeconds: delaySeconds}
2525
}
2626

27-
func (p *Sender) Send(ctx context.Context, data []byte, messageAttributes map[string]string) (string, error) {
28-
attributes := MapToAttributes(messageAttributes)
27+
func (p *Sender) Send(ctx context.Context, data []byte, attributes map[string]string) (string, error) {
28+
attrs := MapToAttributes(attributes)
2929
s := string(data)
3030
result, err := p.Client.SendMessage(&sqs.SendMessageInput{
3131
DelaySeconds: p.DelaySeconds,
32-
MessageAttributes: attributes,
32+
MessageAttributes: attrs,
3333
MessageBody: aws.String(s),
3434
QueueUrl: p.QueueURL,
3535
})

0 commit comments

Comments
 (0)