-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtracer.go
59 lines (49 loc) · 1.29 KB
/
tracer.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
package tracer
import (
"github.com/v2pro/plz/gls"
"sync"
)
type IntSet map[int64]bool
var tracer = Tracer{
Locker: &sync.RWMutex{},
ActiveTransactions: map[string]IntSet{},
TransactionParticipants: map[int64]string{},
}
func (set IntSet) Add(x int64) bool {
exists := set.Contains(x)
set[x] = true
return !exists
}
func (set IntSet) Contains(x int64) bool {
_, contains := set[x]
return contains
}
type Tracer struct {
sync.Locker
ActiveTransactions map[string]IntSet
TransactionParticipants map[int64]string
}
func (tracer *Tracer) BeginTransaction(transaction string) {
tracer.Lock()
defer tracer.Unlock()
if _, exists := tracer.ActiveTransactions[transaction]; !exists {
tracer.ActiveTransactions[transaction] = IntSet{}
}
goID := gls.GoID()
tracer.ActiveTransactions[transaction].Add(goID)
tracer.TransactionParticipants[goID] = transaction
}
func (tracer *Tracer) CommitTransaction(transaction string) {
tracer.Lock()
defer tracer.Unlock()
for participant := range tracer.ActiveTransactions[transaction] {
delete(tracer.TransactionParticipants, participant)
}
delete(tracer.ActiveTransactions, transaction)
}
func (tracer *Tracer) GetActiveTransaction() string {
tracer.Lock()
defer tracer.Unlock()
goID := gls.GoID()
return tracer.TransactionParticipants[goID]
}