Skip to content

Commit

Permalink
Merge pull request #193 from CS3219-AY2425S1/lynn-fix-match
Browse files Browse the repository at this point in the history
Handle Infinite Timeout Bug When Question Doesnt Exist
  • Loading branch information
shishirbychapur authored Nov 6, 2024
2 parents 3b2d1c8 + b0f2afd commit 1d70507
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
16 changes: 9 additions & 7 deletions backend/matching-service/src/controllers/matching.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
import { getRandomQuestion } from '../services/matching.service'
import { convertComplexityToSortedComplexity } from '@repo/question-types'
import { UserQueueRequest, UserQueueRequestDto } from '../types/UserQueueRequestDto'
import loggerUtil from '../common/logger.util'

export async function generateWS(request: ITypedBodyRequest<void>, response: Response): Promise<void> {
const userHasMatch = await isUserInMatch(request.user.id)
Expand Down Expand Up @@ -57,23 +58,24 @@ export async function removeUserFromMatchingQueue(websocketId: string, userId: s
wsConnection.sendMessageToUser(websocketId, JSON.stringify({ type: WebSocketMessageType.CANCEL }))
}

export async function handleCreateMatch(data: IMatch, ws1: string, ws2: string): Promise<IMatch> {
export async function handleCreateMatch(data: IMatch, ws1: string, ws2: string): Promise<IMatch | undefined> {
const isAnyUserInMatch = (await isUserInMatch(data.user1Id)) || (await isUserInMatch(data.user2Id))
if (isAnyUserInMatch) {
wsConnection.sendMessageToUser(ws1, JSON.stringify({ type: WebSocketMessageType.DUPLICATE }))
wsConnection.sendMessageToUser(ws2, JSON.stringify({ type: WebSocketMessageType.DUPLICATE }))
}

const question = await getRandomQuestion(data.category, convertComplexityToSortedComplexity(data.complexity))

if (!question) {
throw new Error('Question not found')
let question = undefined
try {
question = await getRandomQuestion(data.category, convertComplexityToSortedComplexity(data.complexity))
} catch (error) {
loggerUtil.error('Error while fetching question', error)
return
}

const createDto = MatchDto.fromJSON({ ...data, question })
const errors = await createDto.validate()
if (errors.length) {
throw new Error('Invalid match data')
return
}
const dto = await createMatch(createDto)
wsConnection.sendMessageToUser(ws1, JSON.stringify({ type: WebSocketMessageType.SUCCESS, matchId: dto.id }))
Expand Down
12 changes: 10 additions & 2 deletions backend/matching-service/src/services/rabbitmq.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,20 @@ class RabbitMQConnection {
category: content.topic,
complexity: content.complexity,
}
await handleCreateMatch(match as IMatch, content.websocketId, matchedUserContent.websocketId)
const res = await handleCreateMatch(
match as IMatch,
content.websocketId,
matchedUserContent.websocketId
)
this.currentUsers.delete(content.userId)
this.currentUsers.delete(matchedUserContent.userId)
this.cancelledUsers.delete(content.websocketId)
this.cancelledUsers.delete(matchedUserContent.websocketId)
logger.info(`[Match] Match created and stored successfully: ${JSON.stringify(match)}`)
if (!res) {
logger.error(`[Match] Failed to create match: ${JSON.stringify(match)}`)
} else {
logger.info(`[Match] Match created and stored successfully: ${JSON.stringify(match)}`)
}
}
} catch (error) {
logger.error(`[Entry-Queue] Issue checking with Waiting-Queue: ${error}`)
Expand Down
3 changes: 3 additions & 0 deletions frontend/components/dashboard/new-session.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ export const NewSession = () => {
const socket = new WebSocket(
process.env.NEXT_PUBLIC_API_URL?.concat(`/matching/ws/?id=${websocketId}`) ?? 'ws://localhost:3006'
)
setTimeout(() => {
socket.close()
}, 60000)
socketRef.current = socket
socketRef.current.onclose = () => {
updateMatchmakingStatus(MatchingStatus.MATCH_NOT_FOUND)
Expand Down

0 comments on commit 1d70507

Please sign in to comment.