Skip to content
This repository has been archived by the owner on Aug 21, 2023. It is now read-only.

Commit

Permalink
added timeout to redis and db validations
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathankingfc committed Jan 18, 2021
1 parent 3017304 commit f713d00
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ build

/tmp

config-bundle-private/
config-bundle-private*
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ require (
golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be
gopkg.in/square/go-jose.v2 v2.5.1 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,8 @@ gopkg.in/yaml.v3 v3.0.0-20200121175148-a6ecf24a6d71 h1:Xe2gvTZUJpsvOWUnvmL/tmhVB
gopkg.in/yaml.v3 v3.0.0-20200121175148-a6ecf24a6d71/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
mellium.im/sasl v0.2.1 h1:nspKSRg7/SyO0cRGY71OkfHab8tf9kCts6a6oTDut0w=
Expand Down
6 changes: 5 additions & 1 deletion pkg/lib/fieldgroups/database/database_validator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package database

import (
"context"
"crypto/tls"
"crypto/x509"
"database/sql"
Expand All @@ -9,6 +10,7 @@ import (
"net"
"net/url"
"strings"
"time"

"github.com/go-pg/pg/v10"
mysql "github.com/go-sql-driver/mysql" //mysql driver
Expand Down Expand Up @@ -123,7 +125,9 @@ func ValidateDatabaseConnection(opts shared.Options, uri *url.URL, caCert string
defer db.Close()

// Try to ping database
err = db.Ping()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err = db.PingContext(ctx)
if err != nil {
return err
}
Expand Down
18 changes: 18 additions & 0 deletions pkg/lib/shared/jsonTypes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package shared

import (
"bytes"
"encoding/json"
)

// IntOrString is an int that may be unmarshaled from either a JSON number
// literal, or a JSON string.
type IntOrString int

// UnmarshalJSON will unmarshal an array of bytes into this type
func (i *IntOrString) UnmarshalJSON(d []byte) error {
var v int
err := json.Unmarshal(bytes.Trim(d, `"`), &v)
*i = IntOrString(v)
return err
}
3 changes: 2 additions & 1 deletion pkg/lib/shared/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ func ValidateRedisConnection(options *redis.Options, field, fgName string) (bool
rdb := redis.NewClient(options)

// Ping client
var ctx = context.Background()
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
_, err := rdb.Ping(ctx).Result()
if err != nil {
newError := ValidationError{
Expand Down

0 comments on commit f713d00

Please sign in to comment.