🔥A cross-language distributed transaction manager. Support xa, tcc, saga, transactional messages. 跨语言分布式事务管理器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

43 lines
1.1 KiB

package dtmsvr
import (
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/yedf/dtm/common"
"gorm.io/gorm/clause"
)
func AddRoute(engine *gin.Engine) {
engine.POST("/api/dtmsvr/prepare", common.WrapHandler(Prepare))
engine.POST("/api/dtmsvr/commit", common.WrapHandler(Commit))
}
func Prepare(c *gin.Context) (interface{}, error) {
db := DbGet()
m := getSagaModelFromContext(c)
m.Status = "prepared"
writeTransLog(m.Gid, "save prepared", m.Status, -1, m.Steps)
db.Must().Clauses(clause.OnConflict{
DoNothing: true,
}).Create(&m)
return M{"message": "SUCCESS"}, nil
}
func Commit(c *gin.Context) (interface{}, error) {
m := getSagaModelFromContext(c)
saveCommitedSagaModel(m)
go ProcessCommitedSaga(m.Gid)
return M{"message": "SUCCESS"}, nil
}
func getSagaModelFromContext(c *gin.Context) *SagaModel {
data := M{}
b, err := c.GetRawData()
common.PanicIfError(err)
common.MustUnmarshal(b, &data)
logrus.Printf("creating saga model in prepare")
data["steps"] = common.MustMarshalString(data["steps"])
m := SagaModel{}
common.MustRemarshal(data, &m)
return &m
}