1
0
mirror of https://github.com/haiwen/seafile-server.git synced 2025-09-13 22:01:37 +00:00

Add timeout when query from database (#685)

* Add timeout when query from database

* Modify DefaultTimeout to DBOpTimeout

---------

Co-authored-by: 杨赫然 <heran.yang@seafile.com>
This commit is contained in:
feiniks
2024-08-29 15:10:55 +08:00
committed by GitHub
parent 4f450ea8e0
commit 6944257cc8
8 changed files with 162 additions and 70 deletions

View File

@@ -1852,12 +1852,14 @@ func updateBranch(repoID, newCommitID, oldCommitID, secondParentID string) error
sqlStr = "SELECT commit_id FROM Branch WHERE name = ? AND repo_id = ?"
}
trans, err := seafileDB.Begin()
ctx, cancel := context.WithTimeout(context.Background(), option.DBOpTimeout)
defer cancel()
trans, err := seafileDB.BeginTx(ctx, nil)
if err != nil {
err := fmt.Errorf("failed to start transaction: %v", err)
return err
}
row := trans.QueryRow(sqlStr, name, repoID)
row := trans.QueryRowContext(ctx, sqlStr, name, repoID)
if err := row.Scan(&commitID); err != nil {
if err != sql.ErrNoRows {
trans.Rollback()
@@ -1871,7 +1873,7 @@ func updateBranch(repoID, newCommitID, oldCommitID, secondParentID string) error
}
sqlStr = "UPDATE Branch SET commit_id = ? WHERE name = ? AND repo_id = ?"
_, err = trans.Exec(sqlStr, newCommitID, name, repoID)
_, err = trans.ExecContext(ctx, sqlStr, newCommitID, name, repoID)
if err != nil {
trans.Rollback()
return err