From b7f042ceafe543937310fa1e47f2909a86dcf797 Mon Sep 17 00:00:00 2001 From: Jordan Krage Date: Tue, 14 Nov 2023 06:47:24 -0600 Subject: [PATCH] core/services/pg: simplify TxOptions (#11276) --- core/services/pg/transaction.go | 38 ++++----------------------------- core/services/pg/utils.go | 6 ------ 2 files changed, 4 insertions(+), 40 deletions(-) diff --git a/core/services/pg/transaction.go b/core/services/pg/transaction.go index d237c20d4c6..92d72b3d81b 100644 --- a/core/services/pg/transaction.go +++ b/core/services/pg/transaction.go @@ -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 @@ -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 } @@ -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) @@ -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 diff --git a/core/services/pg/utils.go b/core/services/pg/utils.go index 5be2a4915bb..eb53c261296 100644 --- a/core/services/pg/utils.go +++ b/core/services/pg/utils.go @@ -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)