Skip to content

Commit

Permalink
feat: add postgres pool error handler (#4474)
Browse files Browse the repository at this point in the history
Add option to customize pool error handling. Users can decide to log these errors with higher level (eg. error), crash application or reconnect.
  • Loading branch information
Ginden authored and pleerock committed Sep 5, 2019
1 parent f65ecc7 commit a925be9
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 3 deletions.
4 changes: 4 additions & 0 deletions docs/connection-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ See [SSL options](https://github.com/mysqljs/mysql#ssl-options).

* `uuidExtension` - The Postgres extension to use when generating UUIDs. Defaults to `uuid-ossp`. Can be changed to `pgcrypto` if the `uuid-ossp` extension is unavailable.

* `poolErrorHandler` - A function that get's called when underlying pool emits `'error'` event. Takes single parameter (error instance) and defaults to logging with `warn` level.

## `sqlite` connection options

* `database` - Database path. For example "./mydb.sql"
Expand Down Expand Up @@ -258,6 +260,8 @@ See [SSL options](https://github.com/mysqljs/mysql#ssl-options).
* `pool.idleTimeoutMillis` - the minimum amount of time that an object may sit idle in the pool before it is eligible for
eviction due to idle time. Supersedes `softIdleTimeoutMillis`. Default: `30000`.

* `pool.errorHandler` - A function that get's called when underlying pool emits `'error'` event. Takes single parameter (error instance) and defaults to logging with `warn` level.

* `options.fallbackToDefaultDb` - By default, if the database requestion by `options.database` cannot be accessed, the connection
will fail with an error. However, if `options.fallbackToDefaultDb` is set to `true`, then the user's default database will
be used instead (Default: `false`).
Expand Down
7 changes: 7 additions & 0 deletions src/driver/cockroachdb/CockroachConnectionOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,11 @@ export interface CockroachConnectionOptions extends BaseConnectionOptions, Cockr

};


/*
* Function handling errors thrown by drivers pool.
* Defaults to logging error with `warn` level.
*/
readonly poolErrorHandler?: (err: any) => any;

}
5 changes: 4 additions & 1 deletion src/driver/cockroachdb/CockroachDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -710,11 +710,14 @@ export class CockroachDriver implements Driver {
// create a connection pool
const pool = new this.postgres.Pool(connectionOptions);
const { logger } = this.connection;

const poolErrorHandler = options.poolErrorHandler || ((error: any) => logger.log("warn", `Postgres pool raised an error. ${error}`));

/*
Attaching an error handler to pool errors is essential, as, otherwise, errors raised will go unhandled and
cause the hosting app to crash.
*/
pool.on("error", (error: any) => logger.log("warn", `Postgres pool raised an error. ${error}`));
pool.on("error", poolErrorHandler);

return new Promise((ok, fail) => {
pool.connect((err: any, connection: any, release: Function) => {
Expand Down
7 changes: 7 additions & 0 deletions src/driver/postgres/PostgresConnectionOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,11 @@ export interface PostgresConnectionOptions extends BaseConnectionOptions, Postgr
* If uuid-ossp is selected, TypeORM will use the uuid_generate_v4() function from this extension.
*/
readonly uuidExtension?: "pgcrypto" | "uuid-ossp";


/*
* Function handling errors thrown by drivers pool.
* Defaults to logging error with `warn` level.
*/
readonly poolErrorHandler?: (err: any) => any;
}
5 changes: 4 additions & 1 deletion src/driver/postgres/PostgresDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -894,11 +894,14 @@ export class PostgresDriver implements Driver {
// create a connection pool
const pool = new this.postgres.Pool(connectionOptions);
const { logger } = this.connection;

const poolErrorHandler = options.poolErrorHandler || ((error: any) => logger.log("warn", `Postgres pool raised an error. ${error}`));

/*
Attaching an error handler to pool errors is essential, as, otherwise, errors raised will go unhandled and
cause the hosting app to crash.
*/
pool.on("error", (error: any) => logger.log("warn", `Postgres pool raised an error. ${error}`));
pool.on("error", poolErrorHandler);

return new Promise((ok, fail) => {
pool.connect((err: any, connection: any, release: Function) => {
Expand Down
7 changes: 7 additions & 0 deletions src/driver/sqlserver/SqlServerConnectionOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ export interface SqlServerConnectionOptions extends BaseConnectionOptions, SqlSe
* to idle time. Supercedes softIdleTimeoutMillis Default: 30000
*/
readonly idleTimeoutMillis?: number;

/*
* Function handling errors thrown by drivers pool.
* Defaults to logging error with `warn` level.
*/
readonly errorHandler?: (err: any) => any;
};

/**
Expand Down Expand Up @@ -271,4 +277,5 @@ export interface SqlServerConnectionOptions extends BaseConnectionOptions, SqlSe

};


}
4 changes: 3 additions & 1 deletion src/driver/sqlserver/SqlServerDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -749,11 +749,13 @@ export class SqlServerDriver implements Driver {
const pool = new this.mssql.ConnectionPool(connectionOptions);

const { logger } = this.connection;

const poolErrorHandler = (options.pool && options.pool.errorHandler) || ((error: any) => logger.log("warn", `MSSQL pool raised an error. ${error}`));
/*
Attaching an error handler to pool errors is essential, as, otherwise, errors raised will go unhandled and
cause the hosting app to crash.
*/
pool.on("error", (error: any) => logger.log("warn", `MSSQL pool raised an error. ${error}`));
pool.on("error", poolErrorHandler);

const connection = pool.connect((err: any) => {
if (err) return fail(err);
Expand Down

0 comments on commit a925be9

Please sign in to comment.