refactor(context):改进超时上下文的生成。
This commit is contained in:
parent
92e8d312dd
commit
cb2908435a
|
@ -5,6 +5,12 @@ import (
|
||||||
"time"
|
"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)
|
return context.WithTimeout(context.Background(), timeout)
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,6 @@ import (
|
||||||
"electricity_bill_calc/logger"
|
"electricity_bill_calc/logger"
|
||||||
"electricity_bill_calc/model"
|
"electricity_bill_calc/model"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/samber/lo"
|
"github.com/samber/lo"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
@ -21,7 +20,7 @@ var RegionService = _RegionService{
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_RegionService) FetchSubRegions(parent string) ([]model.Region, error) {
|
func (_RegionService) FetchSubRegions(parent string) ([]model.Region, error) {
|
||||||
ctx, cancel := global.TimeoutContext(30 * time.Second)
|
ctx, cancel := global.TimeoutContext()
|
||||||
defer cancel()
|
defer cancel()
|
||||||
if regions, _ := cache.RetreiveSearch[[]model.Region]("region", "parent", parent); regions != nil {
|
if regions, _ := cache.RetreiveSearch[[]model.Region]("region", "parent", parent); regions != nil {
|
||||||
return *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) {
|
func (_RegionService) fetchRegion(code string) (*model.Region, error) {
|
||||||
ctx, cancel := global.TimeoutContext(30 * time.Second)
|
ctx, cancel := global.TimeoutContext()
|
||||||
defer cancel()
|
defer cancel()
|
||||||
if cachedRegion, _ := cache.RetreiveSearch[model.Region]("region", code); cachedRegion != nil {
|
if cachedRegion, _ := cache.RetreiveSearch[model.Region]("region", code); cachedRegion != nil {
|
||||||
return cachedRegion, nil
|
return cachedRegion, nil
|
||||||
|
|
|
@ -107,7 +107,7 @@ func (u _UserService) InvalidUserPassword(uid string) (string, error) {
|
||||||
if user == nil && err != nil {
|
if user == nil && err != nil {
|
||||||
return "", exceptions.NewNotFoundError("指定的用户不存在。")
|
return "", exceptions.NewNotFoundError("指定的用户不存在。")
|
||||||
}
|
}
|
||||||
ctx, cancel := global.TimeoutContext(30 * time.Second)
|
ctx, cancel := global.TimeoutContext()
|
||||||
defer cancel()
|
defer cancel()
|
||||||
verifyCode := tools.RandStr(10)
|
verifyCode := tools.RandStr(10)
|
||||||
user.Password = utils.Sha512Hex(verifyCode)
|
user.Password = utils.Sha512Hex(verifyCode)
|
||||||
|
@ -143,7 +143,7 @@ func (u _UserService) ResetUserPassword(username, password string) (bool, error)
|
||||||
if user == nil || err != nil {
|
if user == nil || err != nil {
|
||||||
return false, exceptions.NewNotFoundError("指定的用户不存在。")
|
return false, exceptions.NewNotFoundError("指定的用户不存在。")
|
||||||
}
|
}
|
||||||
ctx, cancel := global.TimeoutContext(30 * time.Second)
|
ctx, cancel := global.TimeoutContext()
|
||||||
defer cancel()
|
defer cancel()
|
||||||
user.Password = utils.Sha512Hex(password)
|
user.Password = utils.Sha512Hex(password)
|
||||||
user.ResetNeeded = false
|
user.ResetNeeded = false
|
||||||
|
@ -163,7 +163,7 @@ func (_UserService) IsUserExists(uid string) (bool, error) {
|
||||||
if has, _ := cache.CheckExists("user", uid); has {
|
if has, _ := cache.CheckExists("user", uid); has {
|
||||||
return has, nil
|
return has, nil
|
||||||
}
|
}
|
||||||
ctx, cancel := global.TimeoutContext(30 * time.Second)
|
ctx, cancel := global.TimeoutContext()
|
||||||
defer cancel()
|
defer cancel()
|
||||||
has, err := global.DB.NewSelect().Model((*model.User)(nil)).Where("id = ?", uid).Exists(ctx)
|
has, err := global.DB.NewSelect().Model((*model.User)(nil)).Where("id = ?", uid).Exists(ctx)
|
||||||
if has {
|
if has {
|
||||||
|
@ -176,7 +176,7 @@ func (_UserService) IsUsernameExists(username string) (bool, error) {
|
||||||
if has, _ := cache.CheckExists("user", username); has {
|
if has, _ := cache.CheckExists("user", username); has {
|
||||||
return has, nil
|
return has, nil
|
||||||
}
|
}
|
||||||
ctx, cancel := global.TimeoutContext(30 * time.Second)
|
ctx, cancel := global.TimeoutContext()
|
||||||
defer cancel()
|
defer cancel()
|
||||||
has, err := global.DB.NewSelect().Model((*model.User)(nil)).Where("username = ?", username).Exists(ctx)
|
has, err := global.DB.NewSelect().Model((*model.User)(nil)).Where("username = ?", username).Exists(ctx)
|
||||||
if has {
|
if has {
|
||||||
|
@ -206,7 +206,7 @@ func (u _UserService) CreateUser(user *model.User, detail *model.UserDetail) (st
|
||||||
finalAbbr := tools.PinyinAbbr(*detail.Name)
|
finalAbbr := tools.PinyinAbbr(*detail.Name)
|
||||||
detail.Abbr = &finalAbbr
|
detail.Abbr = &finalAbbr
|
||||||
}
|
}
|
||||||
ctx, cancel := global.TimeoutContext(30 * time.Second)
|
ctx, cancel := global.TimeoutContext()
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
tx, err := global.DB.BeginTx(ctx, &sql.TxOptions{})
|
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 := new(model.User)
|
||||||
newStateUser.Enabled = enabled
|
newStateUser.Enabled = enabled
|
||||||
ctx, cancel := global.TimeoutContext(30 * time.Second)
|
ctx, cancel := global.TimeoutContext()
|
||||||
defer cancel()
|
defer cancel()
|
||||||
res, err := global.DB.NewUpdate().Model(newStateUser).WherePK().Column("enabled").Exec(ctx)
|
res, err := global.DB.NewUpdate().Model(newStateUser).WherePK().Column("enabled").Exec(ctx)
|
||||||
if affected, _ := res.RowsAffected(); err == nil && affected > 0 {
|
if affected, _ := res.RowsAffected(); err == nil && affected > 0 {
|
||||||
|
@ -257,7 +257,7 @@ func (_UserService) SearchLimitUsers(keyword string, limit int) ([]model.JoinedU
|
||||||
return *cachedUsers, nil
|
return *cachedUsers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := global.TimeoutContext(30 * time.Second)
|
ctx, cancel := global.TimeoutContext()
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
var users = make([]model.User, 0)
|
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 {
|
if cachedUser, _ := cache.RetreiveSearch[model.UserWithCredentials]("user_with_credentials", username); cachedUser != nil {
|
||||||
return cachedUser, nil
|
return cachedUser, nil
|
||||||
}
|
}
|
||||||
ctx, cancel := global.TimeoutContext(30 * time.Second)
|
ctx, cancel := global.TimeoutContext()
|
||||||
defer cancel()
|
defer cancel()
|
||||||
user := new(model.UserWithCredentials)
|
user := new(model.UserWithCredentials)
|
||||||
err := global.DB.NewSelect().Model(&user).Where("username = ?", username).Scan(ctx)
|
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 {
|
if cachedUser, _ := cache.RetreiveSearch[model.User]("user", username); cachedUser != nil {
|
||||||
return cachedUser, nil
|
return cachedUser, nil
|
||||||
}
|
}
|
||||||
ctx, cancel := global.TimeoutContext(30 * time.Second)
|
ctx, cancel := global.TimeoutContext()
|
||||||
defer cancel()
|
defer cancel()
|
||||||
user := new(model.User)
|
user := new(model.User)
|
||||||
err := global.DB.NewSelect().Model(&user).Where("username = ?", username).Scan(ctx)
|
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 {
|
if cachedUser, _ := cache.RetreiveEntity[model.UserDetail]("user_detail", uid); cachedUser != nil {
|
||||||
return cachedUser, nil
|
return cachedUser, nil
|
||||||
}
|
}
|
||||||
ctx, cancel := global.TimeoutContext(30 * time.Second)
|
ctx, cancel := global.TimeoutContext()
|
||||||
defer cancel()
|
defer cancel()
|
||||||
user := &model.UserDetail{
|
user := &model.UserDetail{
|
||||||
Id: uid,
|
Id: uid,
|
||||||
|
@ -343,7 +343,7 @@ func (_UserService) findUserByID(uid string) (*model.User, error) {
|
||||||
if cachedUser != nil {
|
if cachedUser != nil {
|
||||||
return cachedUser, nil
|
return cachedUser, nil
|
||||||
}
|
}
|
||||||
ctx, cancel := global.TimeoutContext(30 * time.Second)
|
ctx, cancel := global.TimeoutContext()
|
||||||
defer cancel()
|
defer cancel()
|
||||||
user := &model.User{
|
user := &model.User{
|
||||||
Id: uid,
|
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()
|
defer cancel()
|
||||||
total, err := cond.
|
total, err := cond.
|
||||||
Limit(config.ServiceSettings.ItemsPageSize).Offset(startItem).
|
Limit(config.ServiceSettings.ItemsPageSize).Offset(startItem).
|
||||||
|
@ -420,7 +420,7 @@ func (_UserService) FetchUserDetail(uid string) (*model.FullJoinedUserDetail, er
|
||||||
return cachedUser, nil
|
return cachedUser, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := global.TimeoutContext(30 * time.Second)
|
ctx, cancel := global.TimeoutContext()
|
||||||
defer cancel()
|
defer cancel()
|
||||||
user := &model.User{}
|
user := &model.User{}
|
||||||
err := global.DB.NewSelect().Model(&user).Relation("Detail").
|
err := global.DB.NewSelect().Model(&user).Relation("Detail").
|
||||||
|
|
Loading…
Reference in New Issue
Block a user