* Gets a connection from the next host according to the query plan or throws a NoHostAvailableError. * @returns {{host, connection}} * @throws {NoHostAvailableError}
()
| 72 | * @throws {NoHostAvailableError} |
| 73 | */ |
| 74 | getNextConnection() { |
| 75 | let host; |
| 76 | let connection; |
| 77 | const iterator = this._hostIterator; |
| 78 | |
| 79 | // Get a host that is UP in a sync loop |
| 80 | while (true) { |
| 81 | const item = iterator.next(); |
| 82 | if (item.done) { |
| 83 | throw new errors.NoHostAvailableError(this.triedHosts); |
| 84 | } |
| 85 | |
| 86 | host = item.value; |
| 87 | |
| 88 | // Set the distance relative to the client first |
| 89 | const distance = this.client.profileManager.getDistance(host); |
| 90 | if (distance === types.distance.ignored) { |
| 91 | //If its marked as ignore by the load balancing policy, move on. |
| 92 | continue; |
| 93 | } |
| 94 | |
| 95 | if (!host.isUp()) { |
| 96 | this.triedHosts[host.address] = 'Host considered as DOWN'; |
| 97 | continue; |
| 98 | } |
| 99 | |
| 100 | try { |
| 101 | connection = host.borrowConnection(); |
| 102 | this.triedHosts[host.address] = null; |
| 103 | break; |
| 104 | } catch (err) { |
| 105 | this.triedHosts[host.address] = err; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | return { connection, host }; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Gets an available connection and sends the request |
no test coverage detected