fusenapi/shared/shared_state.go

49 lines
787 B
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"
"log"
"github.com/lni/dragonboat/v4"
)
type SharedState struct {
shardID uint64
replicaID uint64
nh *dragonboat.NodeHost
}
func (ss *SharedState) GetUserState(Userid int64) (us *UserState, err error) {
ius, err := ss.nh.SyncRead(context.TODO(), ss.shardID, Userid)
if err != nil {
log.Println(err)
return nil, err
}
if ius != nil {
return ius.(*UserState), nil
}
cs := ss.nh.GetNoOPSession(128)
cmd := &CmdUpdate{UserId: Userid}
cmdBuf, err := FsPasser.PackToBytes(cmd)
if err != nil {
return nil, err
}
result, err := ss.nh.SyncPropose(context.TODO(), cs, cmdBuf)
if err != nil {
return nil, err
}
us = &UserState{}
err = us.Decode(result.Data)
if err != nil {
return nil, err
}
return us, err
}