|
1 | 1 | import amqplib, { Connection } from "amqplib";
|
2 | 2 | import dotenv from "dotenv";
|
3 |
| -import { matchUsers } from "../utils/messageQueue"; |
4 |
| -import { MatchRequestItem } from "../handlers/matchHandler"; |
| 3 | +import { matchUsers } from "../handlers/matchHandler"; |
5 | 4 | import { Complexities, Categories, Languages } from "../utils/constants";
|
| 5 | +import { MatchRequest, MatchRequestItem } from "../utils/types"; |
6 | 6 |
|
7 | 7 | dotenv.config();
|
8 | 8 |
|
9 | 9 | const RABBITMQ_ADDR = process.env.RABBITMQ_ADDR || "amqp://localhost:5672";
|
| 10 | +const QUEUE_NAME_DELIMITER = "_"; |
10 | 11 |
|
11 | 12 | let mrConnection: Connection;
|
12 |
| -const queues: string[] = []; |
13 |
| -const pendingQueueRequests = new Map<string, Map<string, MatchRequestItem>>(); |
| 13 | +const waitingLists = new Map<string, Map<string, MatchRequestItem>>(); |
14 | 14 |
|
15 |
| -const initQueueNames = () => { |
16 |
| - for (const complexity of Object.values(Complexities)) { |
17 |
| - for (const category of Object.values(Categories)) { |
18 |
| - for (const language of Object.values(Languages)) { |
19 |
| - queues.push(`${complexity}_${category}_${language}`); |
20 |
| - } |
| 15 | +export const connectToRabbitMq = async () => { |
| 16 | + try { |
| 17 | + mrConnection = await amqplib.connect(RABBITMQ_ADDR); |
| 18 | + const queues = setUpQueueNames(); |
| 19 | + for (const queue of queues) { |
| 20 | + await setUpConsumer(queue); |
| 21 | + getWaitingList(queue); |
21 | 22 | }
|
| 23 | + } catch (error) { |
| 24 | + console.error(error); |
| 25 | + process.exit(1); |
22 | 26 | }
|
23 | 27 | };
|
24 | 28 |
|
25 |
| -const setUpQueue = async (queueName: string) => { |
| 29 | +export const sendToProducer = async ( |
| 30 | + matchRequest: MatchRequest, |
| 31 | + requestId: string, |
| 32 | + rejectedPartnerId?: string |
| 33 | +): Promise<boolean> => { |
| 34 | + const { user, complexity, category, language, timeout } = matchRequest; |
| 35 | + |
| 36 | + const requestItem: MatchRequestItem = { |
| 37 | + id: requestId, |
| 38 | + user: user, |
| 39 | + sentTimestamp: Date.now(), |
| 40 | + ttlInSecs: timeout, |
| 41 | + rejectedPartnerId: rejectedPartnerId, |
| 42 | + }; |
| 43 | + |
| 44 | + const sent = await routeToQueue( |
| 45 | + [complexity, category, language], |
| 46 | + requestItem |
| 47 | + ); |
| 48 | + return sent; |
| 49 | +}; |
| 50 | + |
| 51 | +const setUpConsumer = async (queueName: string) => { |
26 | 52 | const consumerChannel = await mrConnection.createChannel();
|
27 |
| - await consumerChannel.assertQueue(queueName); |
| 53 | + await consumerChannel.assertQueue(queueName, { durable: true }); |
28 | 54 |
|
29 | 55 | consumerChannel.consume(queueName, (msg) => {
|
30 | 56 | if (msg !== null) {
|
31 |
| - matchUsers(queueName, msg.content.toString()); |
| 57 | + const matchRequestItem = JSON.parse(msg.content.toString()); |
| 58 | + const waitingList = getWaitingList(queueName); |
| 59 | + const [complexity, category] = deconstructQueueName(queueName); |
| 60 | + matchUsers(matchRequestItem, waitingList, complexity, category); |
32 | 61 | consumerChannel.ack(msg);
|
33 | 62 | }
|
34 | 63 | });
|
35 | 64 | };
|
36 | 65 |
|
37 |
| -export const connectToRabbitMq = async () => { |
38 |
| - try { |
39 |
| - initQueueNames(); |
40 |
| - mrConnection = await amqplib.connect(RABBITMQ_ADDR); |
41 |
| - for (const queue of queues) { |
42 |
| - await setUpQueue(queue); |
43 |
| - pendingQueueRequests.set(queue, new Map<string, MatchRequestItem>()); |
44 |
| - } |
45 |
| - } catch (error) { |
46 |
| - console.error(error); |
47 |
| - process.exit(1); |
48 |
| - } |
49 |
| -}; |
50 |
| - |
51 |
| -export const sendToQueue = async ( |
52 |
| - complexity: string, |
53 |
| - category: string, |
54 |
| - language: string, |
55 |
| - data: MatchRequestItem |
| 66 | +const routeToQueue = async ( |
| 67 | + criterias: string[], |
| 68 | + requestItem: MatchRequestItem |
56 | 69 | ): Promise<boolean> => {
|
57 | 70 | try {
|
58 |
| - const queueName = `${complexity}_${category}_${language}`; |
| 71 | + const queueName = constructQueueName(criterias); |
59 | 72 | const senderChannel = await mrConnection.createChannel();
|
60 |
| - senderChannel.sendToQueue(queueName, Buffer.from(JSON.stringify(data))); |
| 73 | + senderChannel.sendToQueue( |
| 74 | + queueName, |
| 75 | + Buffer.from(JSON.stringify(requestItem)), |
| 76 | + { |
| 77 | + persistent: true, |
| 78 | + } |
| 79 | + ); |
61 | 80 | return true;
|
62 | 81 | } catch (error) {
|
63 | 82 | console.log(error);
|
64 | 83 | return false;
|
65 | 84 | }
|
66 | 85 | };
|
67 | 86 |
|
68 |
| -export const getPendingRequests = ( |
69 |
| - queueName: string |
70 |
| -): Map<string, MatchRequestItem> => { |
71 |
| - return pendingQueueRequests.get(queueName)!; |
| 87 | +const setUpQueueNames = () => { |
| 88 | + const queues = []; |
| 89 | + for (const complexity of Object.values(Complexities)) { |
| 90 | + for (const category of Object.values(Categories)) { |
| 91 | + for (const language of Object.values(Languages)) { |
| 92 | + const queueName = constructQueueName([complexity, category, language]); |
| 93 | + queues.push(queueName); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + return queues; |
| 98 | +}; |
| 99 | + |
| 100 | +const constructQueueName = (criterias: string[]) => { |
| 101 | + return criterias.join(QUEUE_NAME_DELIMITER); |
| 102 | +}; |
| 103 | + |
| 104 | +const deconstructQueueName = (queueName: string) => { |
| 105 | + return queueName.split(QUEUE_NAME_DELIMITER); |
| 106 | +}; |
| 107 | + |
| 108 | +const getWaitingList = (queueName: string): Map<string, MatchRequestItem> => { |
| 109 | + if (!waitingLists.has(queueName)) { |
| 110 | + waitingLists.set(queueName, new Map<string, MatchRequestItem>()); |
| 111 | + } |
| 112 | + return waitingLists.get(queueName)!; |
72 | 113 | };
|
0 commit comments