dtmcli

package module
v1.14.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 11, 2022 License: BSD-3-Clause Imports: 13 Imported by: 0

README

English | 简体中文

dtmcli

dtmcli is the http client sdk for dtm, a distributed transaction framework.

The code of this library is fully synchronised with the dtmcli directory under dtm. There will be very few dependencies associated with the current dtmcli and smaller size for the final binary file.

For detailed documentation and usage, please refer to dtm

a complete sample

dtmcli-go-sample

Documentation

Index

Constants

View Source
const (
	// StatusPrepared status for global/branch trans status.
	// first step, tx preparation period
	StatusPrepared = "prepared"
	// StatusSubmitted status for global trans status.
	StatusSubmitted = "submitted"
	// StatusSucceed status for global/branch trans status.
	StatusSucceed = "succeed"
	// StatusFailed status for global/branch trans status.
	// NOTE: change global status to failed can stop trigger (Not recommended in production env)
	StatusFailed = "failed"
	// StatusAborting status for global trans status.
	StatusAborting = "aborting"

	// ResultSuccess for result of a trans/trans branch
	ResultSuccess = dtmimp.ResultSuccess
	// ResultFailure for result of a trans/trans branch
	ResultFailure = dtmimp.ResultFailure
	// ResultOngoing for result of a trans/trans branch
	ResultOngoing = dtmimp.ResultOngoing

	// DBTypeMysql const for driver mysql
	DBTypeMysql = dtmimp.DBTypeMysql
	// DBTypePostgres const for driver postgres
	DBTypePostgres = dtmimp.DBTypePostgres
)

Variables

View Source
var ErrDuplicated = dtmimp.ErrDuplicated

ErrDuplicated error of DUPLICATED for only msg if QueryPrepared executed before call. then DoAndSubmit return this error

View Source
var ErrFailure = dtmimp.ErrFailure

ErrFailure error for returned failure

View Source
var ErrOngoing = dtmimp.ErrOngoing

ErrOngoing error for returned ongoing

View Source
var MapFailure = dtmimp.MapFailure

MapFailure HTTP result of FAILURE

View Source
var MapSuccess = dtmimp.MapSuccess

MapSuccess HTTP result of SUCCESS

Functions

func GetCurrentDBType

func GetCurrentDBType() string

GetCurrentDBType get currentDBType

func GetRestyClient

func GetRestyClient() *resty.Client

GetRestyClient get the resty.Client for http request

func GetXaSQLTimeoutMs

func GetXaSQLTimeoutMs() int

GetXaSQLTimeoutMs get XaSQLTimeoutMs

func MustGenGid

func MustGenGid(server string) string

MustGenGid generate a new gid

func Result2HttpJSON

func Result2HttpJSON(result interface{}) (code int, res interface{})

Result2HttpJSON return the http code and json result if result is error, the return proper code, else return StatusOK

func SetBarrierTableName

func SetBarrierTableName(tablename string)

SetBarrierTableName sets barrier table name

func SetCurrentDBType

func SetCurrentDBType(dbType string)

SetCurrentDBType set currentDBType

func SetPassthroughHeaders

func SetPassthroughHeaders(headers []string)

SetPassthroughHeaders experimental. apply to http header and grpc metadata dtm server will save these headers in trans creating request. and then passthrough them to sub-trans

func SetXaSQLTimeoutMs

func SetXaSQLTimeoutMs(ms int)

SetXaSQLTimeoutMs set XaSQLTimeoutMs

func String2DtmError

func String2DtmError(str string) error

String2DtmError translate string to dtm error

func TccGlobalTransaction

func TccGlobalTransaction(dtm string, gid string, tccFunc TccGlobalFunc) (rerr error)

TccGlobalTransaction begin a tcc global transaction dtm dtm server address gid global transaction ID tccFunc define the detail tcc busi

func TccGlobalTransaction2

func TccGlobalTransaction2(dtm string, gid string, custom func(*Tcc), tccFunc TccGlobalFunc) (rerr error)

TccGlobalTransaction2 new version of TccGlobalTransaction, add custom param

func XaGlobalTransaction

func XaGlobalTransaction(server string, gid string, xaFunc XaGlobalFunc) error

XaGlobalTransaction start a xa global transaction

func XaGlobalTransaction2

func XaGlobalTransaction2(server string, gid string, custom func(*Xa), xaFunc XaGlobalFunc) (rerr error)

XaGlobalTransaction2 start a xa global transaction with xa custom function

func XaLocalTransaction

func XaLocalTransaction(qs url.Values, dbConf DBConf, xaFunc XaLocalFunc) error

XaLocalTransaction start a xa local transaction

Types

type BarrierBusiFunc

type BarrierBusiFunc func(tx *sql.Tx) error

BarrierBusiFunc type for busi func

type BranchBarrier

type BranchBarrier struct {
	TransType        string
	Gid              string
	BranchID         string
	Op               string
	BarrierID        int
	DBType           string // DBTypeMysql | DBTypePostgres
	BarrierTableName string
}

BranchBarrier every branch info

func BarrierFrom

func BarrierFrom(transType, gid, branchID, op string) (*BranchBarrier, error)

BarrierFrom construct transaction info from request

func BarrierFromQuery

func BarrierFromQuery(qs url.Values) (*BranchBarrier, error)

BarrierFromQuery construct transaction info from request

func (*BranchBarrier) Call

func (bb *BranchBarrier) Call(tx *sql.Tx, busiCall BarrierBusiFunc) (rerr error)

