Skip to content

Commit

Permalink
Init config defaults outside of checker function
Browse files Browse the repository at this point in the history
  • Loading branch information
vgarvardt committed Sep 22, 2017
1 parent e4a6e6b commit d96a4df
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 106 deletions.
33 changes: 0 additions & 33 deletions Gopkg.lock

This file was deleted.

38 changes: 0 additions & 38 deletions Gopkg.toml

This file was deleted.

14 changes: 7 additions & 7 deletions checks/http/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ type Config struct {
// - getting response status from defined URL
// - verifying that status code is less than 500
func New(config Config) func() error {
return func() error {
if config.LogFunc == nil {
config.LogFunc = func(err error, details string, extra ...interface{}) {}
}
if config.LogFunc == nil {
config.LogFunc = func(err error, details string, extra ...interface{}) {}
}

if config.RequestTimeout == 0 {
config.RequestTimeout = time.Second * 5
}
if config.RequestTimeout == 0 {
config.RequestTimeout = time.Second * 5
}

return func() error {
req, err := http.NewRequest(http.MethodGet, config.URL, nil)
if err != nil {
config.LogFunc(err, "Creating the request for the health check failed")
Expand Down
8 changes: 4 additions & 4 deletions checks/postgres/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ type Config struct {
// - selecting inserted row
// - deleting inserted row
func New(config Config) func() error {
return func() error {
if config.LogFunc == nil {
config.LogFunc = func(err error, details string, extra ...interface{}) {}
}
if config.LogFunc == nil {
config.LogFunc = func(err error, details string, extra ...interface{}) {}
}

return func() error {
db, err := sql.Open("postgres", config.DSN)
if err != nil {
config.LogFunc(err, "PostgreSQL health check failed during connect")
Expand Down
39 changes: 19 additions & 20 deletions checks/rabbitmq/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,31 @@ type Config struct {
// - publishing a message to the exchange with the defined routing key
// - consuming published message
func New(config Config) func() error {
return func() error {
if config.LogFunc == nil {
config.LogFunc = func(err error, details string, extra ...interface{}) {}
}
if config.LogFunc == nil {
config.LogFunc = func(err error, details string, extra ...interface{}) {}
}

if config.Exchange == "" {
config.Exchange = defaultExchange
}
if config.Exchange == "" {
config.Exchange = defaultExchange
}

if config.RoutingKey == "" {
host, err := os.Hostname()
if nil != err {
config.LogFunc(err, "RabbitMQ health check failed on settings default value for unset routing key")
return err
}
config.RoutingKey = host
if config.RoutingKey == "" {
host, err := os.Hostname()
if nil != err {
config.RoutingKey = "-unknown-"
}
config.RoutingKey = host
}

if config.Queue == "" {
config.Queue = fmt.Sprintf("%s.%s", config.Exchange, config.RoutingKey)
}
if config.Queue == "" {
config.Queue = fmt.Sprintf("%s.%s", config.Exchange, config.RoutingKey)
}

if config.ConsumeTimeout == 0 {
config.ConsumeTimeout = time.Second * 3
}
if config.ConsumeTimeout == 0 {
config.ConsumeTimeout = time.Second * 3
}

return func() error {
conn, err := amqp.Dial(config.DSN)
if err != nil {
config.LogFunc(err, "RabbitMQ health check failed on dial phase")
Expand Down
8 changes: 4 additions & 4 deletions checks/redis/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ type Config struct {
// - connection establishing
// - doing the PING command and verifying the response
func New(config Config) func() error {
return func() error {
if config.LogFunc == nil {
config.LogFunc = func(err error, details string, extra ...interface{}) {}
}
if config.LogFunc == nil {
config.LogFunc = func(err error, details string, extra ...interface{}) {}
}

return func() error {
pool := &redis.Pool{
MaxIdle: 1,
IdleTimeout: 10 * time.Second,
Expand Down

0 comments on commit d96a4df

Please sign in to comment.