-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
97e9381
commit a53f7c5
Showing
2 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |