-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Race condition when reading the service instance - SW Health Indicator #4906
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { WebSocketWorker } from './web-socket.worker'; | ||
export { WSHealthIndicator } from './ws-health-indicator.service'; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,25 @@ | ||||||
import { HealthIndicator, HealthIndicatorResult } from '@nestjs/terminus'; | ||||||
import { Injectable } from '@nestjs/common'; | ||||||
|
||||||
import { IHealthIndicator } from '@novu/application-generic'; | ||||||
|
||||||
import { WSGateway } from '../ws.gateway'; | ||||||
|
||||||
@Injectable() | ||||||
export class WSHealthIndicator extends HealthIndicator implements IHealthIndicator { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will rename in the upcoming PR's in order to make clear what the health indicator is responsible for.
Suggested change
|
||||||
private INDICATOR_KEY = 'ws'; | ||||||
|
||||||
constructor(private wsGateway: WSGateway) { | ||||||
super(); | ||||||
} | ||||||
|
||||||
async isHealthy(): Promise<HealthIndicatorResult> { | ||||||
const status = !!this.wsGateway.server; | ||||||
|
||||||
return this.getStatus(this.INDICATOR_KEY, status); | ||||||
} | ||||||
|
||||||
isActive(): Promise<HealthIndicatorResult> { | ||||||
return this.isHealthy(); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,18 +15,21 @@ export class ExternalServicesRoute { | |
|
||
public async execute(command: ExternalServicesRouteCommand) { | ||
const isOnline = await this.connectionExist(command); | ||
if (isOnline) { | ||
if (command.event === WebSocketEventEnum.RECEIVED) { | ||
await this.processReceivedEvent(command); | ||
} | ||
|
||
if (command.event === WebSocketEventEnum.UNSEEN) { | ||
await this.sendUnseenCountChange(command); | ||
} | ||
|
||
if (command.event === WebSocketEventEnum.UNREAD) { | ||
await this.sendUnreadCountChange(command); | ||
} | ||
|
||
if (!isOnline) { | ||
return; | ||
} | ||
|
||
if (command.event === WebSocketEventEnum.RECEIVED) { | ||
await this.processReceivedEvent(command); | ||
} | ||
|
||
if (command.event === WebSocketEventEnum.UNSEEN) { | ||
await this.sendUnseenCountChange(command); | ||
} | ||
|
||
if (command.event === WebSocketEventEnum.UNREAD) { | ||
await this.sendUnreadCountChange(command); | ||
Comment on lines
+18
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small refactor, not related to the PR |
||
} | ||
} | ||
|
||
|
@@ -127,7 +130,13 @@ export class ExternalServicesRoute { | |
} | ||
} | ||
|
||
private async connectionExist(command: ExternalServicesRouteCommand) { | ||
private async connectionExist(command: ExternalServicesRouteCommand): Promise<boolean | undefined> { | ||
if (!this.wsGateway.server) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the server is not initialized we want to log it. |
||
Logger.error('No sw server found, unable to check if connection exists', LOG_CONTEXT); | ||
|
||
return; | ||
} | ||
|
||
return !!(await this.wsGateway.server.sockets.in(command.userId).fetchSockets()).length; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,21 @@ | ||
const nr = require('newrelic'); | ||
|
||
import { JwtService } from '@nestjs/jwt'; | ||
import { OnGatewayConnection, OnGatewayDisconnect, WebSocketGateway, WebSocketServer } from '@nestjs/websockets'; | ||
import { Logger } from '@nestjs/common'; | ||
import { Server, Socket } from 'socket.io'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
|
||
import { ISubscriberJwt, ObservabilityBackgroundTransactionEnum } from '@novu/shared'; | ||
|
||
import { SubscriberOnlineService } from '../shared/subscriber-online'; | ||
|
||
const LOG_CONTEXT = 'WSGateway'; | ||
|
||
@WebSocketGateway() | ||
export class WSGateway implements OnGatewayConnection, OnGatewayDisconnect { | ||
constructor(private jwtService: JwtService, private subscriberOnlineService: SubscriberOnlineService) {} | ||
|
||
@WebSocketServer() | ||
server: Server; | ||
server: Server | null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now we know that there is an edge case where the Server can be nullish. |
||
|
||
async handleDisconnect(connection: Socket) { | ||
// eslint-disable-next-line @typescript-eslint/no-this-alias | ||
|
@@ -112,6 +116,12 @@ export class WSGateway implements OnGatewayConnection, OnGatewayDisconnect { | |
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
async sendMessage(userId: string, event: string, data: any) { | ||
if (!this.server) { | ||
Logger.error('No sw server available to send message', LOG_CONTEXT); | ||
|
||
Comment on lines
+120
to
+121
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the server is not initialized we want to log it. |
||
return; | ||
} | ||
|
||
this.server.to(userId).emit(event, data); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now we will check if the WS server is up.