1919
2020import Session from './session' ;
2121import Pool from './internal/pool' ;
22- import { connect } from './internal/connector ' ;
22+ import Connection from './internal/connection ' ;
2323import StreamObserver from './internal/stream-observer' ;
2424import { newError , SERVICE_UNAVAILABLE } from './error' ;
2525import { DirectConnectionProvider } from './internal/connection-providers' ;
2626import Bookmark from './internal/bookmark' ;
2727import ConnectivityVerifier from './internal/connectivity-verifier' ;
2828import PoolConfig , { DEFAULT_ACQUISITION_TIMEOUT , DEFAULT_MAX_SIZE } from './internal/pool-config' ;
2929import Logger from './internal/logger' ;
30+ import ConnectionErrorHandler from './internal/connection-error-handler' ;
3031
3132const DEFAULT_MAX_CONNECTION_LIFETIME = 60 * 60 * 1000 ; // 1 hour
3233
@@ -62,18 +63,18 @@ class Driver {
6263 * @constructor
6364 * @param {string } hostPort
6465 * @param {string } userAgent
65- * @param {object } token
66+ * @param {object } authToken
6667 * @param {object } config
6768 * @protected
6869 */
69- constructor ( hostPort , userAgent , token = { } , config = { } ) {
70+ constructor ( hostPort , userAgent , authToken = { } , config = { } ) {
7071 sanitizeConfig ( config ) ;
7172
7273 this . _id = idGenerator ++ ;
7374 this . _hostPort = hostPort ;
7475 this . _userAgent = userAgent ;
7576 this . _openConnections = { } ;
76- this . _token = token ;
77+ this . _authToken = authToken ;
7778 this . _config = config ;
7879 this . _log = Logger . create ( config ) ;
7980 this . _pool = new Pool (
@@ -127,18 +128,24 @@ class Driver {
127128 }
128129
129130 /**
130- * Create a new connection instance .
131- * @return {Connection } new connector-api session instance, a low level session API .
131+ * Create a new connection and initialize it .
132+ * @return {Promise< Connection> } promise resolved with a new connection or rejected when failed to connect .
132133 * @access private
133134 */
134135 _createConnection ( hostPort , release ) {
135- const conn = connect ( hostPort , this . _config , this . _connectionErrorCode ( ) , this . _log ) ;
136- const streamObserver = new _ConnectionStreamObserver ( this , conn ) ;
137- conn . protocol ( ) . initialize ( this . _userAgent , this . _token , streamObserver ) ;
138- conn . _release = ( ) => release ( hostPort , conn ) ;
139-
140- this . _openConnections [ conn . id ] = conn ;
141- return conn ;
136+ const connection = Connection . create ( hostPort , this . _config , this . _createConnectionErrorHandler ( ) , this . _log ) ;
137+ connection . _release = ( ) => release ( hostPort , connection ) ;
138+ this . _openConnections [ connection . id ] = connection ;
139+
140+ return connection . connect ( this . _userAgent , this . _authToken )
141+ . catch ( error => {
142+ if ( this . onError ) {
143+ // notify Driver.onError callback about connection initialization errors
144+ this . onError ( error ) ;
145+ }
146+ // propagate the error because connection failed to connect / initialize
147+ throw error ;
148+ } ) ;
142149 }
143150
144151 /**
@@ -186,7 +193,7 @@ class Driver {
186193 const sessionMode = Driver . _validateSessionMode ( mode ) ;
187194 const connectionProvider = this . _getOrCreateConnectionProvider ( ) ;
188195 const bookmark = new Bookmark ( bookmarkOrBookmarks ) ;
189- return this . _createSession ( sessionMode , connectionProvider , bookmark , this . _config ) ;
196+ return new Session ( sessionMode , connectionProvider , bookmark , this . _config ) ;
190197 }
191198
192199 static _validateSessionMode ( rawMode ) {
@@ -203,14 +210,8 @@ class Driver {
203210 }
204211
205212 // Extension point
206- _createSession ( mode , connectionProvider , bookmark , config ) {
207- return new Session ( mode , connectionProvider , bookmark , config ) ;
208- }
209-
210- // Extension point
211- _connectionErrorCode ( ) {
212- // connection errors might result in different error codes depending on the driver
213- return SERVICE_UNAVAILABLE ;
213+ _createConnectionErrorHandler ( ) {
214+ return new ConnectionErrorHandler ( SERVICE_UNAVAILABLE ) ;
214215 }
215216
216217 _getOrCreateConnectionProvider ( ) {
0 commit comments