Skip to content

Commit

Permalink
fix: cleanup error handler to prevent memory leak (#490)
Browse files Browse the repository at this point in the history
  • Loading branch information
carera committed Apr 13, 2023
1 parent e70b1bd commit b5da02d
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions lib/index.ts
Expand Up @@ -106,6 +106,7 @@ export class RedisAdapter extends Adapter {
private requests: Map<string, Request> = new Map();
private ackRequests: Map<string, AckRequest> = new Map();
private redisListeners: Map<string, Function> = new Map();
private readonly friendlyErrorHandler: () => void;

/**
* Adapter constructor.
Expand Down Expand Up @@ -183,16 +184,13 @@ export class RedisAdapter extends Adapter {
);
}

const registerFriendlyErrorHandler = (redisClient) => {
redisClient.on("error", () => {
if (redisClient.listenerCount("error") === 1) {
console.warn("missing 'error' handler on this Redis client");
}
});
this.friendlyErrorHandler = function () {
if (this.listenerCount("error") === 1) {
console.warn("missing 'error' handler on this Redis client");
}
};

registerFriendlyErrorHandler(this.pubClient);
registerFriendlyErrorHandler(this.subClient);
this.pubClient.on("error", this.friendlyErrorHandler);
this.subClient.on("error", this.friendlyErrorHandler);
}

/**
Expand Down Expand Up @@ -981,6 +979,9 @@ export class RedisAdapter extends Adapter {
this.redisListeners.get("messageBuffer")
);
}

this.pubClient.off("error", this.friendlyErrorHandler);
this.subClient.off("error", this.friendlyErrorHandler);
}
}

Expand Down

0 comments on commit b5da02d

Please sign in to comment.