diff --git a/global/context.go b/global/context.go index 7545f40..b6dd348 100644 --- a/global/context.go +++ b/global/context.go @@ -5,6 +5,12 @@ import ( "time" ) -func TimeoutContext(timeout time.Duration) (context.Context, context.CancelFunc) { +// 生成一个超时时间为5秒的倍率的上下文,如果不传递任何值,默认生成6倍的上下文,即超时时间为30秒。 +func TimeoutContext(multiply ...int64) (context.Context, context.CancelFunc) { + var ratio int64 = 6 + if len(multiply) > 0 { + ratio = multiply[0] + } + timeout := time.Duration(ratio*5) * time.Second return context.WithTimeout(context.Background(), timeout) } diff --git a/service/region.go b/service/region.go index e3b935a..39a1bc6 100644 --- a/service/region.go +++ b/service/region.go @@ -6,7 +6,6 @@ import ( "electricity_bill_calc/logger" "electricity_bill_calc/model" "fmt" - "time" "github.com/samber/lo" "go.uber.org/zap" @@ -21,7 +20,7 @@ var RegionService = _RegionService{ } func (_RegionService) FetchSubRegions(parent string) ([]model.Region, error) { - ctx, cancel := global.TimeoutContext(30 * time.Second) + ctx, cancel := global.TimeoutContext() defer cancel() if regions, _ := cache.RetreiveSearch[[]model.Region]("region", "parent", parent); regions != nil { return *regions, nil @@ -56,7 +55,7 @@ func (r _RegionService) FetchAllParentRegions(code string) ([]model.Region, erro } func (_RegionService) fetchRegion(code string) (*model.Region, error) { - ctx, cancel := global.TimeoutContext(30 * time.Second) + ctx, cancel := global.TimeoutContext() defer cancel() if cachedRegion, _ := cache.RetreiveSearch[model.Region]("region", code); cachedRegion != nil { return cachedRegion, nil diff --git a/service/user.go b/service/user.go index 8e963ce..de60b65 100644 --- a/service/user.go +++ b/service/user.go @@ -107,7 +107,7 @@ func (u _UserService) InvalidUserPassword(uid string) (string, error) { if user == nil && err != nil { return "", exceptions.NewNotFoundError("指定的用户不存在。") } - ctx, cancel := global.TimeoutContext(30 * time.Second) + ctx, cancel := global.TimeoutContext() defer cancel() verifyCode := tools.RandStr(10) user.Password = utils.Sha512Hex(verifyCode) @@ -143,7 +143,7 @@ func (u _UserService) ResetUserPassword(username, password string) (bool, error) if user == nil || err != nil { return false, exceptions.NewNotFoundError("指定的用户不存在。") } - ctx, cancel := global.TimeoutContext(30 * time.Second) + ctx, cancel := global.TimeoutContext() defer cancel() user.Password = utils.Sha512Hex(password) user.ResetNeeded = false @@ -163,7 +163,7 @@ func (_UserService) IsUserExists(uid string) (bool, error) { if has, _ := cache.CheckExists("user", uid); has { return has, nil } - ctx, cancel := global.TimeoutContext(30 * time.Second) + ctx, cancel := global.TimeoutContext() defer cancel() has, err := global.DB.NewSelect().Model((*model.User)(nil)).Where("id = ?", uid).Exists(ctx) if has { @@ -176,7 +176,7 @@ func (_UserService) IsUsernameExists(username string) (bool, error) { if has, _ := cache.CheckExists("user", username); has { return has, nil } - ctx, cancel := global.TimeoutContext(30 * time.Second) + ctx, cancel := global.TimeoutContext() defer cancel() has, err := global.DB.NewSelect().Model((*model.User)(nil)).Where("username = ?", username).Exists(ctx) if has { @@ -206,7 +206,7 @@ func (u _UserService) CreateUser(user *model.User, detail *model.UserDetail) (st finalAbbr := tools.PinyinAbbr(*detail.Name) detail.Abbr = &finalAbbr } - ctx, cancel := global.TimeoutContext(30 * time.Second) + ctx, cancel := global.TimeoutContext() defer cancel() tx, err := global.DB.BeginTx(ctx, &sql.TxOptions{}) @@ -243,7 +243,7 @@ func (u _UserService) SwitchUserState(uid string, enabled bool) error { } newStateUser := new(model.User) newStateUser.Enabled = enabled - ctx, cancel := global.TimeoutContext(30 * time.Second) + ctx, cancel := global.TimeoutContext() defer cancel() res, err := global.DB.NewUpdate().Model(newStateUser).WherePK().Column("enabled").Exec(ctx) if affected, _ := res.RowsAffected(); err == nil && affected > 0 { @@ -257,7 +257,7 @@ func (_UserService) SearchLimitUsers(keyword string, limit int) ([]model.JoinedU return *cachedUsers, nil } - ctx, cancel := global.TimeoutContext(30 * time.Second) + ctx, cancel := global.TimeoutContext() defer cancel() var users = make([]model.User, 0) @@ -298,7 +298,7 @@ func (_UserService) findUserWithCredentialsByUsername(username string) (*model.U if cachedUser, _ := cache.RetreiveSearch[model.UserWithCredentials]("user_with_credentials", username); cachedUser != nil { return cachedUser, nil } - ctx, cancel := global.TimeoutContext(30 * time.Second) + ctx, cancel := global.TimeoutContext() defer cancel() user := new(model.UserWithCredentials) err := global.DB.NewSelect().Model(&user).Where("username = ?", username).Scan(ctx) @@ -312,7 +312,7 @@ func (_UserService) findUserByUsername(username string) (*model.User, error) { if cachedUser, _ := cache.RetreiveSearch[model.User]("user", username); cachedUser != nil { return cachedUser, nil } - ctx, cancel := global.TimeoutContext(30 * time.Second) + ctx, cancel := global.TimeoutContext() defer cancel() user := new(model.User) err := global.DB.NewSelect().Model(&user).Where("username = ?", username).Scan(ctx) @@ -326,7 +326,7 @@ func (_UserService) retreiveUserDetail(uid string) (*model.UserDetail, error) { if cachedUser, _ := cache.RetreiveEntity[model.UserDetail]("user_detail", uid); cachedUser != nil { return cachedUser, nil } - ctx, cancel := global.TimeoutContext(30 * time.Second) + ctx, cancel := global.TimeoutContext() defer cancel() user := &model.UserDetail{ Id: uid, @@ -343,7 +343,7 @@ func (_UserService) findUserByID(uid string) (*model.User, error) { if cachedUser != nil { return cachedUser, nil } - ctx, cancel := global.TimeoutContext(30 * time.Second) + ctx, cancel := global.TimeoutContext() defer cancel() user := &model.User{ Id: uid, @@ -389,7 +389,7 @@ func (_UserService) ListUserDetail(keyword string, userType int, userState *bool } } - ctx, cancel := global.TimeoutContext(30 * time.Second) + ctx, cancel := global.TimeoutContext() defer cancel() total, err := cond. Limit(config.ServiceSettings.ItemsPageSize).Offset(startItem). @@ -420,7 +420,7 @@ func (_UserService) FetchUserDetail(uid string) (*model.FullJoinedUserDetail, er return cachedUser, nil } - ctx, cancel := global.TimeoutContext(30 * time.Second) + ctx, cancel := global.TimeoutContext() defer cancel() user := &model.User{} err := global.DB.NewSelect().Model(&user).Relation("Detail").