From a53f7c5a0cf2589a5338f1825ff582da5bafd82c Mon Sep 17 00:00:00 2001 From: Duc Nguyen Date: Sun, 7 Jul 2024 19:17:49 +0700 Subject: [PATCH] Add dynamodb and sqs --- dynamodb/health_checker.go | 71 ++++++++++++++++++++++++++++++++++++++ sqs/health_checker.go | 57 ++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 dynamodb/health_checker.go create mode 100644 sqs/health_checker.go diff --git a/dynamodb/health_checker.go b/dynamodb/health_checker.go new file mode 100644 index 0000000..0ed7efc --- /dev/null +++ b/dynamodb/health_checker.go @@ -0,0 +1,71 @@ +package dynamodb + +import ( + "context" + "errors" + "github.com/aws/aws-sdk-go/service/dynamodb" + "time" +) + +type HealthChecker struct { + db *dynamodb.DynamoDB + name string + timeout time.Duration +} + +func NewDynamoDBHealthChecker(db *dynamodb.DynamoDB, name string, timeouts ...time.Duration) *HealthChecker { + var timeout time.Duration + if len(timeouts) >= 1 { + timeout = timeouts[0] + } else { + timeout = 4 * time.Second + } + return &HealthChecker{db, name, timeout} +} +func NewHealthChecker(db *dynamodb.DynamoDB, options ...string) *HealthChecker { + var name string + if len(options) > 0 && len(options[0]) > 0 { + name = options[0] + } else { + name = "dynamodb" + } + return NewDynamoDBHealthChecker(db, name, 4*time.Second) +} + +func (s *HealthChecker) Name() string { + return s.name +} + +func (s *HealthChecker) Check(ctx context.Context) (map[string]interface{}, error) { + res := make(map[string]interface{}, 0) + if s.timeout > 0 { + ctx, _ = context.WithTimeout(ctx, s.timeout) + } + + checkerChan := make(chan error) + go func() { + input := &dynamodb.ListTablesInput{} + _, err := s.db.ListTables(input) + checkerChan <- err + }() + select { + case err := <-checkerChan: + if err != nil { + return res, err + } + return res, err + case <-ctx.Done(): + return res, errors.New("connection timout") + } +} + +func (s *HealthChecker) Build(ctx context.Context, data map[string]interface{}, err error) map[string]interface{} { + if err == nil { + return data + } + if data == nil { + data = make(map[string]interface{}, 0) + } + data["error"] = err.Error() + return data +} diff --git a/sqs/health_checker.go b/sqs/health_checker.go new file mode 100644 index 0000000..4c113c2 --- /dev/null +++ b/sqs/health_checker.go @@ -0,0 +1,57 @@ +package sqs + +import ( + "context" + "github.com/aws/aws-sdk-go/service/sqs" + "time" +) + +type HealthChecker struct { + Client *sqs.SQS + QueueName *string + Service string + Timeout time.Duration +} + +func NewHealthChecker(client *sqs.SQS, queueName string, options ...string) *HealthChecker { + var name string + if len(options) > 0 && len(options[0]) > 0 { + name = options[0] + } else { + name = "sqs" + } + return NewSQSHealthChecker(client, name, queueName) +} +func NewSQSHealthChecker(client *sqs.SQS, name string, queueName string, options ...time.Duration) *HealthChecker { + var timeout time.Duration + if len(options) >= 1 && options[0] > 0 { + timeout = options[0] + } else { + timeout = 4 * time.Second + } + return &HealthChecker{Client: client, QueueName: &queueName, Service: name, Timeout: timeout} +} + +func (h *HealthChecker) Name() string { + return h.Service +} + +func (h *HealthChecker) Check(ctx context.Context) (map[string]interface{}, error) { + res := make(map[string]interface{}) + h.Client.Config.HTTPClient.Timeout = h.Timeout + _, err := h.Client.GetQueueUrl(&sqs.GetQueueUrlInput{ + QueueName: h.QueueName, + }) + return res, err +} + +func (h *HealthChecker) Build(ctx context.Context, data map[string]interface{}, err error) map[string]interface{} { + if err == nil { + return data + } + if data == nil { + data = make(map[string]interface{}, 0) + } + data["error"] = err.Error() + return data +}