Browse Source

Merge pull request #838 from clawconduce/master

Some pre-work for lazy loading #730
Chris Bannister 9 years ago
parent
commit
b6e5fc4b08
2 changed files with 20 additions and 16 deletions
  1. 1 3
      connectionpool.go
  2. 19 13
      session.go

+ 1 - 3
connectionpool.go

@@ -70,9 +70,7 @@ type policyConnPool struct {
 	endpoints []string
 }
 
-func connConfig(session *Session) (*ConnConfig, error) {
-	cfg := session.cfg
-
+func connConfig(cfg *ClusterConfig) (*ConnConfig, error) {
 	var (
 		err       error
 		tlsConfig *tls.Config

+ 19 - 13
session.go

@@ -126,16 +126,24 @@ func NewSession(cfg ClusterConfig) (*Session, error) {
 		policy: cfg.PoolConfig.HostSelectionPolicy,
 	}
 
-	if err := s.init(); err != nil {
-		// TODO(zariel): dont wrap this error in fmt.Errorf, return a typed error
-		s.Close()
+	//Check the TLS Config before trying to connect to anything external
+	connCfg, err := connConfig(&s.cfg)
+	if err != nil {
+		//TODO: Return a typed error
 		return nil, fmt.Errorf("gocql: unable to create session: %v", err)
 	}
+	s.connCfg = connCfg
 
-	if s.pool.Size() == 0 {
-		// TODO(zariel): move this to init
+	if err := s.init(); err != nil {
 		s.Close()
-		return nil, ErrNoConnectionsStarted
+		if err == ErrNoConnectionsStarted {
+			//This error used to be generated inside NewSession & returned directly
+			//Forward it on up to be backwards compatible
+			return nil, ErrNoConnectionsStarted
+		} else {
+			// TODO(zariel): dont wrap this error in fmt.Errorf, return a typed error
+			return nil, fmt.Errorf("gocql: unable to create session: %v", err)
+		}
 	}
 
 	return s, nil
@@ -147,12 +155,6 @@ func (s *Session) init() error {
 		return err
 	}
 
-	connCfg, err := connConfig(s)
-	if err != nil {
-		return err
-	}
-	s.connCfg = connCfg
-
 	if !s.cfg.disableControlConn {
 		s.control = createControlConn(s)
 		if s.cfg.ProtoVersion == 0 {
@@ -165,7 +167,7 @@ func (s *Session) init() error {
 
 			// TODO(zariel): we really only need this in 1 place
 			s.cfg.ProtoVersion = proto
-			connCfg.ProtoVersion = proto
+			s.connCfg.ProtoVersion = proto
 		}
 
 		if err := s.control.connect(hosts); err != nil {
@@ -212,6 +214,10 @@ func (s *Session) init() error {
 		s.useSystemSchema = hosts[0].Version().Major >= 3
 	}
 
+	if s.pool.Size() == 0 {
+		return ErrNoConnectionsStarted
+	}
+
 	return nil
 }