Skip to content

Commit

Permalink
fix: opt statement timeout, add cmp
Browse files Browse the repository at this point in the history
  • Loading branch information
23doors committed Aug 28, 2020
1 parent f3b36c8 commit 71c1c04
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
13 changes: 7 additions & 6 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/go-pg/pg/v9"
"github.com/google/go-cmp/cmp"
"go.uber.org/zap"

"github.com/Syncano/pkg-go/v2/log"
Expand Down Expand Up @@ -42,7 +43,7 @@ type DB struct {
type Options struct {
pg.Options

StatementTimeout int
StatementTimeout time.Duration
Host string
Port string
}
Expand Down Expand Up @@ -90,7 +91,7 @@ func (o *Options) PGOptions() *pg.Options {

if opts.OnConnect == nil && o.StatementTimeout != 0 {
opts.OnConnect = func(conn *pg.Conn) error {
_, err := conn.Exec("SET statement_timeout = {}", 3*time.Second/time.Microsecond)
_, err := conn.Exec("SET statement_timeout = {}", o.StatementTimeout/time.Microsecond)
return err
}
}
Expand All @@ -99,12 +100,12 @@ func (o *Options) PGOptions() *pg.Options {
}

// NewDB creates a database.
func NewDB(opts, instancesOpts *pg.Options, logger *log.Logger, debug bool) *DB {
commonDB := initDB(opts, logger, debug)
func NewDB(opts, instancesOpts *Options, logger *log.Logger, debug bool) *DB {
commonDB := initDB(opts.PGOptions(), logger, debug)
tenantDB := commonDB

if instancesOpts != nil && (instancesOpts.Addr != opts.Addr || instancesOpts.Database != opts.Database) {
tenantDB = initDB(instancesOpts, logger, debug)
if instancesOpts != nil && !cmp.Equal(instancesOpts, opts) {
tenantDB = initDB(instancesOpts.PGOptions(), logger, debug)
}

return &DB{
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/go-pg/pg/v9 v9.1.7
github.com/go-redis/cache/v7 v7.0.2
github.com/go-redis/redis/v7 v7.4.0
github.com/google/go-cmp v0.5.1
github.com/google/uuid v1.1.1 // indirect
github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 // indirect
github.com/hashicorp/yamux v0.0.0-20200609203250-aecfd211c9ce
Expand Down

0 comments on commit 71c1c04

Please sign in to comment.