tcc

package
v0.0.0-...-d31700d Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2022 License: MIT Imports: 11 Imported by: 0

README

tcc

distributed transaction: try, confirm, cancel.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action interface {
	Name() string
	Try() error
	Confirm() error
	Cancel() error
}

type Engine

type Engine struct {
	// contains filtered or unexported fields
}
Example (Handle)
fmt.Println(tccEngine.handle(nil, nil, &sqlmq.StdMessage{
	Data: []byte{},
}))
fmt.Println(tccEngine.handle(nil, nil, &sqlmq.StdMessage{
	Data: []byte(`{"Status": "confirmed", "Actions":[{"Name":"test-action"}]}`),
}))
fmt.Println(tccEngine.handle(nil, nil, &sqlmq.StdMessage{
	Data: []byte(`{"Status": "canceled", "Actions":[{"Name":"action1","Raw":1}]}`),
}))
fmt.Println(tccEngine.handle(nil, nil, &sqlmq.StdMessage{
	Data: []byte(`{"Status": "confirmed", "Concurrent": true, "Actions":[{"Name":"test-action"}]}`),
}))
fmt.Println(tccEngine.handle(nil, nil, &sqlmq.StdMessage{
	Data: []byte(`{"Status": "canceled", "Concurrent": true, "Actions":[{"Name":"action1","Raw":1}]}`),
}))
Output:

1h0m0s true unexpected end of JSON input
1h0m0s true test-action: action not registered
1h0m0s true action1: json: cannot unmarshal number into Go value of type tcc.testAction1
1h0m0s true test-action: action not registered
1h0m0s true action1: json: cannot unmarshal number into Go value of type tcc.testAction1

func NewEngine

func NewEngine(name string, mq *sqlmq.SqlMQ) *Engine

name must be unique for the same mq.

Example
defer func() {
	fmt.Println(timePrefix.ReplaceAllString(recover().(string), ""))
}()
NewEngine("test", testMQ)
Output:

queue tcc-test already registered

func (*Engine) New

func (engine *Engine) New(timeout time.Duration, concurrent bool) (*TCC, error)
Example
tccEngine.mqName = "tcc-test2"
defer func() {
	tccEngine.mqName = "tcc-test"
}()
_, err := tccEngine.New(time.Second, true)
fmt.Println(err)
fmt.Println(tccEngine.Run(time.Second, true))
Output:

unknown queue: tcc-test2
unknown queue: tcc-test2

func (*Engine) Register

func (engine *Engine) Register(actions ...Action)
Example
defer func() {
	fmt.Println(timePrefix.ReplaceAllString(recover().(string), ""))
}()
tccEngine.Register(testAction1{})
Output:

action action1 already registered

func (*Engine) Run

func (engine *Engine) Run(timeout time.Duration, concurrent bool, actions ...Action) error

type TCC

type TCC struct {
	// contains filtered or unexported fields
}
Example (CancelRetry)
runTest(false, testAction1{}, testAction2{}, &testAction6{})
time.Sleep(4 * time.Second)
Output:

action1 Try
action2 Try
action6 Try
error happened
action6 Cancel 1
action6 Cancel 2
action2 Cancel
action1 Cancel
Example (CancelRetry_concurrently)
runTest(true, testAction1{}, testAction2{}, &testAction7{})
time.Sleep(4 * time.Second)
Output:

action1 Try
action2 Try
action7 Try
error happened
action2 Cancel
action1 Cancel
action7 Cancel 1
action7 Cancel 2
Example (ConfirmOrCancel)
tx, err := testDB.Begin()
if err != nil {
	panic(err)
}
fmt.Println((&TCC{msg: &sqlmq.StdMessage{Data: &tccData{}}}).confirmOrCancel(tx))
Output:

0s true tcc(0) is canceled, cann't Cancel
Example (ConfirmRetry)
runTest(false, testAction1{}, testAction2{}, &testAction4{})
time.Sleep(4 * time.Second)
Output:

action1 Try
action2 Try
action4 Try
action1 Confirm
action2 Confirm
action4 Confirm 1
action4 Confirm 2
Example (ConfirmRetry_concurrently)
runTest(true, testAction1{}, testAction2{}, &testAction5{})
time.Sleep(4 * time.Second)
Output:

action1 Try
action2 Try
action5 Try
action1 Confirm
action2 Confirm
action5 Confirm 1
action5 Confirm 2
Example (Fail)
runTest(false, testAction1{}, testAction3{}, testAction2{})
Output:

action1 Try
action3 Try
error happened
action3 Cancel
action1 Cancel
Example (Fail_concurrent)
runTest(true, testAction1{}, testAction3{}, testAction2{})
Output:

action1 Try
action3 Try
error happened
action3 Cancel
action1 Cancel
Example (StatusError)
tcc, err := tccEngine.New(time.Minute, false)
if err != nil {
	panic(err)
}
canCommit, err := tcc.statusError("confirm action", testDB)
fmt.Println(canCommit, tccId.ReplaceAllString(err.Error(), "tcc(1)"))

db := getDB()
db.Close()

fmt.Println(tcc.statusError("confirm action", db))
Output:

true tcc(1) is trying, cann't confirm action
false sql: database is closed
Example (Success)
runTest(false, testAction1{}, testAction2{})
Output:

action1 Try
action2 Try
action1 Confirm
action2 Confirm
Example (Success_concurrent)
runTest(true, testAction1{}, testAction2{})
Output:

action1 Try
action2 Try
action1 Confirm
action2 Confirm
Example (Update)
tcc, err := tccEngine.New(time.Minute, false)
if err != nil {
	panic(err)
}
fmt.Println(tcc.update("", statusTrying, "xx", testDB))
Output:

false pq: syntax error at or near "WHERE"

func (*TCC) Cancel

func (tcc *TCC) Cancel() error
Example
tcc, err := tccEngine.New(time.Minute, false)
if err != nil {
	panic(err)
}
fmt.Println(tcc.Confirm())
fmt.Println(tccId.ReplaceAllString(tcc.Cancel().Error(), "tcc(1)"))
Output:

<nil>
tcc(1) is confirmed, cann't Cancel

func (*TCC) Confirm

func (tcc *TCC) Confirm() error
Example
tcc, err := tccEngine.New(time.Minute, false)
if err != nil {
	panic(err)
}
fmt.Println(tcc.Confirm())
fmt.Println(tccId.ReplaceAllString(tcc.Confirm().Error(), "tcc(1)"))
Output:

<nil>
tcc(1) is confirmed, cann't Confirm

func (*TCC) Try

func (tcc *TCC) Try(action Action) error
Example
tcc, err := tccEngine.New(time.Minute, false)
if err != nil {
	panic(err)
}
fmt.Println(tcc.Try(testAction{}))
fmt.Println(tcc.Try(&testAction1{}))
fmt.Println(tcc.Try(testAction1{Data: make(chan int)}))
tcc.msg.Id = -9
fmt.Println(tcc.Try(testAction1{}))
Output:

action test-action is not registered
action action1 has been registered with type "tcc.testAction1", but tried with type "*tcc.testAction1"
json: unsupported type: chan int
tcc(-9) not exists

Jump to

Keyboard shortcuts

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