1
- package util
1
+ package cassandra
2
2
3
3
import (
4
4
"sync"
8
8
log "github.com/sirupsen/logrus"
9
9
)
10
10
11
- // CassandraSession stores a connection to Cassandra along with associated configurations
12
- type CassandraSession struct {
11
+ // Session stores a connection to Cassandra along with associated configurations
12
+ type Session struct {
13
13
session * gocql.Session
14
14
cluster * gocql.ClusterConfig
15
15
shutdown chan struct {}
@@ -20,22 +20,22 @@ type CassandraSession struct {
20
20
sync.RWMutex
21
21
}
22
22
23
- // NewCassandraSession creates and returns a CassandraSession
24
- func NewCassandraSession (session * gocql.Session ,
23
+ // NewSession creates and returns a CassandraSession
24
+ func NewSession (session * gocql.Session ,
25
25
clusterConfig * gocql.ClusterConfig ,
26
26
shutdown chan struct {},
27
27
timeout time.Duration ,
28
28
interval time.Duration ,
29
29
addrs string ,
30
- logPrefix string ) * CassandraSession {
30
+ logPrefix string ) * Session {
31
31
if clusterConfig == nil {
32
- panic ("NewCassandraSession received nil pointer for ClusterConfig" )
32
+ panic ("NewSession received nil pointer for ClusterConfig" )
33
33
}
34
34
if session == nil {
35
- panic ("NewCassandraSession received nil pointer for session" )
35
+ panic ("NewSession received nil pointer for session" )
36
36
}
37
37
38
- cs := & CassandraSession {
38
+ cs := & Session {
39
39
session : session ,
40
40
cluster : clusterConfig ,
41
41
shutdown : shutdown ,
@@ -53,46 +53,46 @@ func NewCassandraSession(session *gocql.Session,
53
53
// if it cannot query Cassandra for longer than connectionCheckTimeout it will create a new session
54
54
//
55
55
// if you are not using a WaitGroup in the caller, just pass in nil
56
- func (c * CassandraSession ) DeadConnectionCheck (wg * sync.WaitGroup ) {
56
+ func (s * Session ) DeadConnectionCheck (wg * sync.WaitGroup ) {
57
57
if wg != nil {
58
58
defer wg .Done ()
59
59
}
60
60
61
- ticker := time .NewTicker (time .Second * c .connectionCheckInterval )
61
+ ticker := time .NewTicker (time .Second * s .connectionCheckInterval )
62
62
var totaltime time.Duration
63
63
var err error
64
64
var oldSession * gocql.Session
65
65
66
66
OUTER:
67
67
for {
68
- // connection to cassandra has been down for longer than the configured timeout
69
- if totaltime >= c .connectionCheckTimeout {
70
- c .Lock ()
68
+ // connection to Cassandra has been down for longer than the configured timeout
69
+ if totaltime >= s .connectionCheckTimeout {
70
+ s .Lock ()
71
71
for {
72
72
select {
73
- case <- c .shutdown :
74
- log .Infof ("%s: received shutdown, exiting deadConnectionCheck" , c .logPrefix )
75
- if c .session != nil && ! c .session .Closed () {
76
- c .session .Close ()
73
+ case <- s .shutdown :
74
+ log .Infof ("%s: received shutdown, exiting deadConnectionCheck" , s .logPrefix )
75
+ if s .session != nil && ! s .session .Closed () {
76
+ s .session .Close ()
77
77
}
78
78
// make sure we unlock the sessionLock before returning
79
- c .Unlock ()
79
+ s .Unlock ()
80
80
return
81
81
default :
82
- log .Errorf ("%s: creating new session to cassandra using hosts: %v" , c .logPrefix , c .addrs )
83
- if c .session != nil && ! c .session .Closed () && oldSession == nil {
84
- oldSession = c .session
82
+ log .Errorf ("%s: creating new session to cassandra using hosts: %v" , s .logPrefix , s .addrs )
83
+ if s .session != nil && ! s .session .Closed () && oldSession == nil {
84
+ oldSession = s .session
85
85
}
86
- c .session , err = c .cluster .CreateSession ()
86
+ s .session , err = s .cluster .CreateSession ()
87
87
if err != nil {
88
- log .Errorf ("%s: error while attempting to recreate cassandra session. will retry after %v: %v" , c .logPrefix , c .connectionCheckInterval .String (), err )
89
- time .Sleep (c .connectionCheckInterval )
90
- totaltime += c .connectionCheckInterval
88
+ log .Errorf ("%s: error while attempting to recreate cassandra session. will retry after %v: %v" , s .logPrefix , s .connectionCheckInterval .String (), err )
89
+ time .Sleep (s .connectionCheckInterval )
90
+ totaltime += s .connectionCheckInterval
91
91
// continue inner loop to attempt to reconnect
92
92
continue
93
93
}
94
- c .Unlock ()
95
- log .Errorf ("%s: reconnecting to cassandra took %v" , c .logPrefix , (totaltime - c .connectionCheckTimeout ).String ())
94
+ s .Unlock ()
95
+ log .Errorf ("%s: reconnecting to cassandra took %v" , s .logPrefix , (totaltime - s .connectionCheckTimeout ).String ())
96
96
totaltime = 0
97
97
if oldSession != nil {
98
98
oldSession .Close ()
@@ -105,33 +105,33 @@ OUTER:
105
105
}
106
106
107
107
select {
108
- case <- c .shutdown :
109
- log .Infof ("%s: received shutdown, exiting deadConnectionCheck" , c .logPrefix )
110
- if c .session != nil && ! c .session .Closed () {
111
- c .session .Close ()
108
+ case <- s .shutdown :
109
+ log .Infof ("%s: received shutdown, exiting deadConnectionCheck" , s .logPrefix )
110
+ if s .session != nil && ! s .session .Closed () {
111
+ s .session .Close ()
112
112
}
113
113
return
114
114
case <- ticker .C :
115
- c .RLock ()
115
+ s .RLock ()
116
116
// this query should work on all cassandra deployments, but we may need to revisit this
117
- err = c .session .Query ("SELECT cql_version FROM system.local" ).Exec ()
117
+ err = s .session .Query ("SELECT cql_version FROM system.local" ).Exec ()
118
118
if err == nil {
119
119
totaltime = 0
120
120
} else {
121
- totaltime += c .connectionCheckInterval
122
- log .Errorf ("%s: could not execute connection check query for %v: %v" , c .logPrefix , totaltime .String (), err )
121
+ totaltime += s .connectionCheckInterval
122
+ log .Errorf ("%s: could not execute connection check query for %v: %v" , s .logPrefix , totaltime .String (), err )
123
123
}
124
- c .RUnlock ()
124
+ s .RUnlock ()
125
125
}
126
126
}
127
127
}
128
128
129
- // CurrentSession retrieves the current active cassandra session
129
+ // CurrentSession retrieves the current active Cassandra session
130
130
//
131
131
// If the connection to Cassandra is down, this will block until it can be restored
132
- func (c * CassandraSession ) CurrentSession () * gocql.Session {
133
- c .RLock ()
134
- session := c .session
135
- c .RUnlock ()
132
+ func (s * Session ) CurrentSession () * gocql.Session {
133
+ s .RLock ()
134
+ session := s .session
135
+ s .RUnlock ()
136
136
return session
137
137
}
0 commit comments