|
@@ -58,6 +58,8 @@ func setupTLSConfig(sslOpts *SslOptions) (*tls.Config, error) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
type policyConnPool struct {
|
|
type policyConnPool struct {
|
|
|
|
|
+ session *Session
|
|
|
|
|
+
|
|
|
port int
|
|
port int
|
|
|
numConns int
|
|
numConns int
|
|
|
connCfg *ConnConfig
|
|
connCfg *ConnConfig
|
|
@@ -69,7 +71,7 @@ type policyConnPool struct {
|
|
|
hostConnPools map[string]*hostConnPool
|
|
hostConnPools map[string]*hostConnPool
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func newPolicyConnPool(cfg *ClusterConfig, hostPolicy HostSelectionPolicy,
|
|
|
|
|
|
|
+func newPolicyConnPool(session *Session, hostPolicy HostSelectionPolicy,
|
|
|
connPolicy func() ConnSelectionPolicy) (*policyConnPool, error) {
|
|
connPolicy func() ConnSelectionPolicy) (*policyConnPool, error) {
|
|
|
|
|
|
|
|
var (
|
|
var (
|
|
@@ -77,6 +79,8 @@ func newPolicyConnPool(cfg *ClusterConfig, hostPolicy HostSelectionPolicy,
|
|
|
tlsConfig *tls.Config
|
|
tlsConfig *tls.Config
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
+ cfg := session.cfg
|
|
|
|
|
+
|
|
|
if cfg.SslOpts != nil {
|
|
if cfg.SslOpts != nil {
|
|
|
tlsConfig, err = setupTLSConfig(cfg.SslOpts)
|
|
tlsConfig, err = setupTLSConfig(cfg.SslOpts)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -86,6 +90,7 @@ func newPolicyConnPool(cfg *ClusterConfig, hostPolicy HostSelectionPolicy,
|
|
|
|
|
|
|
|
// create the pool
|
|
// create the pool
|
|
|
pool := &policyConnPool{
|
|
pool := &policyConnPool{
|
|
|
|
|
+ session: session,
|
|
|
port: cfg.Port,
|
|
port: cfg.Port,
|
|
|
numConns: cfg.NumConns,
|
|
numConns: cfg.NumConns,
|
|
|
connCfg: &ConnConfig{
|
|
connCfg: &ConnConfig{
|
|
@@ -130,6 +135,7 @@ func (p *policyConnPool) SetHosts(hosts []HostInfo) {
|
|
|
if !exists {
|
|
if !exists {
|
|
|
// create a connection pool for the host
|
|
// create a connection pool for the host
|
|
|
pool = newHostConnPool(
|
|
pool = newHostConnPool(
|
|
|
|
|
+ p.session,
|
|
|
hosts[i].Peer,
|
|
hosts[i].Peer,
|
|
|
p.port,
|
|
p.port,
|
|
|
p.numConns,
|
|
p.numConns,
|
|
@@ -184,9 +190,18 @@ func (p *policyConnPool) Pick(qry *Query) (SelectedHost, *Conn) {
|
|
|
host = nextHost()
|
|
host = nextHost()
|
|
|
if host == nil {
|
|
if host == nil {
|
|
|
break
|
|
break
|
|
|
|
|
+ } else if host.Info() == nil {
|
|
|
|
|
+ panic(fmt.Sprintf("policy %T returned no host info: %+v", p.hostPolicy, host))
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ pool, ok := p.hostConnPools[host.Info().Peer]
|
|
|
|
|
+ if !ok {
|
|
|
|
|
+ continue
|
|
|
}
|
|
}
|
|
|
- conn = p.hostConnPools[host.Info().Peer].Pick(qry)
|
|
|
|
|
|
|
+
|
|
|
|
|
+ conn = pool.Pick(qry)
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
p.mu.RUnlock()
|
|
p.mu.RUnlock()
|
|
|
return host, conn
|
|
return host, conn
|
|
|
}
|
|
}
|
|
@@ -208,6 +223,7 @@ func (p *policyConnPool) Close() {
|
|
|
// hostConnPool is a connection pool for a single host.
|
|
// hostConnPool is a connection pool for a single host.
|
|
|
// Connection selection is based on a provided ConnSelectionPolicy
|
|
// Connection selection is based on a provided ConnSelectionPolicy
|
|
|
type hostConnPool struct {
|
|
type hostConnPool struct {
|
|
|
|
|
+ session *Session
|
|
|
host string
|
|
host string
|
|
|
port int
|
|
port int
|
|
|
addr string
|
|
addr string
|
|
@@ -222,10 +238,11 @@ type hostConnPool struct {
|
|
|
filling bool
|
|
filling bool
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func newHostConnPool(host string, port int, size int, connCfg *ConnConfig,
|
|
|
|
|
|
|
+func newHostConnPool(session *Session, host string, port, size int, connCfg *ConnConfig,
|
|
|
keyspace string, policy ConnSelectionPolicy) *hostConnPool {
|
|
keyspace string, policy ConnSelectionPolicy) *hostConnPool {
|
|
|
|
|
|
|
|
pool := &hostConnPool{
|
|
pool := &hostConnPool{
|
|
|
|
|
+ session: session,
|
|
|
host: host,
|
|
host: host,
|
|
|
port: port,
|
|
port: port,
|
|
|
addr: JoinHostPort(host, port),
|
|
addr: JoinHostPort(host, port),
|
|
@@ -395,7 +412,7 @@ func (pool *hostConnPool) fillingStopped() {
|
|
|
// create a new connection to the host and add it to the pool
|
|
// create a new connection to the host and add it to the pool
|
|
|
func (pool *hostConnPool) connect() error {
|
|
func (pool *hostConnPool) connect() error {
|
|
|
// try to connect
|
|
// try to connect
|
|
|
- conn, err := Connect(pool.addr, pool.connCfg, pool)
|
|
|
|
|
|
|
+ conn, err := Connect(pool.addr, pool.connCfg, pool, pool.session)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|