Skip to content

Commit

Permalink
Unify open() and openSSL() (#165)
Browse files Browse the repository at this point in the history
* Unify open() and openSSL()

* Fix test
  • Loading branch information
Aiee authored Dec 7, 2021
1 parent 804d6d3 commit 83f96b3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 26 deletions.
4 changes: 2 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func logoutAndClose(conn *connection, sessionID int64) {
func TestConnection(t *testing.T) {
hostAdress := HostAddress{Host: address, Port: port}
conn := newConnection(hostAdress)
err := conn.open(hostAdress, testPoolConfig.TimeOut)
err := conn.open(hostAdress, testPoolConfig.TimeOut, nil)
if err != nil {
t.Fatalf("fail to open connection, address: %s, port: %d, %s", address, port, err.Error())
}
Expand Down Expand Up @@ -186,7 +186,7 @@ func TestAuthentication(t *testing.T) {
hostAdress := HostAddress{Host: address, Port: port}

conn := newConnection(hostAdress)
err := conn.open(hostAdress, testPoolConfig.TimeOut)
err := conn.open(hostAdress, testPoolConfig.TimeOut, nil)
if err != nil {
t.Fatalf("fail to open connection, address: %s, port: %d, %s", address, port, err.Error())
}
Expand Down
14 changes: 7 additions & 7 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type connection struct {
severAddress HostAddress
timeout time.Duration
returnedAt time.Time // the connection was created or returned.
sslConfig *tls.Config
graph *graph.GraphServiceClient
}

Expand All @@ -31,15 +32,14 @@ func newConnection(severAddress HostAddress) *connection {
severAddress: severAddress,
timeout: 0 * time.Millisecond,
returnedAt: time.Now(),
sslConfig: nil,
graph: nil,
}
}

func (cn *connection) open(hostAddress HostAddress, timeout time.Duration) error {
return cn.openSSL(hostAddress, timeout, nil)
}

func (cn *connection) openSSL(hostAddress HostAddress, timeout time.Duration, sslConfig *tls.Config) error {
// open opens a transport for the connection
// if sslConfig is not nil, an SSL transport will be created
func (cn *connection) open(hostAddress HostAddress, timeout time.Duration, sslConfig *tls.Config) error {
ip := hostAddress.Host
port := hostAddress.Port
newAdd := fmt.Sprintf("%s:%d", ip, port)
Expand Down Expand Up @@ -79,7 +79,7 @@ func (cn *connection) verifyClientVersion() error {
return fmt.Errorf("failed to verify client version: %s", err.Error())
}
if resp.GetErrorCode() != nebula.ErrorCode_SUCCEEDED {
return fmt.Errorf("incompatible version between client and server: %s.", string(resp.GetErrorMsg()))
return fmt.Errorf("incompatible version between client and server: %s", string(resp.GetErrorMsg()))
}
return nil
}
Expand All @@ -90,7 +90,7 @@ func (cn *connection) verifyClientVersion() error {
// When the timeout occurs, the connection will be reopened to avoid the impact of the message.
func (cn *connection) reopen() error {
cn.close()
return cn.open(cn.severAddress, cn.timeout)
return cn.open(cn.severAddress, cn.timeout, cn.sslConfig)
}

// Authenticate
Expand Down
22 changes: 5 additions & 17 deletions connection_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (pool *ConnectionPool) initPool() error {
newConn := newConnection(pool.addresses[i%len(pool.addresses)])

// Open connection to host
if err := newConn.openSSL(newConn.severAddress, pool.conf.TimeOut, pool.sslConfig); err != nil {
if err := newConn.open(newConn.severAddress, pool.conf.TimeOut, pool.sslConfig); err != nil {
// If initialization failed, clean idle queue
idleLen := pool.idleConnectionQueue.Len()
for i := 0; i < idleLen; i++ {
Expand Down Expand Up @@ -181,14 +181,8 @@ func (pool *ConnectionPool) release(conn *connection) {
func (pool *ConnectionPool) Ping(host HostAddress, timeout time.Duration) error {
newConn := newConnection(host)
// Open connection to host
if pool.sslConfig == nil {
if err := newConn.open(newConn.severAddress, timeout); err != nil {
return err
}
} else {
if err := newConn.openSSL(newConn.severAddress, timeout, pool.sslConfig); err != nil {
return err
}
if err := newConn.open(newConn.severAddress, timeout, pool.sslConfig); err != nil {
return err
}
newConn.close()
return nil
Expand Down Expand Up @@ -240,14 +234,8 @@ func (pool *ConnectionPool) newConnToHost() (*connection, error) {
host := pool.getHost()
newConn := newConnection(host)
// Open connection to host
if pool.sslConfig == nil {
if err := newConn.open(newConn.severAddress, pool.conf.TimeOut); err != nil {
return nil, err
}
} else {
if err := newConn.openSSL(newConn.severAddress, pool.conf.TimeOut, pool.sslConfig); err != nil {
return nil, err
}
if err := newConn.open(newConn.severAddress, pool.conf.TimeOut, pool.sslConfig); err != nil {
return nil, err
}
// Add connection to active queue
pool.activeConnectionQueue.PushBack(newConn)
Expand Down

0 comments on commit 83f96b3

Please sign in to comment.