Skip to content
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

per-transaction timeout interval setting #441

Open
wants to merge 1 commit 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 db.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
//
type DbMap struct {
ctx context.Context
// TimeOutInterval is used to set the timeout interval for transactions based on DbMap
TimeOutInterval time.Duration

// Db handle to use with this map
Db *sql.DB
Expand Down
48 changes: 48 additions & 0 deletions gorp.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,34 @@ func extractExecutorAndContext(e SqlExecutor) (executor, context.Context) {
return nil, nil
}

func extractTimeOutInterval(e SqlExecutor) time.Duration {
switch m := e.(type) {
case *DbMap:
return m.TimeOutInterval
case *Transaction:
return m.TimeOutInterval
}
return 0
}

func createNewContext(ctx context.Context, duration time.Duration) (context.Context, context.CancelFunc) {
if ctx == nil {
if duration == 0 {
return nil, nil
} else {
return context.WithTimeout(context.Background(), duration)
}

} else {
if duration == 0 {
return ctx, nil
} else {
return context.WithTimeout(ctx, duration)
}
}

}

// maybeExpandNamedQuery checks the given arg to see if it's eligible to be used
// as input to a named query. If so, it rewrites the query to use
// dialect-dependent bindvars and instantiates the corresponding slice of
Expand Down Expand Up @@ -625,6 +653,11 @@ func insert(m *DbMap, exec SqlExecutor, list ...interface{}) error {

func exec(e SqlExecutor, query string, args ...interface{}) (sql.Result, error) {
executor, ctx := extractExecutorAndContext(e)
timeout := extractTimeOutInterval(e)
ctx, cancel := createNewContext(ctx, timeout)
if cancel != nil {
defer cancel()
}

if ctx != nil {
return executor.ExecContext(ctx, query, args...)
Expand All @@ -635,6 +668,11 @@ func exec(e SqlExecutor, query string, args ...interface{}) (sql.Result, error)

func prepare(e SqlExecutor, query string) (*sql.Stmt, error) {
executor, ctx := extractExecutorAndContext(e)
timeout := extractTimeOutInterval(e)
ctx, cancel := createNewContext(ctx, timeout)
if cancel != nil {
defer cancel()
}

if ctx != nil {
return executor.PrepareContext(ctx, query)
Expand All @@ -645,6 +683,11 @@ func prepare(e SqlExecutor, query string) (*sql.Stmt, error) {

func queryRow(e SqlExecutor, query string, args ...interface{}) *sql.Row {
executor, ctx := extractExecutorAndContext(e)
timeout := extractTimeOutInterval(e)
ctx, cancel := createNewContext(ctx, timeout)
if cancel != nil {
defer cancel()
}

if ctx != nil {
return executor.QueryRowContext(ctx, query, args...)
Expand All @@ -655,6 +698,11 @@ func queryRow(e SqlExecutor, query string, args ...interface{}) *sql.Row {

func query(e SqlExecutor, query string, args ...interface{}) (*sql.Rows, error) {
executor, ctx := extractExecutorAndContext(e)
timeout := extractTimeOutInterval(e)
ctx, cancel := createNewContext(ctx, timeout)
if cancel != nil {
defer cancel()
}

if ctx != nil {
return executor.QueryContext(ctx, query, args...)
Expand Down
9 changes: 5 additions & 4 deletions transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ import (
// of that transaction. Transactions should be terminated with
// a call to Commit() or Rollback()
type Transaction struct {
ctx context.Context
dbmap *DbMap
tx *sql.Tx
closed bool
ctx context.Context
TimeOutInterval time.Duration
dbmap *DbMap
tx *sql.Tx
closed bool
}

func (t *Transaction) WithContext(ctx context.Context) SqlExecutor {
Expand Down