From e3828e2516fad1b8ea941964e3322737cefcea5f Mon Sep 17 00:00:00 2001 From: Sjoerd Smink Date: Wed, 8 Dec 2021 16:58:37 +0100 Subject: [PATCH 1/5] Add errorHandler option Add errorHandler option for adapter creation --- lib/index.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/index.ts b/lib/index.ts index f322f13..fe54424 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -80,6 +80,7 @@ export interface PostgresAdapterOptions { channelPrefix: string; /** * The name of the table for payloads over the 8000 bytes limit or containing binary data + * @default "socket_io_attachments" */ tableName: string; /** @@ -107,6 +108,11 @@ export interface PostgresAdapterOptions { * @default 30000 */ cleanupInterval: number; + /** + * Handler for errors. If undefined, it will terminate the process when an error occurs (see https://nodejs.org/api/events.html#error-events). + * @default undefined + */ + errorHandler: (err: any) => void | undefined } /** @@ -122,7 +128,11 @@ export function createAdapter( opts: Partial = {} ) { return function (nsp) { - return new PostgresAdapter(nsp, pool, opts); + const postgresAdapter = new PostgresAdapter(nsp, pool, opts); + if (opts.errorHandler) { + postgresAdapter.on("error", opts.errorHandler); + } + return postgresAdapter; }; } From d21ded06facbd27511bcc67358830fed78d90ff0 Mon Sep 17 00:00:00 2001 From: Sjoerd Smink Date: Wed, 8 Dec 2021 18:00:48 +0100 Subject: [PATCH 2/5] Catch pg pool errors --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index b900045..531e711 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,10 @@ pool.query(` ); `); +pool.on("error", (err) => { + console.error("Postgres error", err); +}); + io.adapter(createAdapter(pool)); io.listen(3000); ``` From e6c2dc8269b770e39b3cee0ed1a5152bc61081da Mon Sep 17 00:00:00 2001 From: Sjoerd Smink Date: Mon, 13 Dec 2021 09:17:31 +0100 Subject: [PATCH 3/5] Use errorHandler in PostgresAdapter --- lib/index.ts | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/lib/index.ts b/lib/index.ts index 276b183..d351a74 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -127,12 +127,8 @@ export function createAdapter( pool: any, opts: Partial = {} ) { - return function (nsp: any) { - const postgresAdapter = new PostgresAdapter(nsp, pool, opts); - if (opts.errorHandler) { - postgresAdapter.on("error", opts.errorHandler); - } - return postgresAdapter; + return function (nsp) { + return new PostgresAdapter(nsp, pool, opts); }; } @@ -145,6 +141,7 @@ export class PostgresAdapter extends Adapter { public heartbeatTimeout: number; public payloadThreshold: number; public cleanupInterval: number; + public errorHandler: (err: any) => void; private readonly pool: any; private client: any; @@ -174,6 +171,8 @@ export class PostgresAdapter extends Adapter { this.heartbeatTimeout = opts.heartbeatTimeout || 10000; this.payloadThreshold = opts.payloadThreshold || 8000; this.cleanupInterval = opts.cleanupInterval || 30000; + const defaultErrorHandler = (err: any) => this.emit("error", err); + this.errorHandler = opts.errorHandler || defaultErrorHandler this.initSubscription(); this.publish({ @@ -208,7 +207,7 @@ export class PostgresAdapter extends Adapter { try { await this.onEvent(msg.payload); } catch (err) { - this.emit("error", err); + this.errorHandler(err); } }); @@ -223,7 +222,7 @@ export class PostgresAdapter extends Adapter { this.client = client; } catch (err) { - this.emit("error", err); + this.errorHandler(err); debug("error while initializing client, scheduling reconnection..."); this.scheduleReconnection(); } @@ -401,7 +400,7 @@ export class PostgresAdapter extends Adapter { `DELETE FROM ${this.tableName} WHERE created_at < now() - interval '${this.cleanupInterval} milliseconds'` ); } catch (err) { - this.emit("error", err); + this.errorHandler(err); } this.scheduleCleanup(); }, this.cleanupInterval); @@ -439,7 +438,7 @@ export class PostgresAdapter extends Adapter { this.scheduleHeartbeat(); } catch (err) { - this.emit("error", err); + this.errorHandler(err); } } From 99481092ceeed795065ab24c17d87fe5b94e9806 Mon Sep 17 00:00:00 2001 From: Sjoerd Smink Date: Mon, 13 Dec 2021 09:18:53 +0100 Subject: [PATCH 4/5] Add removed any --- lib/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/index.ts b/lib/index.ts index d351a74..f8e5430 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -127,7 +127,7 @@ export function createAdapter( pool: any, opts: Partial = {} ) { - return function (nsp) { + return function (nsp: any) { return new PostgresAdapter(nsp, pool, opts); }; } From b1cb065c48859601ec13f7be7d87f7fd842034a0 Mon Sep 17 00:00:00 2001 From: Sjoerd Smink Date: Mon, 13 Dec 2021 09:33:27 +0100 Subject: [PATCH 5/5] Debug message instead of this.emit on error --- lib/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/index.ts b/lib/index.ts index f8e5430..185bbfe 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -171,8 +171,8 @@ export class PostgresAdapter extends Adapter { this.heartbeatTimeout = opts.heartbeatTimeout || 10000; this.payloadThreshold = opts.payloadThreshold || 8000; this.cleanupInterval = opts.cleanupInterval || 30000; - const defaultErrorHandler = (err: any) => this.emit("error", err); - this.errorHandler = opts.errorHandler || defaultErrorHandler + const defaultErrorHandler = (err: any) => debug(err); + this.errorHandler = opts.errorHandler || defaultErrorHandler; this.initSubscription(); this.publish({