@@ -31,27 +31,29 @@ import RoundRobinLoadBalancingStrategy, {ROUND_ROBIN_STRATEGY_NAME} from './inte
31
31
class RoutingDriver extends Driver {
32
32
33
33
constructor ( url , routingContext , userAgent , token = { } , config = { } ) {
34
- super ( url , userAgent , token , RoutingDriver . _validateConfig ( config ) ) ;
34
+ super ( url , userAgent , token , validateConfig ( config ) ) ;
35
35
this . _routingContext = routingContext ;
36
36
}
37
37
38
38
_createConnectionProvider ( address , connectionPool , driverOnErrorCallback ) {
39
- const loadBalancingStrategy = RoutingDriver . _createLoadBalancingStrategy ( this . _config , connectionPool ) ;
39
+ const loadBalancingStrategy = createLoadBalancingStrategy ( this . _config , connectionPool ) ;
40
40
return new LoadBalancer ( address , this . _routingContext , connectionPool , loadBalancingStrategy , driverOnErrorCallback ) ;
41
41
}
42
42
43
43
_createSession ( mode , connectionProvider , bookmark , config ) {
44
44
return new RoutingSession ( mode , connectionProvider , bookmark , config , ( error , conn ) => {
45
- if ( error . code === SESSION_EXPIRED ) {
46
- this . _forgetConnection ( conn ) ;
45
+ if ( ! conn ) {
46
+ // connection can be undefined if error happened before connection was acquired
47
47
return error ;
48
- } else if ( RoutingDriver . _isFailureToWrite ( error ) ) {
49
- let url = 'UNKNOWN' ;
50
- // connection is undefined if error happened before connection was acquired
51
- if ( conn ) {
52
- url = conn . url ;
53
- this . _connectionProvider . forgetWriter ( conn . url ) ;
54
- }
48
+ }
49
+
50
+ const url = conn . url ;
51
+
52
+ if ( error . code === SESSION_EXPIRED || isDatabaseUnavailable ( error ) ) {
53
+ this . _connectionProvider . forget ( url ) ;
54
+ return error ;
55
+ } else if ( isFailureToWrite ( error ) ) {
56
+ this . _connectionProvider . forgetWriter ( url ) ;
55
57
return newError ( 'No longer possible to write to server at ' + url , SESSION_EXPIRED ) ;
56
58
} else {
57
59
return error ;
@@ -64,44 +66,54 @@ class RoutingDriver extends Driver {
64
66
// result in SESSION_EXPIRED because there might still exist other servers capable of serving the request
65
67
return SESSION_EXPIRED ;
66
68
}
69
+ }
67
70
68
- _forgetConnection ( connection ) {
69
- // connection is undefined if error happened before connection was acquired
70
- if ( connection ) {
71
- this . _connectionProvider . forget ( connection . url ) ;
72
- }
71
+ /**
72
+ * @private
73
+ */
74
+ function validateConfig ( config ) {
75
+ if ( config . trust === 'TRUST_ON_FIRST_USE' ) {
76
+ throw newError ( 'The chosen trust mode is not compatible with a routing driver' ) ;
73
77
}
78
+ return config ;
79
+ }
74
80
75
- static _validateConfig ( config ) {
76
- if ( config . trust === 'TRUST_ON_FIRST_USE' ) {
77
- throw newError ( 'The chosen trust mode is not compatible with a routing driver' ) ;
78
- }
79
- return config ;
80
- }
81
+ /**
82
+ * @private
83
+ */
84
+ function isFailureToWrite ( error ) {
85
+ return error . code === 'Neo.ClientError.Cluster.NotALeader' ||
86
+ error . code === 'Neo.ClientError.General.ForbiddenOnReadOnlyDatabase' ;
87
+ }
81
88
82
- static _isFailureToWrite ( error ) {
83
- return error . code === 'Neo.ClientError.Cluster.NotALeader' ||
84
- error . code === 'Neo.ClientError.General.ForbiddenOnReadOnlyDatabase' ;
85
- }
89
+ /**
90
+ * @private
91
+ */
92
+ function isDatabaseUnavailable ( error ) {
93
+ return error . code === 'Neo.TransientError.General.DatabaseUnavailable' ;
94
+ }
86
95
87
- /**
88
- * Create new load balancing strategy based on the config.
89
- * @param {object } config the user provided config.
90
- * @param {Pool } connectionPool the connection pool for this driver.
91
- * @return {LoadBalancingStrategy } new strategy.
92
- */
93
- static _createLoadBalancingStrategy ( config , connectionPool ) {
94
- const configuredValue = config . loadBalancingStrategy ;
95
- if ( ! configuredValue || configuredValue === LEAST_CONNECTED_STRATEGY_NAME ) {
96
- return new LeastConnectedLoadBalancingStrategy ( connectionPool ) ;
97
- } else if ( configuredValue === ROUND_ROBIN_STRATEGY_NAME ) {
98
- return new RoundRobinLoadBalancingStrategy ( ) ;
99
- } else {
100
- throw newError ( 'Unknown load balancing strategy: ' + configuredValue ) ;
101
- }
96
+ /**
97
+ * Create new load balancing strategy based on the config.
98
+ * @param {object } config the user provided config.
99
+ * @param {Pool } connectionPool the connection pool for this driver.
100
+ * @return {LoadBalancingStrategy } new strategy.
101
+ * @private
102
+ */
103
+ function createLoadBalancingStrategy ( config , connectionPool ) {
104
+ const configuredValue = config . loadBalancingStrategy ;
105
+ if ( ! configuredValue || configuredValue === LEAST_CONNECTED_STRATEGY_NAME ) {
106
+ return new LeastConnectedLoadBalancingStrategy ( connectionPool ) ;
107
+ } else if ( configuredValue === ROUND_ROBIN_STRATEGY_NAME ) {
108
+ return new RoundRobinLoadBalancingStrategy ( ) ;
109
+ } else {
110
+ throw newError ( 'Unknown load balancing strategy: ' + configuredValue ) ;
102
111
}
103
112
}
104
113
114
+ /**
115
+ * @private
116
+ */
105
117
class RoutingSession extends Session {
106
118
constructor ( mode , connectionProvider , bookmark , config , onFailedConnection ) {
107
119
super ( mode , connectionProvider , bookmark , config ) ;
0 commit comments