Skip to content

Commit

Permalink
cleanup pool
Browse files Browse the repository at this point in the history
  • Loading branch information
halvardssm committed Apr 28, 2024
1 parent 2980d63 commit 4b475f4
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 17 deletions.
13 changes: 3 additions & 10 deletions lib/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ import {
MysqlPoolAcquireEvent,
MysqlPoolCloseEvent,
MysqlPoolConnectEvent,
MysqlPoolDestroyEvent,
MysqlPoolReleaseEvent,
} from "./utils/events.ts";
import { logger } from "./utils/logger.ts";
import { MysqlClientEventTarget } from "./utils/events.ts";

export interface MysqlClientPoolOptions
Expand Down Expand Up @@ -129,6 +127,9 @@ export class MysqlClientPool extends SqlxBase implements

async acquire(): Promise<MysqlPoolClient> {
const client = await this.deferredStack.pop();
if (!client.connected) {
await client.connection.connect();
}

this.eventTarget.dispatchEvent(
new MysqlPoolAcquireEvent({ connectable: client }),
Expand All @@ -144,7 +145,6 @@ export class MysqlClientPool extends SqlxBase implements
this.deferredStack.push(client);
} catch (e) {
if (e instanceof SqlxError && e.message === "Max pool size reached") {
logger().debug(e.message);
await client.connection.close();
throw e;
} else {
Expand All @@ -153,13 +153,6 @@ export class MysqlClientPool extends SqlxBase implements
}
}

async destroy(client: MysqlPoolClient): Promise<void> {
this.eventTarget.dispatchEvent(
new MysqlPoolDestroyEvent({ connectable: client }),
);
await client.connection.close();
}

async [Symbol.asyncDispose](): Promise<void> {
await this.close();
}
Expand Down
8 changes: 1 addition & 7 deletions lib/utils/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
type SqlxConnectableEventInit,
SqlxEventTarget,
SqlxPoolConnectableAcquireEvent,
SqlxPoolConnectableDestroyEvent,
SqlxPoolConnectableReleaseEvent,
type SqlxPoolConnectionEventType,
} from "@halvardm/sqlx";
Expand Down Expand Up @@ -58,10 +57,6 @@ export class MysqlPoolReleaseEvent
extends SqlxPoolConnectableReleaseEvent<MysqlPoolEventInit> {
}

export class MysqlPoolDestroyEvent
extends SqlxPoolConnectableDestroyEvent<MysqlPoolEventInit> {
}

export type MysqlClientEvents =
| MysqlClientConnectEvent
| MysqlClientCloseEvent;
Expand All @@ -70,5 +65,4 @@ export type MysqlPoolEvents =
| MysqlClientConnectEvent
| MysqlClientCloseEvent
| MysqlPoolAcquireEvent
| MysqlPoolReleaseEvent
| MysqlPoolDestroyEvent;
| MysqlPoolReleaseEvent;

0 comments on commit 4b475f4

Please sign in to comment.