Skip to content

Commit

Permalink
fix randomMatching performance
Browse files Browse the repository at this point in the history
  • Loading branch information
root14 committed Aug 8, 2024
1 parent 77e78b4 commit 40dd524
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 66 deletions.
57 changes: 28 additions & 29 deletions src/controller/wscontroller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { v4 as uuid, v4 } from "uuid"
import WebSocket from "ws";

import { jwtValidate } from "../util/jwtValidate"
import { joinPool, randomMatch } from "../matchFinder";
import { randomMatch } from "../matchFinder";

const wsPort = process.env.WS_PORT || 3003

Expand All @@ -14,41 +14,40 @@ const handleWs = () => {
wsServer.on("connection", async (connection, request) => {
console.log(`Client connected`)

if (jwtValidate(request.headers.authorization || "")) {

connection.on("error", (error: Error) => {
console.log("Connection Error: " + error.message)
})

connection.on("close", () => {
console.log("Client connection closed");
})

connection.on("message", async (message: string) => {
const { userId } = JSON.parse(message)
joinPool(userId)

let findedUser
if (!jwtValidate(request.headers.authorization || "")) {
//cannot validate jwt
console.log("cannot validate jwt for a user")
connection.send("cannot validate your jwt")
connection.close()
return
}

findedUser = await randomMatch(userId)
connection.on("error", (error: Error) => {
console.log("Connection Error: " + error.message)
})

/*
await setInterval(async () => {
findedUser = randomMatch()
}, 5000)*/
connection.on("close", () => {
console.log("Client connection closed");
})

connection.on("message", async (message: string) => {
const { userId } = JSON.parse(message)
let findedUser = await randomMatch(userId)

console.log(`finded user is ${findedUser}`)
})

/** await setInterval(async () => {
randomMatch(userId).then((result) => {
console.log(`result is ${result}`)
if (result instanceof String) {
findedUser = result
}
})
}, 5000) */


} else {
//cannot validate jwt
console.log("cannot validate jwt for a user")
connection.send("cannot validate your jwt")
connection.close()
}
console.log(`finded user is ${findedUser}`)
})
})

wsServer.on("error", (error: Error) => {
Expand Down
51 changes: 14 additions & 37 deletions src/matchFinder.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,25 @@
import { redisClient } from "./data/redisConnector"

async function joinPool(userId: string) {
//insert user to pool
await redisClient.sAdd('user-pool', userId)
//expire unMathced user
await redisClient.expire(`user-pool${userId}`, 3600)

}

async function randomMatch(userId: string) {
try {
//limited as a precaution. OutOfMemoryException.


const cardinality = await redisClient.sCard('user-pool')

if (cardinality > 2) {

const poolUserList = await redisClient.sPop('user-pool')
if (poolUserList.at(0) === userId) {
await redisClient.sAdd('user-pool', userId)
return "kendine denk geldin"
} else {
return poolUserList
}
} else {
return "not enough peope in pool"
}

} catch (err) {
console.log(err)
return `match finder err ${err}`
const cardinality = await redisClient.sCard('user-pool')
await redisClient.expire(`user-pool${userId}`, 3600)
let poolUser

if (cardinality < 1) {
//if no user in pool, add user
await redisClient.sAdd('user-pool', userId)
} else {
//if someone waiting in pool, match
poolUser = await redisClient.sPop('user-pool')
}
}


function locationBased(userId: string) {



return poolUser
}

function locationBasedMatch(userId: string) { }

export {
randomMatch, locationBased, joinPool
locationBasedMatch, randomMatch
}


Expand Down
12 changes: 12 additions & 0 deletions src/util/finderStates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @constant ERR -> on error
* @constant RAND_FAIL -> across yourself
* @constant NOT_ENOUGH_USER -> when there are not enough people in the pool
*/

enum FinderStates {
ERR = "0",
RAND_FAIL = "1",
NOTE_NOUGH_USER = "2"
}
export default FinderStates

0 comments on commit 40dd524

Please sign in to comment.