fusenapi/shared/sm_update_handler.go

66 lines
1.3 KiB
Go
Raw Normal View History

2023-08-09 08:54:52 +00:00
package shared
2023-08-09 08:17:24 +00:00
import (
"context"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"log"
"time"
"github.com/474420502/passer"
sm "github.com/lni/dragonboat/v4/statemachine"
)
type CmdUpdate struct {
UserId int64
}
// 上下文传递
type (
ctxSM struct{}
// ctxUserState struct{}
ctxEntry struct{}
)
// 结构体异步传递后, 执行的注册函数, 实际上就是update的handler
var FsPasser *passer.Passer[sm.Result] = func() *passer.Passer[sm.Result] {
fsPasser := passer.NewPasser[sm.Result]()
fsPasser.RegisterPasser(&CmdUpdate{}, func(ctx context.Context, obj any) (sm.Result, error) {
var result sm.Result
cmd := obj.(*CmdUpdate)
s := ctx.Value(ctxSM{}).(*FsStateMachine)
e := ctx.Value(ctxEntry{}).(*sm.Entry)
if old, ok := s.store[cmd.UserId]; ok {
if time.Since(old.UpdateAt) <= time.Second {
return result, nil
}
}
// log.Println("update")
models := gmodel.NewAllModels(s.gdb)
user, err := models.FsUser.FindUserById(context.TODO(), cmd.UserId)
if err != nil {
log.Println(err)
}
userState := &UserState{
UserId: cmd.UserId,
PwdHash: auth.StringToHash(*user.PasswordHash),
2023-09-04 08:59:43 +00:00
UpdateAt: time.Now().UTC(),
2023-08-09 08:17:24 +00:00
}
s.store[cmd.UserId] = userState
err = userState.Encode(func(b []byte) error {
e.Result.Data = b
result.Data = b
return nil
})
return result, err
})
return fsPasser
}()