Skip to content

Commit

Permalink
Make sure policies are not reused between sessions
Browse files Browse the repository at this point in the history
Sharing a policy between sessions is not supported because the policy
receives state updates from the session.

Let's update the documentation and add a panic in TokenAwareHostPolicy
constructor. It is better to panic early than to have races and
undefined behavior later.

See also discussion in scylladb#94
  • Loading branch information
martin-sucha committed Jan 27, 2022
1 parent d73e6b1 commit 554432f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
type PoolConfig struct {
// HostSelectionPolicy sets the policy for selecting which host to use for a
// given query (default: RoundRobinHostPolicy())
// It is not supported to use a single HostSelectionPolicy in multiple sessions
// (even if you close the old session before using in a new session).
HostSelectionPolicy HostSelectionPolicy
}

Expand Down
8 changes: 8 additions & 0 deletions policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ type KeyspaceUpdateEvent struct {

// HostSelectionPolicy is an interface for selecting
// the most appropriate host to execute a given query.
// HostSelectionPolicy instances cannot be shared between sessions.
type HostSelectionPolicy interface {
HostStateNotifier
SetPartitioner
Expand Down Expand Up @@ -388,6 +389,13 @@ type tokenAwareHostPolicy struct {
}

func (t *tokenAwareHostPolicy) Init(s *Session) {
t.mu.Lock()
defer t.mu.Unlock()
if t.getKeyspaceMetadata != nil {
// Init was already called.
// See https://github.com/scylladb/gocql/issues/94.
panic("sharing token aware host selection policy between sessions is not supported")
}
t.getKeyspaceMetadata = s.KeyspaceMetadata
t.getKeyspaceName = func() string { return s.cfg.Keyspace }
t.logger = s.logger
Expand Down

0 comments on commit 554432f

Please sign in to comment.