Call see detail description in https://en.dtm.pub/practice/barrier.html tx: local transaction connection busiCall: busi func

func (*BranchBarrier) CallWithDB

func (bb *BranchBarrier) CallWithDB(db *sql.DB, busiCall BarrierBusiFunc) error

CallWithDB the same as Call, but with *sql.DB

func (*BranchBarrier) MongoCall

func (bb *BranchBarrier) MongoCall(mc *mongo.Client, busiCall func(mongo.SessionContext) error) (rerr error)

MongoCall sub-trans barrier for mongo. see http://dtm.pub/practice/barrier experimental

func (*BranchBarrier) MongoQueryPrepared

func (bb *BranchBarrier) MongoQueryPrepared(mc *mongo.Client) error

MongoQueryPrepared query prepared for redis experimental

func (*BranchBarrier) QueryPrepared

func (bb *BranchBarrier) QueryPrepared(db *sql.DB) error

QueryPrepared queries prepared data

func (*BranchBarrier) RedisCheckAdjustAmount

func (bb *BranchBarrier) RedisCheckAdjustAmount(rd *redis.Client, key string, amount int, barrierExpire int) error

RedisCheckAdjustAmount check the value of key is valid and >= amount. then adjust the amount

func (*BranchBarrier) RedisQueryPrepared

func (bb *BranchBarrier) RedisQueryPrepared(rd *redis.Client, barrierExpire int) error

RedisQueryPrepared query prepared for redis

func (*BranchBarrier) String

func (bb *BranchBarrier) String() string

type DB

type DB = dtmimp.DB

DB interface

type DBConf

type DBConf = dtmimp.DBConf

DBConf declares db configuration

type Msg

type Msg struct {
	dtmimp.TransBase
	// contains filtered or unexported fields
}

Msg reliable msg type

func NewMsg

func NewMsg(server string, gid string) *Msg

NewMsg create new msg

func (*Msg) Add

func (s *Msg) Add(action string, postData interface{}) *Msg

Add add a new step

func (*Msg) BuildCustomOptions

func (s *Msg) BuildCustomOptions()

BuildCustomOptions add custom options to the request context

func (*Msg) DoAndSubmit

func (s *Msg) DoAndSubmit(queryPrepared string, busiCall func(bb *BranchBarrier) error) error

DoAndSubmit one method for the entire prepare->busi->submit the error returned by busiCall will be returned if busiCall return ErrFailure, then abort is called directly if busiCall return not nil error other than ErrFailure, then DoAndSubmit will call queryPrepared to get the result

func (*Msg) DoAndSubmitDB

func (s *Msg) DoAndSubmitDB(queryPrepared string, db *sql.DB, busiCall BarrierBusiFunc) error

DoAndSubmitDB short method for Do on db type. please see DoAndSubmit

func (*Msg) Prepare

func (s *Msg) Prepare(queryPrepared string) error

Prepare prepare the msg, msg will later be submitted

func (*Msg) SetDelay

func (s *Msg) SetDelay(delay uint64) *Msg

SetDelay delay call branch, unit second

func (*Msg) Submit

func (s *Msg) Submit() error

Submit submit the msg

type Saga

type Saga struct {
	dtmimp.TransBase
	// contains filtered or unexported fields
}

Saga struct of saga

func NewSaga

func NewSaga(server string, gid string) *Saga

NewSaga create a saga

func (*Saga) Add

func (s *Saga) Add(action string, compensate string, postData interface{}) *Saga

Add add a saga step

func (*Saga) AddBranchOrder

func (s *Saga) AddBranchOrder(branch int, preBranches []int) *Saga

AddBranchOrder specify that branch should be after preBranches. branch should is larger than all the element in preBranches

func (*Saga) BuildCustomOptions

func (s *Saga) BuildCustomOptions()

BuildCustomOptions add custom options to the request context

func (*Saga) SetConcurrent

func (s *Saga) SetConcurrent() *Saga

SetConcurrent enable the concurrent exec of sub trans

func (*Saga) Submit

func (s *Saga) Submit() error

Submit submit the saga trans

type Tcc

type Tcc struct {
	dtmimp.TransBase
}

Tcc struct of tcc

func TccFromQuery

func TccFromQuery(qs url.Values) (*Tcc, error)

TccFromQuery tcc from request info

func (*Tcc) CallBranch

func (t *Tcc) CallBranch(body interface{}, tryURL string, confirmURL string, cancelURL string) (*resty.Response, error)

CallBranch call a tcc branch

type TccGlobalFunc

type TccGlobalFunc func(tcc *Tcc) (*resty.Response, error)

TccGlobalFunc type of global tcc call

type TransOptions

type TransOptions = dtmimp.TransOptions

TransOptions transaction option

type Xa

type Xa struct {
	dtmimp.TransBase
	Phase2URL string
}

Xa xa transaction

func XaFromQuery

func XaFromQuery(qs url.Values) (*Xa, error)

XaFromQuery construct xa info from request

func (*Xa) CallBranch

func (x *Xa) CallBranch(body interface{}, url string) (*resty.Response, error)

CallBranch call a xa branch

type XaGlobalFunc

type XaGlobalFunc func(xa *Xa) (*resty.Response, error)

XaGlobalFunc type of xa global function

type XaLocalFunc

type XaLocalFunc func(db *sql.DB, xa *Xa) error

XaLocalFunc type of xa local function

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL