Skip to content

Commit 63ee25d

Browse files
committed
Introduce Driver.getServerInfo method
This is part of the drivers unification initiative. The `Driver.verifyConnectivity` method was implemented in different ways accross the drivers with different returns value. For concistency and clarity about what the method does, the new behaviour of the `Driver.verifyConnectivy` is check the connectivity with all the readers available and then returns a resolve empty promise in case of success (the promise will be rejected, otherwise). The Server Info information was moved to the method `Driver.getSeverInfo` which verifies the connectivity and then returnt the `ServerInfo` of one of the read servers. For keeping the backwards compatibility, the `Driver.verifyConnectivity` was depreacted in this version and it should be replace with a version with `Promise<void>` as return value in the next major release. `Driver.getServerInfo` should be used instead if the client code needs server information.
1 parent 13e9238 commit 63ee25d

File tree

8 files changed

+84
-6
lines changed

8 files changed

+84
-6
lines changed

packages/bolt-connection/src/connection-provider/connection-provider-direct.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,8 @@ export default class DirectConnectionProvider extends PooledConnectionProvider {
103103
version => version >= BOLT_PROTOCOL_V4_4
104104
)
105105
}
106+
107+
async verifyConnectivityAndGetServerInfo () {
108+
return await this._verifyConnectivityAndGetServerVersion({ address: this._address })
109+
}
106110
}

packages/bolt-connection/src/connection-provider/connection-provider-pooled.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
import { createChannelConnection, ConnectionErrorHandler } from '../connection'
2121
import Pool, { PoolConfig } from '../pool'
22-
import { error, ConnectionProvider } from 'neo4j-driver-core'
22+
import { error, ConnectionProvider, ServerInfo } from 'neo4j-driver-core'
2323

2424
const { SERVICE_UNAVAILABLE } = error
2525
export default class PooledConnectionProvider extends ConnectionProvider {
@@ -109,6 +109,20 @@ export default class PooledConnectionProvider extends ConnectionProvider {
109109
return conn.close()
110110
}
111111

112+
/**
113+
* Acquire a connection from the pool and return it ServerInfo
114+
* @param {object} param
115+
* @param {string} param.address the server address
116+
* @return {Promise<ServerInfo>} the server info
117+
*/
118+
async _verifyConnectivityAndGetServerVersion ({ address }) {
119+
const connection = await this._connectionPool.acquire(address)
120+
const serverInfo = new ServerInfo(connection.server, connection.protocol().version)
121+
await connection.resetAndFlush().catch(() => {})
122+
await connection._release()
123+
return serverInfo
124+
}
125+
112126
async close () {
113127
// purge all idle connections in the connection pool
114128
await this._connectionPool.close()

packages/bolt-connection/src/connection-provider/connection-provider-routing.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,24 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
237237
return await this._hasProtocolVersion(
238238
version => version >= BOLT_PROTOCOL_V4_4
239239
)
240-
}
240+
}
241+
242+
async verifyConnectivityAndGetServerInfo ({ database, accessMode }) {
243+
const context = { database: database || DEFAULT_DB_NAME }
244+
245+
const routingTable = await this._freshRoutingTable({
246+
accessMode,
247+
database: context.database,
248+
onDatabaseNameResolved: (databaseName) => {
249+
context.database = context.database || databaseName
250+
}
251+
})
252+
253+
const servers = accessMode === WRITE ? routingTable.writers : routingTable.readers
254+
255+
return Promise.all(servers.map(address => this._verifyConnectivityAndGetServerVersion({ address })))
256+
.then(([serverInfo]) => serverInfo)
257+
}
241258

242259
forget (address, database) {
243260
this._routingTableRegistry.apply(database, {

packages/bolt-connection/src/pool/pool.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class Pool {
7171
/**
7272
* Acquire and idle resource fom the pool or create a new one.
7373
* @param {ServerAddress} address the address for which we're acquiring.
74-
* @return {Object} resource that is ready to use.
74+
* @return {Promise<Object>} resource that is ready to use.
7575
*/
7676
acquire (address) {
7777
const key = address.asKey()

packages/core/src/connection-provider.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import Connection from './connection'
2121
import { bookmarks } from './internal'
22+
import { ServerInfo } from './result-summary'
2223

2324

2425
/**
@@ -83,6 +84,20 @@ class ConnectionProvider {
8384
throw Error('Not implemented')
8485
}
8586

87+
/**
88+
* This method verifies the connectivity of the database by trying to acquire a connection
89+
* for each server available in the cluster.
90+
*
91+
* @param {object} param - object parameter
92+
* @property {string} param.database - the target database for the to-be-acquired connection
93+
* @property {string} param.accessMode - the access mode for the to-be-acquired connection
94+
*
95+
* @returns {Promise<ServerInfo>} promise resolved with server info or rejected with error.
96+
*/
97+
verifyConnectivityAndGetServerInfo(param? :{ database?: string, accessMode?: string }): Promise<ServerInfo> {
98+
throw Error('Not implemented')
99+
}
100+
86101
/**
87102
* Closes this connection provider along with its internals (connections, pools, etc.)
88103
*

packages/core/src/driver.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ class Driver {
157157
/**
158158
* Verifies connectivity of this driver by trying to open a connection with the provided driver options.
159159
*
160+
* @deprecated This return of this method will change in 6.0.0 to not return the {@link Promise<ServerInfo>} and return a
161+
* {@link Promise<void>} instead. If you need to use the server info, use {@link getServerInfo} instead.
162+
*
160163
* @public
161164
* @param {Object} param - The object parameter
162165
* @param {string} param.database - The target database to verify connectivity for.
@@ -166,8 +169,19 @@ class Driver {
166169
ServerInfo
167170
> {
168171
const connectionProvider = this._getOrCreateConnectionProvider()
169-
const connectivityVerifier = new ConnectivityVerifier(connectionProvider)
170-
return connectivityVerifier.verify({ database })
172+
return connectionProvider.verifyConnectivityAndGetServerInfo({ database, accessMode: READ })
173+
}
174+
175+
/**
176+
* Get ServerInfo for the giver database.
177+
*
178+
* @param {Object} param - The object parameter
179+
* @param {string} param.database - The target database to verify connectivity for.
180+
* @returns {Promise<void>} promise resolved with void or rejected with error.
181+
*/
182+
getServerInfo({ database = ''}: { database?: string } = {}): Promise<ServerInfo> {
183+
const connectionProvider = this._getOrCreateConnectionProvider()
184+
return connectionProvider.verifyConnectivityAndGetServerInfo({ database, accessMode: READ })
171185
}
172186

173187
/**

packages/neo4j-driver-lite/test/unit/index.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,8 @@ describe('index', () => {
221221
close: () => Promise.resolve(),
222222
supportsMultiDb: () => Promise.resolve(true),
223223
supportsTransactionConfig: () => Promise.resolve(true),
224-
supportsUserImpersonation: () => Promise.resolve(true)
224+
supportsUserImpersonation: () => Promise.resolve(true),
225+
verifyConnectivityAndGetServerInfo: () => Promise.resolve(new ServerInfo({}))
225226
}
226227
})
227228
expect(session).toBeDefined()

packages/testkit-backend/src/request-handlers.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,8 @@ export function GetFeatures (_context, _params, wire) {
381381
'Feature:Bolt:4.2',
382382
'Feature:Bolt:4.3',
383383
'Feature:Bolt:4.4',
384+
'Feature:API:Driver:GetServerInfo',
385+
'Feature:API:Driver.VerifyConnectivity',
384386
'Feature:API:Result.List',
385387
'Feature:API:Result.Peek',
386388
'Feature:Configuration:ConnectionAcquisitionTimeout',
@@ -410,6 +412,17 @@ export function VerifyConnectivity (context, { driverId }, wire) {
410412
.catch(error => wire.writeError(error))
411413
}
412414

415+
export function GetServerInfo (context, { driverId }, wire) {
416+
const driver = context.getDriver(driverId)
417+
return driver
418+
.getServerInfo()
419+
.then(info => wire.writeResponse('ServerInfo', {
420+
...info,
421+
protocolVersion: info.protocolVersion.toFixed(1)
422+
}))
423+
.catch(error => wire.writeError(error))
424+
}
425+
413426
export function CheckMultiDBSupport (context, { driverId }, wire) {
414427
const driver = context.getDriver(driverId)
415428
return driver

0 commit comments

Comments
 (0)