Skip to content

Commit

Permalink
more
Browse files Browse the repository at this point in the history
  • Loading branch information
cirospaciari committed Jan 14, 2025
1 parent 8e94113 commit 7b88aec
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions src/js/bun/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,20 @@ class ConnectionPool {

release(connection: PooledConnection) {
if (this.waitingQueue.length > 0) {
if (connection.storedError) {
// this connection got a error but maybe we can wait for another

if (this.hasConnectionsAvailable()) {
return;
}

// we have no connections available so lets fails
let pending;
while ((pending = this.waitingQueue.shift())) {
pending(connection.storedError, connection);
}
return;
}
// we have some pending connections, lets connect them with the released connection
const pending = this.waitingQueue.shift();
pending?.(connection.storedError, connection);
Expand All @@ -378,12 +392,28 @@ class ConnectionPool {
}
}

hasConnectionsAvailable() {
if (this.readyConnections.size > 0) return true;
if (this.poolStarted) {
const pollSize = this.connections.length;
for (let i = 0; i < pollSize; i++) {
const connection = this.connections[i];
if (connection.state !== "closed") {
// some connection is connecting or connected
return true;
}
}
}
return false;
}

isConnected() {
if (this.readyConnections.size > 0) {
return true;
}
if (this.poolStarted) {
for (let i = 0; i < this.connections.length; i++) {
const pollSize = this.connections.length;
for (let i = 0; i < pollSize; i++) {
const connection = this.connections[i];
if (connection.state === "connected") {
return true;
Expand All @@ -397,8 +427,8 @@ class ConnectionPool {
return;
}
if (this.poolStarted) {
this.poolStarted = false;
for (let i = 0; i < this.connections.length; i++) {
const pollSize = this.connections.length;
for (let i = 0; i < pollSize; i++) {
const connection = this.connections[i];
if (connection.state === "connected") {
connection.connection.flush();
Expand All @@ -418,7 +448,8 @@ class ConnectionPool {
const promises: Array<Promise<any>> = [];
if (this.poolStarted) {
this.poolStarted = false;
for (let i = 0; i < this.connections.length; i++) {
const pollSize = this.connections.length;
for (let i = 0; i < pollSize; i++) {
const connection = this.connections[i];
switch (connection.state) {
case "pending":
Expand Down

0 comments on commit 7b88aec

Please sign in to comment.