1 Star 0 Fork 0

go-better/go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
tcc_action.go 1.44 KB
一键复制 编辑 原始数据 按行查看 历史
bughou 提交于 2022-03-21 22:24 +08:00 . save
package tcc
import (
"database/sql"
"encoding/json"
"fmt"
"time"
)
type tccAction struct {
Name string `json:",omitempty"`
Raw json.RawMessage
Status string `json:",omitempty"`
}
func marshalAction(action Action) ([]byte, error) {
actionJson, err := json.Marshal(action)
if err != nil {
return nil, err
}
return json.Marshal(tccAction{
Name: action.Name(),
Raw: json.RawMessage(actionJson),
})
}
func (ta tccAction) confirm(tcc *TCC, tx *sql.Tx, actionIndex int) (time.Duration, bool, error) {
action, err := tcc.engine.unmarshalAction(ta.Name, ta.Raw)
if err != nil {
return time.Hour, true, err
}
if err := action.Confirm(); err != nil {
return 0, true, err
}
return setActionStatus(tcc, tx, actionIndex, statusConfirmed, "confirm action")
}
func (ta tccAction) cancel(tcc *TCC, tx *sql.Tx, actionIndex int) (time.Duration, bool, error) {
action, err := tcc.engine.unmarshalAction(ta.Name, ta.Raw)
if err != nil {
return time.Hour, true, err
}
if err := action.Cancel(); err != nil {
return 0, true, err
}
return setActionStatus(tcc, tx, actionIndex, statusCanceled, "cancel action")
}
func setActionStatus(
tcc *TCC, tx *sql.Tx, actionIndex int, status, method string,
) (time.Duration, bool, error) {
setSql := fmt.Sprintf(
`data = jsonb_set(data, '{Actions,%d,Status}'::text[], to_jsonb('%s'::text))`,
actionIndex, status,
)
canCommit, err := tcc.update(setSql, status, method, tx)
return 0, canCommit, err
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/go-better/go.git
git@gitee.com:go-better/go.git
go-better
go
go
d31700df43a9

搜索帮助