1
0
mirror of https://github.com/haiwen/seafile-server.git synced 2025-09-13 05:41:30 +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

@@ -1,9 +1,11 @@
package main
import (
"context"
"encoding/json"
"reflect"
"runtime/debug"
"time"
log "github.com/sirupsen/logrus"
)
@@ -114,14 +116,16 @@ func Notify(msg *Message) {
func getGroupMembers(group int) map[string]struct{} {
query := `SELECT user_name FROM GroupUser WHERE group_id = ?`
stmt, err := ccnetDB.Prepare(query)
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
stmt, err := ccnetDB.PrepareContext(ctx, query)
if err != nil {
log.Printf("failed to prepare sql: %s%v", query, err)
return nil
}
defer stmt.Close()
rows, err := stmt.Query(group)
rows, err := stmt.QueryContext(ctx, group)
if err != nil {
log.Printf("failed to query sql: %v", err)
return nil