|
| 1 | +/** |
| 2 | + * Copyright (c) 2002-2017 "Neo Technology,"," |
| 3 | + * Network Engine for Objects in Lund AB [http://neotechnology.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import {newError} from '../error'; |
| 21 | + |
| 22 | +/** |
| 23 | + * Utility to lazily initialize connections and return them back to the pool when unused. |
| 24 | + */ |
| 25 | +export default class ConnectionHolder { |
| 26 | + |
| 27 | + /** |
| 28 | + * @constructor |
| 29 | + * @param {string} mode - the access mode for new connection holder. |
| 30 | + * @param {ConnectionProvider} connectionProvider - the connection provider to acquire connections from. |
| 31 | + */ |
| 32 | + constructor(mode, connectionProvider) { |
| 33 | + this._mode = mode; |
| 34 | + this._connectionProvider = connectionProvider; |
| 35 | + this._referenceCount = 0; |
| 36 | + this._connectionPromise = Promise.resolve(null); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Make this holder initialize new connection if none exists already. |
| 41 | + * @return {undefined} |
| 42 | + */ |
| 43 | + initializeConnection() { |
| 44 | + if (this._referenceCount === 0) { |
| 45 | + this._connectionPromise = this._connectionProvider.acquireConnection(this._mode); |
| 46 | + } |
| 47 | + this._referenceCount++; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Get the current connection promise. |
| 52 | + * @return {Promise<Connection>} promise resolved with the current connection. |
| 53 | + */ |
| 54 | + getConnection() { |
| 55 | + return this._connectionPromise; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Notify this holder that single party does not require current connection any more. |
| 60 | + * @return {Promise<Connection>} promise resolved with the current connection. |
| 61 | + */ |
| 62 | + releaseConnection() { |
| 63 | + if (this._referenceCount === 0) { |
| 64 | + return this._connectionPromise; |
| 65 | + } |
| 66 | + |
| 67 | + this._referenceCount--; |
| 68 | + if (this._referenceCount === 0) { |
| 69 | + // release a connection without muting ACK_FAILURE, this is the last action on this connection |
| 70 | + return this._releaseConnection(true); |
| 71 | + } |
| 72 | + return this._connectionPromise; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Closes this holder and releases current connection (if any) despite any existing users. |
| 77 | + * @return {Promise<Connection>} promise resolved when current connection is released to the pool. |
| 78 | + */ |
| 79 | + close() { |
| 80 | + if (this._referenceCount === 0) { |
| 81 | + return this._connectionPromise; |
| 82 | + } |
| 83 | + this._referenceCount = 0; |
| 84 | + // release a connection and mute ACK_FAILURE, this might be called concurrently with other |
| 85 | + // operations and thus should ignore failure handling |
| 86 | + return this._releaseConnection(false); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Return the current pooled connection instance to the connection pool. |
| 91 | + * We don't pool Session instances, to avoid users using the Session after they've called close. |
| 92 | + * The `Session` object is just a thin wrapper around Connection anyway, so it makes little difference. |
| 93 | + * @return {Promise} - promise resolved then connection is returned to the pool. |
| 94 | + * @private |
| 95 | + */ |
| 96 | + _releaseConnection(sync) { |
| 97 | + this._connectionPromise = this._connectionPromise.then(connection => { |
| 98 | + if (connection) { |
| 99 | + if(sync) { |
| 100 | + connection.reset(); |
| 101 | + } else { |
| 102 | + connection.resetAsync(); |
| 103 | + } |
| 104 | + connection.sync(); |
| 105 | + connection._release(); |
| 106 | + } |
| 107 | + }).catch(ignoredError => { |
| 108 | + }); |
| 109 | + |
| 110 | + return this._connectionPromise; |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +class EmptyConnectionHolder extends ConnectionHolder { |
| 115 | + |
| 116 | + initializeConnection() { |
| 117 | + // nothing to initialize |
| 118 | + } |
| 119 | + |
| 120 | + getConnection() { |
| 121 | + return Promise.reject(newError('This connection holder does not serve connections')); |
| 122 | + } |
| 123 | + |
| 124 | + releaseConnection() { |
| 125 | + return Promise.resolve(); |
| 126 | + } |
| 127 | + |
| 128 | + close() { |
| 129 | + return Promise.resolve(); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +/** |
| 134 | + * Connection holder that does not manage any connections. |
| 135 | + * @type {ConnectionHolder} |
| 136 | + */ |
| 137 | +export const EMPTY_CONNECTION_HOLDER = new EmptyConnectionHolder(); |
0 commit comments