Skip to content

Commit 8ed5752

Browse files
authored
Return error when constructing cluster session failed (#112)
1 parent 8fc3fd3 commit 8ed5752

File tree

5 files changed

+23
-12
lines changed

5 files changed

+23
-12
lines changed

client/session.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -1147,12 +1147,12 @@ func newSessionWithSpecifiedSqlDialect(config *Config) Session {
11471147
return Session{config: config}
11481148
}
11491149

1150-
func NewClusterSession(clusterConfig *ClusterConfig) Session {
1150+
func NewClusterSession(clusterConfig *ClusterConfig) (Session, error) {
11511151
clusterConfig.sqlDialect = TreeSqlDialect
11521152
return newClusterSessionWithSqlDialect(clusterConfig)
11531153
}
11541154

1155-
func newClusterSessionWithSqlDialect(clusterConfig *ClusterConfig) Session {
1155+
func newClusterSessionWithSqlDialect(clusterConfig *ClusterConfig) (Session, error) {
11561156
session := Session{}
11571157
node := endPoint{}
11581158
for i := 0; i < len(clusterConfig.NodeUrls); i++ {
@@ -1180,9 +1180,9 @@ func newClusterSessionWithSqlDialect(clusterConfig *ClusterConfig) Session {
11801180
}
11811181
}
11821182
if !session.trans.IsOpen() {
1183-
log.Fatal("No Server Can Connect")
1183+
return session, fmt.Errorf("no server can connect")
11841184
}
1185-
return session
1185+
return session, nil
11861186
}
11871187

11881188
func (s *Session) initClusterConn(node endPoint) error {

client/sessionpool.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,11 @@ func (spool *SessionPool) getTableSession() (ITableSession, error) {
114114

115115
func (spool *SessionPool) ConstructSession(config *PoolConfig) (session Session, err error) {
116116
if len(config.NodeUrls) > 0 {
117-
session = newClusterSessionWithSqlDialect(getClusterSessionConfig(config))
118-
if err := session.OpenCluster(spool.enableCompression); err != nil {
117+
session, err = newClusterSessionWithSqlDialect(getClusterSessionConfig(config))
118+
if err != nil {
119+
return session, err
120+
}
121+
if err = session.OpenCluster(spool.enableCompression); err != nil {
119122
log.Print(err)
120123
return session, err
121124
}

client/tablesession.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,11 @@ func NewTableSession(config *Config, enableRPCCompression bool, connectionTimeou
109109
// - An error if there is an issue during session initialization.
110110
func NewClusterTableSession(clusterConfig *ClusterConfig, enableRPCCompression bool) (ITableSession, error) {
111111
clusterConfig.sqlDialect = TableSqlDialect
112-
session := newClusterSessionWithSqlDialect(clusterConfig)
113-
if err := session.OpenCluster(enableRPCCompression); err != nil {
112+
session, err := newClusterSessionWithSqlDialect(clusterConfig)
113+
if err != nil {
114+
return nil, err
115+
}
116+
if err = session.OpenCluster(enableRPCCompression); err != nil {
114117
return nil, err
115118
}
116119
return &TableSession{session: session}, nil

example/session_example.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,11 @@ func connectCluster() {
152152
UserName: "root",
153153
Password: "root",
154154
}
155-
session = client.NewClusterSession(config)
156-
if err := session.OpenCluster(false); err != nil {
155+
session, err := client.NewClusterSession(config)
156+
if err != nil {
157+
log.Fatal(err)
158+
}
159+
if err = session.OpenCluster(false); err != nil {
157160
log.Fatal(err)
158161
}
159162
}

test/e2e/e2e_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ func (s *e2eTestSuite) SetupSuite() {
4747
UserName: "root",
4848
Password: "root",
4949
}
50-
s.session = client.NewClusterSession(&clusterConfig)
51-
err := s.session.Open(false, 0)
50+
session, err := client.NewClusterSession(&clusterConfig)
51+
s.Require().NoError(err)
52+
s.session = session
53+
err = s.session.Open(false, 0)
5254
s.Require().NoError(err)
5355
}
5456

0 commit comments

Comments
 (0)