Skip to content

Commit

Permalink
Return error when constructing cluster session failed (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
shuwenwei authored Dec 11, 2024
1 parent 8fc3fd3 commit 8ed5752
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 12 deletions.
8 changes: 4 additions & 4 deletions client/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1147,12 +1147,12 @@ func newSessionWithSpecifiedSqlDialect(config *Config) Session {
return Session{config: config}
}

func NewClusterSession(clusterConfig *ClusterConfig) Session {
func NewClusterSession(clusterConfig *ClusterConfig) (Session, error) {
clusterConfig.sqlDialect = TreeSqlDialect
return newClusterSessionWithSqlDialect(clusterConfig)
}

func newClusterSessionWithSqlDialect(clusterConfig *ClusterConfig) Session {
func newClusterSessionWithSqlDialect(clusterConfig *ClusterConfig) (Session, error) {
session := Session{}
node := endPoint{}
for i := 0; i < len(clusterConfig.NodeUrls); i++ {
Expand Down Expand Up @@ -1180,9 +1180,9 @@ func newClusterSessionWithSqlDialect(clusterConfig *ClusterConfig) Session {
}
}
if !session.trans.IsOpen() {
log.Fatal("No Server Can Connect")
return session, fmt.Errorf("no server can connect")
}
return session
return session, nil
}

func (s *Session) initClusterConn(node endPoint) error {
Expand Down
7 changes: 5 additions & 2 deletions client/sessionpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,11 @@ func (spool *SessionPool) getTableSession() (ITableSession, error) {

func (spool *SessionPool) ConstructSession(config *PoolConfig) (session Session, err error) {
if len(config.NodeUrls) > 0 {
session = newClusterSessionWithSqlDialect(getClusterSessionConfig(config))
if err := session.OpenCluster(spool.enableCompression); err != nil {
session, err = newClusterSessionWithSqlDialect(getClusterSessionConfig(config))
if err != nil {
return session, err
}
if err = session.OpenCluster(spool.enableCompression); err != nil {
log.Print(err)
return session, err
}
Expand Down
7 changes: 5 additions & 2 deletions client/tablesession.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,11 @@ func NewTableSession(config *Config, enableRPCCompression bool, connectionTimeou
// - An error if there is an issue during session initialization.
func NewClusterTableSession(clusterConfig *ClusterConfig, enableRPCCompression bool) (ITableSession, error) {
clusterConfig.sqlDialect = TableSqlDialect
session := newClusterSessionWithSqlDialect(clusterConfig)
if err := session.OpenCluster(enableRPCCompression); err != nil {
session, err := newClusterSessionWithSqlDialect(clusterConfig)
if err != nil {
return nil, err
}
if err = session.OpenCluster(enableRPCCompression); err != nil {
return nil, err
}
return &TableSession{session: session}, nil
Expand Down
7 changes: 5 additions & 2 deletions example/session_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,11 @@ func connectCluster() {
UserName: "root",
Password: "root",
}
session = client.NewClusterSession(config)
if err := session.OpenCluster(false); err != nil {
session, err := client.NewClusterSession(config)
if err != nil {
log.Fatal(err)
}
if err = session.OpenCluster(false); err != nil {
log.Fatal(err)
}
}
Expand Down
6 changes: 4 additions & 2 deletions test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ func (s *e2eTestSuite) SetupSuite() {
UserName: "root",
Password: "root",
}
s.session = client.NewClusterSession(&clusterConfig)
err := s.session.Open(false, 0)
session, err := client.NewClusterSession(&clusterConfig)
s.Require().NoError(err)
s.session = session
err = s.session.Open(false, 0)
s.Require().NoError(err)
}

Expand Down

0 comments on commit 8ed5752

Please sign in to comment.