Skip to content

Commit

Permalink
core/services/pg: simplify TxOptions (#11276)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmank88 authored Nov 14, 2023
1 parent ac94719 commit b7f042c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 40 deletions.
38 changes: 4 additions & 34 deletions core/services/pg/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,16 @@ import (
"time"

"github.com/getsentry/sentry-go"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
"go.uber.org/multierr"

"github.com/jmoiron/sqlx"

"github.com/smartcontractkit/chainlink-relay/pkg/logger"

corelogger "github.com/smartcontractkit/chainlink/v2/core/logger"
)

type TxOptions struct {
sql.TxOptions
LockTimeout time.Duration
IdleInTxSessionTimeout time.Duration
}

// NOTE: In an ideal world the timeouts below would be set to something sane in
Expand All @@ -39,27 +35,14 @@ func OptReadOnlyTx() TxOptions {
return TxOptions{TxOptions: sql.TxOptions{ReadOnly: true}}
}

func applyDefaults(optss []TxOptions) (lockTimeout, idleInTxSessionTimeout time.Duration, txOpts sql.TxOptions) {
lockTimeout = defaultLockTimeout
idleInTxSessionTimeout = defaultIdleInTxSessionTimeout
txIsolation := DefaultIsolation
func applyDefaults(optss []TxOptions) (txOpts sql.TxOptions) {
readOnly := false
if len(optss) > 0 {
opts := optss[0]
if opts.LockTimeout != 0 {
lockTimeout = opts.LockTimeout
}
if opts.IdleInTxSessionTimeout != 0 {
idleInTxSessionTimeout = opts.IdleInTxSessionTimeout
}
if opts.Isolation != 0 {
txIsolation = opts.Isolation
}
readOnly = opts.ReadOnly
}
txOpts = sql.TxOptions{
Isolation: txIsolation,
ReadOnly: readOnly,
ReadOnly: readOnly,
}
return
}
Expand All @@ -86,7 +69,7 @@ type TxBeginner interface {
}

func sqlxTransactionQ(ctx context.Context, db TxBeginner, lggr logger.Logger, fn func(q Queryer) error, optss ...TxOptions) (err error) {
lockTimeout, idleInTxSessionTimeout, txOpts := applyDefaults(optss)
txOpts := applyDefaults(optss)

var tx *sqlx.Tx
tx, err = db.BeginTxx(ctx, &txOpts)
Expand Down Expand Up @@ -126,19 +109,6 @@ func sqlxTransactionQ(ctx context.Context, db TxBeginner, lggr logger.Logger, fn
}
}()

if lockTimeout != defaultLockTimeout {
_, err = tx.Exec(fmt.Sprintf(`SET LOCAL lock_timeout = %d`, lockTimeout.Milliseconds()))
if err != nil {
return errors.Wrap(err, "error setting transaction local lock_timeout")
}
}
if idleInTxSessionTimeout != defaultIdleInTxSessionTimeout {
_, err = tx.Exec(fmt.Sprintf(`SET LOCAL idle_in_transaction_session_timeout = %d`, idleInTxSessionTimeout.Milliseconds()))
if err != nil {
return errors.Wrap(err, "error setting transaction local idle_in_transaction_session_timeout")
}
}

err = fn(tx)

return
Expand Down
6 changes: 0 additions & 6 deletions core/services/pg/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ const (
DefaultQueryTimeout = 10 * time.Second
// longQueryTimeout is a bigger upper bound for how long a SQL query should take
longQueryTimeout = 1 * time.Minute
// defaultLockTimeout controls the max time we will wait for any kind of database lock.
// It's good to set this to _something_ because waiting for locks forever is really bad.
defaultLockTimeout = 15 * time.Second
// defaultIdleInTxSessionTimeout controls the max time we leave a transaction open and idle.
// It's good to set this to _something_ because leaving transactions open forever is really bad.
defaultIdleInTxSessionTimeout = 1 * time.Hour
)

var _ driver.Valuer = Limit(-1)
Expand Down

0 comments on commit b7f042c

Please sign in to comment.