Skip to content

Commit 383c458

Browse files
committed
CASSGO-23 Make Session immutable
Patch by Stanislav Bychkov; reviewed by João Reis for CASSGO-23
1 parent 0094af0 commit 383c458

File tree

6 files changed

+20
-69
lines changed

6 files changed

+20
-69
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5454

5555
- Move "execute batch" methods to Batch type (CASSGO-57)
5656

57+
- Make `Session` immutable by removing setters and associated mutex (CASSGO-23)
58+
5759
### Fixed
5860
- Cassandra version unmarshal fix (CASSGO-49)
5961

cassandra_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,10 @@ func TestTracing(t *testing.T) {
152152
}
153153

154154
// also works from session tracer
155-
session.SetTrace(trace)
156155
trace.mu.Lock()
157156
buf.Reset()
158157
trace.mu.Unlock()
159-
if err := session.Query(`SELECT id FROM trace WHERE id = ?`, 42).Scan(&value); err != nil {
158+
if err := session.Query(`SELECT id FROM trace WHERE id = ?`, 42).Trace(trace).Scan(&value); err != nil {
160159
t.Fatal("select:", err)
161160
}
162161
if buf.Len() == 0 {

cluster.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,17 @@ type ClusterConfig struct {
263263
// If not specified, defaults to the gocql.defaultLogger.
264264
Logger StdLogger
265265

266+
// Tracer will be used for all queries. Alternatively it can be set of on a
267+
// per query basis.
268+
// default: nil
269+
Tracer Tracer
270+
271+
// NextPagePrefetch sets the default threshold for pre-fetching new pages. If
272+
// there are only p*pageSize rows remaining, the next page will be requested
273+
// automatically. This value can also be changed on a per-query basis.
274+
// default: 0.25.
275+
NextPagePrefetch float64
276+
266277
// internal config for testing
267278
disableControlConn bool
268279
}
@@ -298,6 +309,7 @@ func NewCluster(hosts ...string) *ClusterConfig {
298309
ConvictionPolicy: &SimpleConvictionPolicy{},
299310
ReconnectionPolicy: &ConstantReconnectionPolicy{MaxRetries: 3, Interval: 1 * time.Second},
300311
WriteCoalesceWaitTime: 200 * time.Microsecond,
312+
NextPagePrefetch: 0.25,
301313
}
302314
return cfg
303315
}

doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@
271271
//
272272
// # Paging
273273
//
274-
// The driver supports paging of results with automatic prefetch, see ClusterConfig.PageSize, Session.SetPrefetch,
274+
// The driver supports paging of results with automatic prefetch, see ClusterConfig.PageSize,
275275
// Query.PageSize, and Query.Prefetch.
276276
//
277277
// It is also possible to control the paging manually with Query.PageState (this disables automatic prefetch).

session.go

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ type Session struct {
7575
ring ring
7676
metadata clusterMetadata
7777

78-
mu sync.RWMutex
79-
8078
control *controlConn
8179

8280
// event handlers
@@ -153,14 +151,15 @@ func NewSession(cfg ClusterConfig) (*Session, error) {
153151

154152
s := &Session{
155153
cons: cfg.Consistency,
156-
prefetch: 0.25,
154+
prefetch: cfg.NextPagePrefetch,
157155
cfg: cfg,
158156
pageSize: cfg.PageSize,
159157
stmtsLRU: &preparedLRU{lru: lru.New(cfg.MaxPreparedStmts)},
160158
connectObserver: cfg.ConnectObserver,
161159
ctx: ctx,
162160
cancel: cancel,
163161
logger: cfg.logger(),
162+
trace: cfg.Tracer,
164163
}
165164

166165
s.schemaDescriber = newSchemaDescriber(s)
@@ -416,41 +415,6 @@ func (s *Session) reconnectDownedHosts(intv time.Duration) {
416415
}
417416
}
418417

419-
// SetConsistency sets the default consistency level for this session. This
420-
// setting can also be changed on a per-query basis and the default value
421-
// is Quorum.
422-
func (s *Session) SetConsistency(cons Consistency) {
423-
s.mu.Lock()
424-
s.cons = cons
425-
s.mu.Unlock()
426-
}
427-
428-
// SetPageSize sets the default page size for this session. A value <= 0 will
429-
// disable paging. This setting can also be changed on a per-query basis.
430-
func (s *Session) SetPageSize(n int) {
431-
s.mu.Lock()
432-
s.pageSize = n
433-
s.mu.Unlock()
434-
}
435-
436-
// SetPrefetch sets the default threshold for pre-fetching new pages. If
437-
// there are only p*pageSize rows remaining, the next page will be requested
438-
// automatically. This value can also be changed on a per-query basis and
439-
// the default value is 0.25.
440-
func (s *Session) SetPrefetch(p float64) {
441-
s.mu.Lock()
442-
s.prefetch = p
443-
s.mu.Unlock()
444-
}
445-
446-
// SetTrace sets the default tracer for this session. This setting can also
447-
// be changed on a per-query basis.
448-
func (s *Session) SetTrace(trace Tracer) {
449-
s.mu.Lock()
450-
s.trace = trace
451-
s.mu.Unlock()
452-
}
453-
454418
// Query generates a new query object for interacting with the database.
455419
// Further details of the query may be tweaked using the resulting query
456420
// value before the query is executed. Query is automatically prepared
@@ -1005,7 +969,6 @@ type queryRoutingInfo struct {
1005969
func (q *Query) defaultsFromSession() {
1006970
s := q.session
1007971

1008-
s.mu.RLock()
1009972
q.cons = s.cons
1010973
q.pageSize = s.pageSize
1011974
q.trace = s.trace
@@ -1018,7 +981,6 @@ func (q *Query) defaultsFromSession() {
1018981
q.metrics = &queryMetrics{m: make(map[string]*hostMetrics)}
1019982

1020983
q.spec = &NonSpeculativeExecution{}
1021-
s.mu.RUnlock()
1022984
}
1023985

1024986
// Statement returns the statement that was used to generate this query.
@@ -1855,7 +1817,6 @@ func (s *Session) NewBatch(typ BatchType) *Batch {
18551817

18561818
// Batch creates a new batch operation using defaults defined in the cluster
18571819
func (s *Session) Batch(typ BatchType) *Batch {
1858-
s.mu.RLock()
18591820
batch := &Batch{
18601821
Type: typ,
18611822
rt: s.cfg.RetryPolicy,
@@ -1871,7 +1832,6 @@ func (s *Session) Batch(typ BatchType) *Batch {
18711832
routingInfo: &queryRoutingInfo{},
18721833
}
18731834

1874-
s.mu.RUnlock()
18751835
return batch
18761836
}
18771837

session_test.go

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,43 +35,21 @@ import (
3535
)
3636

3737
func TestSessionAPI(t *testing.T) {
38-
cfg := &ClusterConfig{}
38+
cfg := NewCluster()
3939

4040
s := &Session{
4141
cfg: *cfg,
4242
cons: Quorum,
4343
policy: RoundRobinHostPolicy(),
4444
logger: cfg.logger(),
4545
}
46+
defer s.Close()
4647

4748
s.pool = cfg.PoolConfig.buildPool(s)
4849
s.executor = &queryExecutor{
4950
pool: s.pool,
5051
policy: s.policy,
5152
}
52-
defer s.Close()
53-
54-
s.SetConsistency(All)
55-
if s.cons != All {
56-
t.Fatalf("expected consistency 'All', got '%v'", s.cons)
57-
}
58-
59-
s.SetPageSize(100)
60-
if s.pageSize != 100 {
61-
t.Fatalf("expected pageSize 100, got %v", s.pageSize)
62-
}
63-
64-
s.SetPrefetch(0.75)
65-
if s.prefetch != 0.75 {
66-
t.Fatalf("expceted prefetch 0.75, got %v", s.prefetch)
67-
}
68-
69-
trace := &traceWriter{}
70-
71-
s.SetTrace(trace)
72-
if s.trace != trace {
73-
t.Fatalf("expected traceWriter '%v',got '%v'", trace, s.trace)
74-
}
7553

7654
qry := s.Query("test", 1)
7755
if v, ok := qry.values[0].(int); !ok {

0 commit comments

Comments
 (0)