1
0
mirror of https://github.com/haiwen/seafile-server.git synced 2025-09-01 15:36: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

@@ -3,12 +3,14 @@
package share
import (
"context"
"database/sql"
"fmt"
"path/filepath"
"strconv"
"strings"
"github.com/haiwen/seafile-server/fileserver/option"
"github.com/haiwen/seafile-server/fileserver/repomgr"
log "github.com/sirupsen/logrus"
)
@@ -70,7 +72,9 @@ func checkVirtualRepoPerm(repoID, originRepoID, user, vPath string) string {
}
func getUserGroups(sqlStr string, args ...interface{}) ([]group, error) {
rows, err := ccnetDB.Query(sqlStr, args...)
ctx, cancel := context.WithTimeout(context.Background(), option.DBOpTimeout)
defer cancel()
rows, err := ccnetDB.QueryContext(ctx, sqlStr, args...)
if err != nil {
return nil, err
}
@@ -148,7 +152,9 @@ func getGroupsByUser(userName string, returnAncestors bool) ([]group, error) {
func getGroupPaths(sqlStr string) (string, error) {
var paths string
rows, err := ccnetDB.Query(sqlStr)
ctx, cancel := context.WithTimeout(context.Background(), option.DBOpTimeout)
defer cancel()
rows, err := ccnetDB.QueryContext(ctx, sqlStr)
if err != nil {
return paths, err
}
@@ -190,7 +196,9 @@ func checkGroupPermByUser(repoID string, userName string) (string, error) {
}
sqlBuilder.WriteString(")")
rows, err := seafileDB.Query(sqlBuilder.String(), repoID)
ctx, cancel := context.WithTimeout(context.Background(), option.DBOpTimeout)
defer cancel()
rows, err := seafileDB.QueryContext(ctx, sqlBuilder.String(), repoID)
if err != nil {
err := fmt.Errorf("Failed to get group permission by user %s: %v", userName, err)
return "", err
@@ -220,7 +228,9 @@ func checkGroupPermByUser(repoID string, userName string) (string, error) {
func checkSharedRepoPerm(repoID string, email string) (string, error) {
sqlStr := "SELECT permission FROM SharedRepo WHERE repo_id=? AND to_email=?"
row := seafileDB.QueryRow(sqlStr, repoID, email)
ctx, cancel := context.WithTimeout(context.Background(), option.DBOpTimeout)
defer cancel()
row := seafileDB.QueryRowContext(ctx, sqlStr, repoID, email)
var perm string
if err := row.Scan(&perm); err != nil {
@@ -234,7 +244,9 @@ func checkSharedRepoPerm(repoID string, email string) (string, error) {
func checkInnerPubRepoPerm(repoID string) (string, error) {
sqlStr := "SELECT permission FROM InnerPubRepo WHERE repo_id=?"
row := seafileDB.QueryRow(sqlStr, repoID)
ctx, cancel := context.WithTimeout(context.Background(), option.DBOpTimeout)
defer cancel()
row := seafileDB.QueryRowContext(ctx, sqlStr, repoID)
var perm string
if err := row.Scan(&perm); err != nil {
@@ -286,7 +298,9 @@ func getSharedDirsToUser(originRepoID string, toEmail string) (map[string]string
sqlStr := "SELECT v.path, s.permission FROM SharedRepo s, VirtualRepo v WHERE " +
"s.repo_id = v.repo_id AND s.to_email = ? AND v.origin_repo = ?"
rows, err := seafileDB.Query(sqlStr, toEmail, originRepoID)
ctx, cancel := context.WithTimeout(context.Background(), option.DBOpTimeout)
defer cancel()
rows, err := seafileDB.QueryContext(ctx, sqlStr, toEmail, originRepoID)
if err != nil {
err := fmt.Errorf("Failed to get shared directories by user %s: %v", toEmail, err)
return nil, err
@@ -342,7 +356,9 @@ func getSharedDirsToGroup(originRepoID string, groups []group) (map[string]strin
"s.repo_id = v.repo_id AND v.origin_repo = ? "+
"AND s.group_id in (%s)", groupIDs)
rows, err := seafileDB.Query(sqlStr, originRepoID)
ctx, cancel := context.WithTimeout(context.Background(), option.DBOpTimeout)
defer cancel()
rows, err := seafileDB.QueryContext(ctx, sqlStr, originRepoID)
if err != nil {
err := fmt.Errorf("Failed to get shared directories: %v", err)
return nil, err
@@ -428,13 +444,15 @@ func GetReposByOwner(email string) ([]*SharedRepo, error) {
"v.repo_id IS NULL " +
"ORDER BY i.update_time DESC, o.repo_id"
stmt, err := seafileDB.Prepare(query)
ctx, cancel := context.WithTimeout(context.Background(), option.DBOpTimeout)
defer cancel()
stmt, err := seafileDB.PrepareContext(ctx, query)
if err != nil {
return nil, err
}
defer stmt.Close()
rows, err := stmt.Query(email)
rows, err := stmt.QueryContext(ctx, email)
if err != nil {
return nil, err
@@ -483,13 +501,15 @@ func ListInnerPubRepos() ([]*SharedRepo, error) {
"WHERE InnerPubRepo.repo_id=RepoOwner.repo_id AND " +
"InnerPubRepo.repo_id = Branch.repo_id AND Branch.name = 'master'"
stmt, err := seafileDB.Prepare(query)
ctx, cancel := context.WithTimeout(context.Background(), option.DBOpTimeout)
defer cancel()
stmt, err := seafileDB.PrepareContext(ctx, query)
if err != nil {
return nil, err
}
defer stmt.Close()
rows, err := stmt.Query()
rows, err := stmt.QueryContext(ctx)
if err != nil {
return nil, err
}
@@ -552,14 +572,16 @@ func ListShareRepos(email, columnType string) ([]*SharedRepo, error) {
return nil, err
}
stmt, err := seafileDB.Prepare(query)
ctx, cancel := context.WithTimeout(context.Background(), option.DBOpTimeout)
defer cancel()
stmt, err := seafileDB.PrepareContext(ctx, query)
if err != nil {
return nil, err
}
defer stmt.Close()
rows, err := stmt.Query(email)
rows, err := stmt.QueryContext(ctx, email)
if err != nil {
return nil, err
}
@@ -632,7 +654,9 @@ func GetGroupReposByUser(user string, orgID int) ([]*SharedRepo, error) {
}
sqlBuilder.WriteString(" ) ORDER BY group_id")
rows, err := seafileDB.Query(sqlBuilder.String())
ctx, cancel := context.WithTimeout(context.Background(), option.DBOpTimeout)
defer cancel()
rows, err := seafileDB.QueryContext(ctx, sqlBuilder.String())
if err != nil {
return nil, err
}