Conversation
|
Introduce DefaultContextTimeout Configuration for DB Contexts This pull request adds a new DefaultContextTimeout field to the GORM Config struct. The change enables users to configure a default timeout for DB contexts, applying a context.WithTimeout to DB operations when no deadline is set and DefaultContextTimeout is greater than zero. Several modifications were made to ensure this timeout is respected in the main query execution and transaction initiation paths. Additionally, test dependencies in go.mod were updated. Key Changes• Added Affected Areas• gorm.go (Config struct, initialization logic) This summary was automatically generated by @propel-code-bot |
| if db.DefaultContextTimeout > 0 { | ||
| if _, ok := stmt.Context.Deadline(); !ok { | ||
| stmt.Context, _ = context.WithTimeout(stmt.Context, db.DefaultContextTimeout) | ||
| } | ||
| } |
There was a problem hiding this comment.
[CriticalError]
Consider adding a safety check for nil statement context to prevent potential panic:
| if db.DefaultContextTimeout > 0 { | |
| if _, ok := stmt.Context.Deadline(); !ok { | |
| stmt.Context, _ = context.WithTimeout(stmt.Context, db.DefaultContextTimeout) | |
| } | |
| } | |
| if db.DefaultContextTimeout > 0 { | |
| if stmt.Context == nil { | |
| stmt.Context = context.Background() | |
| } | |
| if _, ok := stmt.Context.Deadline(); !ok { | |
| stmt.Context, _ = context.WithTimeout(stmt.Context, db.DefaultContextTimeout) | |
| } | |
| } |
If stmt.Context is nil when checking Deadline(), it will cause a panic. This adds a safety check to initialize with a background context if needed.
⚡ Committable suggestion
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
What did this pull request do?
User Case Description