Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify open() and openSSL() #165

Merged
merged 2 commits into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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