From f560d4f0e42e789f773508466a3796a07c5932f3 Mon Sep 17 00:00:00 2001 From: Duc Nguyen Date: Sat, 23 Sep 2023 22:33:59 +0700 Subject: [PATCH] Remove sqs and dynamodb --- dynamodb/health_checker.go | 71 ------------------------------------- firestore/health_checker.go | 9 ++--- sqs/health_checker.go | 57 ----------------------------- 3 files changed, 5 insertions(+), 132 deletions(-) delete mode 100644 dynamodb/health_checker.go delete mode 100644 sqs/health_checker.go diff --git a/dynamodb/health_checker.go b/dynamodb/health_checker.go deleted file mode 100644 index d823bb7..0000000 --- a/dynamodb/health_checker.go +++ /dev/null @@ -1,71 +0,0 @@ -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/firestore/health_checker.go b/firestore/health_checker.go index 2eb49aa..15adaad 100644 --- a/firestore/health_checker.go +++ b/firestore/health_checker.go @@ -3,6 +3,7 @@ package firestore import ( "cloud.google.com/go/firestore" "context" + "errors" "google.golang.org/api/option" "google.golang.org/api/transport" ) @@ -21,7 +22,7 @@ func NewFirestoreHealthChecker(name string, projectId string, opts ...option.Cli func NewHealthCheckerWithProjectId(projectId string, opts ...option.ClientOption) *HealthChecker { return NewFirestoreHealthChecker("firestore", projectId, opts...) } -func NewHealthChecker(ctx context.Context, credentials []byte, options ...string) *HealthChecker { +func NewHealthChecker(ctx context.Context, credentials []byte, projectId string, options ...string) (*HealthChecker, error) { var name string if len(options) > 0 && len(options[0]) > 0 { name = options[0] @@ -31,12 +32,12 @@ func NewHealthChecker(ctx context.Context, credentials []byte, options ...string opts := option.WithCredentialsJSON(credentials) creds, er2 := transport.Creds(ctx, opts) if er2 != nil { - panic("Credentials Error: " + er2.Error()) + return nil, er2 } if creds == nil { - panic("Error: creds is nil") + return nil, errors.New("error: credentials is nil") } - return NewFirestoreHealthChecker(name, creds.ProjectID, opts) + return NewFirestoreHealthChecker(name, projectId, opts), nil } func (s HealthChecker) Name() string { return s.name diff --git a/sqs/health_checker.go b/sqs/health_checker.go deleted file mode 100644 index 4c113c2..0000000 --- a/sqs/health_checker.go +++ /dev/null @@ -1,57 +0,0 @@ -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 -}