Skip to content

Commit 7ceb0d9

Browse files
authored
Add DefaultContextTimeout option (#7567)
1 parent 4e34a6d commit 7ceb0d9

4 files changed

Lines changed: 13 additions & 6 deletions

File tree

callbacks.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,16 @@ func (p *processor) Execute(db *DB) *DB {
8989
resetBuildClauses = true
9090
}
9191

92-
if optimizer, ok := db.Statement.Dest.(StatementModifier); ok {
92+
if optimizer, ok := stmt.Dest.(StatementModifier); ok {
9393
optimizer.ModifyStatement(stmt)
9494
}
9595

96+
if db.DefaultContextTimeout > 0 {
97+
if _, ok := stmt.Context.Deadline(); !ok {
98+
stmt.Context, _ = context.WithTimeout(stmt.Context, db.DefaultContextTimeout)
99+
}
100+
}
101+
96102
// assign model values
97103
if stmt.Model == nil {
98104
stmt.Model = stmt.Dest

finisher_api.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -675,9 +675,9 @@ func (db *DB) Begin(opts ...*sql.TxOptions) *DB {
675675
}
676676

677677
ctx := tx.Statement.Context
678-
if _, ok := ctx.Deadline(); !ok {
679-
if db.Config.DefaultTransactionTimeout > 0 {
680-
ctx, _ = context.WithTimeout(ctx, db.Config.DefaultTransactionTimeout)
678+
if db.DefaultTransactionTimeout > 0 {
679+
if _, ok := ctx.Deadline(); !ok {
680+
ctx, _ = context.WithTimeout(ctx, db.DefaultTransactionTimeout)
681681
}
682682
}
683683

gorm.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type Config struct {
2323
// You can disable it by setting `SkipDefaultTransaction` to true
2424
SkipDefaultTransaction bool
2525
DefaultTransactionTimeout time.Duration
26+
DefaultContextTimeout time.Duration
2627

2728
// NamingStrategy tables, columns naming strategy
2829
NamingStrategy schema.Namer

tests/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/google/uuid v1.6.0
77
github.com/jinzhu/now v1.1.5
88
github.com/lib/pq v1.10.9
9-
github.com/stretchr/testify v1.10.0
9+
github.com/stretchr/testify v1.11.0
1010
gorm.io/driver/gaussdb v0.1.0
1111
gorm.io/driver/mysql v1.6.0
1212
gorm.io/driver/postgres v1.6.0
@@ -28,7 +28,7 @@ require (
2828
github.com/jackc/puddle/v2 v2.2.2 // indirect
2929
github.com/jinzhu/inflection v1.0.0 // indirect
3030
github.com/mattn/go-sqlite3 v1.14.32 // indirect
31-
github.com/microsoft/go-mssqldb v1.9.2 // indirect
31+
github.com/microsoft/go-mssqldb v1.9.3 // indirect
3232
github.com/pmezard/go-difflib v1.0.0 // indirect
3333
github.com/tjfoc/gmsm v1.4.1 // indirect
3434
golang.org/x/crypto v0.41.0 // indirect

0 commit comments

Comments
 (0)