Skip to content

Commit

Permalink
Add dynamodb and sqs
Browse files Browse the repository at this point in the history
  • Loading branch information
minhduc140583 committed Jul 7, 2024
1 parent 97e9381 commit a53f7c5
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
71 changes: 71 additions & 0 deletions dynamodb/health_checker.go
Original file line number Diff line number Diff line change
@@ -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
}
57 changes: 57 additions & 0 deletions sqs/health_checker.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit a53f7c5

Please sign in to comment.