tcc barrier seems ok

This commit is contained in:
yedongfu 2021-07-08 23:19:58 +08:00
parent b0102048a9
commit 5db1825ed6
5 changed files with 23 additions and 2 deletions

View File

@ -55,6 +55,11 @@ func main() {
dtmsvr.MainStart()
examples.PopulateMysql()
examples.SagaBarrierMainStart()
} else if os.Args[1] == "tcc_barrier" {
dtmsvr.PopulateMysql()
dtmsvr.MainStart()
examples.PopulateMysql()
examples.TccBarrierMainStart()
} else {
logrus.Fatalf("unknown arg: %s", os.Args[1])
}

View File

@ -74,7 +74,7 @@ func ThroughBarrierCall(db *sql.DB, transInfo *TransInfo, busiCall BusiFunc) (re
"compensate": "action",
}[transInfo.BranchType]
originAffected, _ := insertBarrier(tx, transInfo.TransType, transInfo.Gid, transInfo.BranchID, originType)
currentAffected, rerr := insertBarrier(tx, transInfo.TransType, transInfo.Gid, transInfo.BranchID, transInfo.TransType)
currentAffected, rerr := insertBarrier(tx, transInfo.TransType, transInfo.Gid, transInfo.BranchID, transInfo.BranchType)
if currentAffected == 0 || (originType == "cancel" || originType == "compensate") && originAffected > 0 {
res = "SUCCESS" // 如果被忽略,那么直接返回 "SUCCESS",表示成功,可以进行下一步
return

View File

@ -49,10 +49,11 @@ func TccFromReq(c *gin.Context) (*Tcc, error) {
}
func (t *Tcc) CallBranch(body interface{}, tryUrl string, confirmUrl string, cancelUrl string) (*resty.Response, error) {
branchID := common.GenGid()
resp, err := common.RestyClient.R().
SetBody(&M{
"gid": t.Gid,
"branch_id": common.GenGid(),
"branch_id": branchID,
"trans_type": "tcc",
"status": "prepared",
"data": string(common.MustMarshal(body)),
@ -66,5 +67,11 @@ func (t *Tcc) CallBranch(body interface{}, tryUrl string, confirmUrl string, can
}
return common.RestyClient.R().
SetBody(body).
SetQueryParams(common.MS{
"gid": t.Gid,
"branch_id": branchID,
"trans_type": "tcc",
"branch_type": "try",
}).
Post(tryUrl)
}

View File

@ -20,6 +20,14 @@ type UserAccount struct {
func (u *UserAccount) TableName() string { return "user_account" }
type UserAccountTrading struct {
common.ModelBase
UserId int
TradingBalance string
}
func (u *UserAccountTrading) TableName() string { return "user_account_trading" }
func dbGet() *common.DB {
return common.DbGet(Config.Mysql)
}

View File

@ -17,6 +17,7 @@ const DtmServer = "http://localhost:8080/api/dtmsvr"
const (
SagaBarrierBusiPort = iota + 8090
TccBarrierBusiPort
)
type TransReq struct {