-
Notifications
You must be signed in to change notification settings - Fork 26
/
transaction.go
96 lines (78 loc) · 1.71 KB
/
transaction.go
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
95
96
package message
import (
"sync"
"time"
)
type TransactionGetter interface {
GetDuration() time.Duration
}
type Transactor interface {
Messager
TransactionGetter
SetDuration(duration time.Duration)
SetDurationStart(time time.Time)
NewEvent(mtype, name string) Messager
LogEvent(mtype, name string, args ...string)
}
type Transaction struct {
Message
children []Messager
isCompleted bool
mu sync.Mutex
duration time.Duration
durationStart time.Time
}
func (t *Transaction) Complete() {
if t.isCompleted {
return
}
t.isCompleted = true
if t.duration == 0 {
t.duration = time.Now().Sub(t.Message.timestamp)
}
if t.Message.flush != nil {
t.Message.flush(t)
}
}
func (t *Transaction) GetDuration() time.Duration {
return t.duration
}
func (t *Transaction) SetDuration(duration time.Duration) {
t.duration = duration
}
func (t *Transaction) SetDurationStart(time time.Time) {
t.durationStart = time
}
func (t *Transaction) NewEvent(mtype, name string) Messager {
var e = NewEvent(mtype, name, nil)
t.AddChild(e)
return e
}
func (t *Transaction) LogEvent(mtype, name string, args ...string) {
var e = t.NewEvent(mtype, name)
if len(args) > 0 {
e.SetStatus(args[0])
}
if len(args) > 1 {
e.SetData(args[1])
}
e.Complete()
}
func (t *Transaction) AddChild(m Messager) {
t.mu.Lock()
defer t.mu.Unlock()
t.children = append(t.children, m)
}
func (t *Transaction) GetChildren() []Messager {
return t.children
}
func NewTransaction(mtype, name string, flush Flush) *Transaction {
return &Transaction{
Message: NewMessage(mtype, name, flush),
children: make([]Messager, 0),
isCompleted: false,
mu: sync.Mutex{},
duration: 0,
durationStart: time.Time{},
}
}