Skip to content

Commit

Permalink
connection timeout configuration (#127)
Browse files Browse the repository at this point in the history
* created connection timeout configuration

* created postgres timeout test

* moved timeout test to conn_test

* handle rows.Scan error

* back timeout tests to postgres_test
  • Loading branch information
henriquechehad authored and Thiago Avelino committed Mar 18, 2017
1 parent be3cf87 commit 999f5c0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion adapters/postgres/connection/conn.go
Expand Up @@ -22,7 +22,7 @@ func MustGet() *sqlx.DB {
if db == nil {
cfg = config.Prest{}
config.Parse(&cfg)
dbURI := fmt.Sprintf("user=%s dbname=%s host=%s port=%v sslmode=disable", cfg.PGUser, cfg.PGDatabase, cfg.PGHost, cfg.PGPort)
dbURI := fmt.Sprintf("user=%s dbname=%s host=%s port=%v sslmode=disable connect_timeout=%d", cfg.PGUser, cfg.PGDatabase, cfg.PGHost, cfg.PGPort, cfg.PGConnTimeout)
if cfg.PGPass != "" {
dbURI += " password=" + cfg.PGPass
}
Expand Down
6 changes: 4 additions & 2 deletions adapters/postgres/postgres.go
Expand Up @@ -258,7 +258,6 @@ func CountByRequest(req *http.Request) (countQuery string, err error) {
func Query(SQL string, params ...interface{}) (jsonData []byte, err error) {
db := connection.MustGet()
prepare, err := db.Prepare(SQL)

if err != nil {
return
}
Expand All @@ -282,7 +281,10 @@ func Query(SQL string, params ...interface{}) (jsonData []byte, err error) {
for i := 0; i < count; i++ {
valuePtrs[i] = &values[i]
}
rows.Scan(valuePtrs...)
err = rows.Scan(valuePtrs...)
if err != nil {
return
}
entry := make(map[string]interface{})
for i, col := range columns {
var v interface{}
Expand Down
21 changes: 21 additions & 0 deletions adapters/postgres/postgres_test.go
Expand Up @@ -791,3 +791,24 @@ func TestColumnsByRequest(t *testing.T) {
}
}
}

func TestTimeout(t *testing.T) {
_, err := Query("SET statement_timeout TO 10;")
if err != nil {
t.Errorf("Error setting statement_timeout: %s", err)
}

_, err = Query("SELECT pg_sleep(1000);")
if err == nil {
t.Errorf("Error should not be nil")
}

if !strings.Contains(err.Error(), "statement timeout") {
t.Errorf("Returned different error: %s", err)
}

_, err = Query("SET statement_timeout TO 0;")
if err != nil {
t.Errorf("Error disabling statement_timeout")
}
}
3 changes: 3 additions & 0 deletions config/config.go
Expand Up @@ -36,6 +36,7 @@ type Prest struct {
PGDatabase string
PGMaxIdleConn int
PGMAxOpenConn int
PGConnTimeout int
JWTKey string
MigrationsPath string
QueriesPath string
Expand Down Expand Up @@ -66,6 +67,7 @@ func viperCfg() {
viper.SetDefault("pg.port", 5432)
viper.SetDefault("pg.maxidleconn", 10)
viper.SetDefault("pg.maxopenconn", 10)
viper.SetDefault("pg.conntimeout", 10)

user, err := user.Current()
if err != nil {
Expand All @@ -89,6 +91,7 @@ func Parse(cfg *Prest) (err error) {
cfg.PGDatabase = viper.GetString("pg.database")
cfg.PGMaxIdleConn = viper.GetInt("pg.maxidleconn")
cfg.PGMAxOpenConn = viper.GetInt("pg.maxopenconn")
cfg.PGConnTimeout = viper.GetInt("pg.conntimeout")
cfg.JWTKey = viper.GetString("jwt.key")
cfg.MigrationsPath = viper.GetString("migrations")
cfg.AccessConf.Restrict = viper.GetBool("access.restrict")
Expand Down

0 comments on commit 999f5c0

Please sign in to comment.