Skip to content

Add configurable options for Ping Check and SSLMode #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ port="5432"
user="postgres"
dbname="postgres"
password="xxxxx"
sslmode="disable"
pingCheck=true
maxIdleConn = 2
maxOpenConn = 2

Expand Down
2 changes: 2 additions & 0 deletions kshieldconfig_example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
# port = "5432"
# user = "postgres"
# password = "password123"
# sslmode = "disable"
# pingCheck = true
# dbname = "mydb"
# maxIdleConn = 10
# maxOpenConn = 100
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ type MySQL struct {
Port string `toml:"port"`
User string `toml:"user"`
Password string `toml:"password"`
PingCheck bool `toml:"pingCheck"`
// DBName string `toml:"dbname"`
// SSLmode string `toml:"sslmode"`
MaxIdleConn int `toml:"maxIdleConn"`
Expand Down
11 changes: 7 additions & 4 deletions pkg/mysqldb/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ func Open(conf config.MySQL) (*sql.DB, string, error) {
Msg("Failed to connect to database")
return nil, "", err
}
err = db.Ping()
if err != nil {
// fmt.Printf("Failed to connect to database. Error: %s", err.Error())
return nil, "", err

if conf.PingCheck {
err = db.Ping()
if err != nil {
// fmt.Printf("Failed to connect to database. Error: %s", err.Error())
return nil, "", err
}
}
if conf.MaxIdleConn > 0 {
db.SetMaxIdleConns(conf.MaxIdleConn)
Expand Down
29 changes: 19 additions & 10 deletions pkg/postgresdb/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql"
"fmt"
"regexp"
"strings"

_ "github.com/lib/pq"
"github.com/rs/zerolog/log"
Expand All @@ -15,7 +16,8 @@ type Postgres struct {
User string `toml:"user"`
Password string `toml:"password"`
DBName string `toml:"dbname"`
// SSLmode string `toml:"sslmode"`
SSLmode string `toml:"sslmode"`
PingCheck bool `toml:"pingCheck"`
MaxIdleConn int `toml:"maxIdleConn"`
MaxOpenConn int `toml:"maxOpenConn"`
}
Expand All @@ -35,7 +37,7 @@ var re = regexp.MustCompile(`(?m)(?:host=)([^\s]+)`)

func Open(conf Postgres) (*sql.DB, string, error) {
// "host=localhost port=5432 user=postgres password=postgres dbname=postgres sslmode=disable"
url := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", conf.Host, conf.Port, conf.User, conf.Password, conf.DBName)
url := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s pingcheck=%t", conf.Host, conf.Port, conf.User, conf.Password, conf.DBName, conf.SSLmode, conf.PingCheck)

db, err := ConnectDatabaseUsingConnectionString(url)
if err != nil {
Expand Down Expand Up @@ -68,7 +70,11 @@ func Open(conf Postgres) (*sql.DB, string, error) {

// ConnectDatabaseUsingConnectionString connects to a PostgreSQL database using the provided connection string.
// It returns a database connection, the connection string, and an error if any.

func ConnectDatabaseUsingConnectionString(url string) (*sql.DB, error) {
parts := strings.Split(url, "pingcheck=")
pingcheck := parts[1]
url = parts[0]
db, err := sql.Open("postgres", url)
if err != nil {
log.Error().
Expand All @@ -78,15 +84,18 @@ func ConnectDatabaseUsingConnectionString(url string) (*sql.DB, error) {
return nil, err
}

err = db.Ping()
if err != nil {
log.Error().
Err(err).
Str("conn", url).
Msg("Failed to ping database")
db.Close()
return nil, err
if len(pingcheck) > 0 && pingcheck == "true" {
err = db.Ping()
if err != nil {
log.Error().
Err(err).
Str("conn", url).
Msg("Failed to ping database")
db.Close()
return nil, err
}
}


return db, nil
}