forked from vitessio/vitess
/
safe_session.go
57 lines (48 loc) · 1.35 KB
/
safe_session.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
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package vtgate
import (
"sync"
"github.com/youtube/vitess/go/vt/topo"
"github.com/youtube/vitess/go/vt/vtgate/proto"
)
type SafeSession struct {
mu sync.Mutex
*proto.Session
}
func NewSafeSession(sessn *proto.Session) *SafeSession {
return &SafeSession{Session: sessn}
}
func (session *SafeSession) InTransaction() bool {
if session == nil || session.Session == nil {
return false
}
session.mu.Lock()
defer session.mu.Unlock()
return session.Session.InTransaction
}
func (session *SafeSession) Find(keyspace, shard string, tabletType topo.TabletType) int64 {
if session == nil {
return 0
}
session.mu.Lock()
defer session.mu.Unlock()
for _, shardSession := range session.ShardSessions {
if keyspace == shardSession.Keyspace && tabletType == shardSession.TabletType && shard == shardSession.Shard {
return shardSession.TransactionId
}
}
return 0
}
func (session *SafeSession) Append(shardSession *proto.ShardSession) {
session.mu.Lock()
defer session.mu.Unlock()
session.ShardSessions = append(session.ShardSessions, shardSession)
}
func (session *SafeSession) Reset() {
session.mu.Lock()
defer session.mu.Unlock()
session.Session.InTransaction = false
session.ShardSessions = nil
}