forked from xiaoxuxiansheng/gotcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.go
More file actions
94 lines (77 loc) · 2.02 KB
/
model.go
File metadata and controls
94 lines (77 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package gotcc
import (
"time"
)
type RequestEntity struct {
// 组件名称
ComponentID string `json:"componentName"`
// 组件入参
Request map[string]interface{} `json:"request"`
}
type ComponentEntities []*ComponentEntity
func (c ComponentEntities) ToComponents() []TCCComponent {
components := make([]TCCComponent, 0, len(c))
for _, entity := range c {
components = append(components, entity.Component)
}
return components
}
type ComponentEntity struct {
Request map[string]interface{}
Component TCCComponent
}
// 事务状态
type TXStatus string
const (
// 事务执行中
TXHanging TXStatus = "hanging"
// 事务成功
TXSuccessful TXStatus = "successful"
// 事务失败
TXFailure TXStatus = "failure"
)
func (t TXStatus) String() string {
return string(t)
}
type ComponentTryStatus string
func (c ComponentTryStatus) String() string {
return string(c)
}
const (
TryHanging ComponentTryStatus = "hanging"
// 事务成功
TrySucceesful ComponentTryStatus = "successful"
// 事务失败
TryFailure ComponentTryStatus = "failure"
)
type ComponentTryEntity struct {
ComponentID string
TryStatus ComponentTryStatus
}
// 事务
type Transaction struct {
TXID string `json:"txID"`
Components []*ComponentTryEntity
Status TXStatus `json:"status"`
CreatedAt time.Time `json:"createdAt"`
}
func (t *Transaction) getStatus(createdBefore time.Time) TXStatus {
// 1 如果当中出现失败的,直接置为失败
var hangingExist bool
for _, component := range t.Components {
if component.TryStatus == TryFailure {
return TXFailure
}
hangingExist = hangingExist || (component.TryStatus != TrySucceesful)
}
// 2 如果存在 hanging 状态,并且已经超时,也直接置为失败
if hangingExist && t.CreatedAt.Before(createdBefore) {
return TXFailure
}
// 3 如果存在组件 try 操作处于 hanging 状态,则返回 hanging 状态
if hangingExist {
return TXHanging
}
// 4 走到这个分支必然意味着所有组件的 try 操作都成功了
return TXSuccessful
}