@@ -2,6 +2,7 @@ package gorethink
22
33import (
44 "crypto/tls"
5+ "sync"
56 "time"
67
78 p "github.com/dancannon/gorethink/ql2"
@@ -10,8 +11,10 @@ import (
1011// A Session represents a connection to a RethinkDB cluster and should be used
1112// when executing queries.
1213type Session struct {
13- hosts []Host
14- opts * ConnectOpts
14+ hosts []Host
15+ opts * ConnectOpts
16+
17+ mu sync.RWMutex
1518 cluster * Cluster
1619 closed bool
1720}
@@ -114,25 +117,32 @@ func (s *Session) Reconnect(optArgs ...CloseOpts) error {
114117 return err
115118 }
116119
120+ s .mu .Lock ()
117121 s .cluster , err = NewCluster (s .hosts , s .opts )
118122 if err != nil {
119123 return err
120124 }
121125
122126 s .closed = false
127+ s .mu .Unlock ()
123128
124129 return nil
125130}
126131
127132// Close closes the session
128133func (s * Session ) Close (optArgs ... CloseOpts ) error {
134+ s .mu .Lock ()
135+ defer s .mu .Unlock ()
136+
129137 if s .closed {
130138 return nil
131139 }
132140
133141 if len (optArgs ) >= 1 {
134142 if optArgs [0 ].NoReplyWait {
143+ s .mu .Unlock ()
135144 s .NoReplyWait ()
145+ s .mu .Lock ()
136146 }
137147 }
138148
@@ -148,12 +158,18 @@ func (s *Session) Close(optArgs ...CloseOpts) error {
148158// SetMaxIdleConns sets the maximum number of connections in the idle
149159// connection pool.
150160func (s * Session ) SetMaxIdleConns (n int ) {
161+ s .mu .Lock ()
162+ defer s .mu .Unlock ()
163+
151164 s .opts .MaxIdle = n
152165 s .cluster .SetMaxIdleConns (n )
153166}
154167
155168// SetMaxOpenConns sets the maximum number of open connections to the database.
156169func (s * Session ) SetMaxOpenConns (n int ) {
170+ s .mu .Lock ()
171+ defer s .mu .Unlock ()
172+
157173 s .opts .MaxOpen = n
158174 s .cluster .SetMaxOpenConns (n )
159175}
@@ -162,28 +178,55 @@ func (s *Session) SetMaxOpenConns(n int) {
162178// processed by the server. Note that this guarantee only applies to queries
163179// run on the given connection
164180func (s * Session ) NoReplyWait () error {
181+ s .mu .RLock ()
182+ defer s .mu .RUnlock ()
183+
184+ if s .closed {
185+ return ErrConnectionClosed
186+ }
187+
165188 return s .cluster .Exec (Query {
166189 Type : p .Query_NOREPLY_WAIT ,
167190 })
168191}
169192
170193// Use changes the default database used
171194func (s * Session ) Use (database string ) {
195+ s .mu .RLock ()
196+ defer s .mu .RUnlock ()
197+
172198 s .opts .Database = database
173199}
174200
175201// Query executes a ReQL query using the session to connect to the database
176202func (s * Session ) Query (q Query ) (* Cursor , error ) {
203+ s .mu .RLock ()
204+ defer s .mu .RUnlock ()
205+
206+ if s .closed {
207+ return nil , ErrConnectionClosed
208+ }
209+
177210 return s .cluster .Query (q )
178211}
179212
180213// Exec executes a ReQL query using the session to connect to the database
181214func (s * Session ) Exec (q Query ) error {
215+ s .mu .RLock ()
216+ defer s .mu .RUnlock ()
217+
218+ if s .closed {
219+ return ErrConnectionClosed
220+ }
221+
182222 return s .cluster .Exec (q )
183223}
184224
185225// SetHosts resets the hosts used when connecting to the RethinkDB cluster
186226func (s * Session ) SetHosts (hosts []Host ) {
227+ s .mu .Lock ()
228+ defer s .mu .Unlock ()
229+
187230 s .hosts = hosts
188231}
189232
0 commit comments