Skip to content

Commit

Permalink
fix: mutex on port allocate (#1346)
Browse files Browse the repository at this point in the history
* fix: mutex on port allocate

* fix: mutex on port allocate
  • Loading branch information
xiangmy21 committed May 13, 2024
1 parent 400f02c commit f37e759
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 23 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@babel/runtime": "7.21.5",
"@types/node-cron": "3.0.7",
"@types/web-push": "3.6.3",
"async-mutex": "^0.5.0",
"bcrypt": "5.1.0",
"cors": "2.8.5",
"cos-nodejs-sdk-v5": "^2.11.19",
Expand Down
36 changes: 18 additions & 18 deletions src/helpers/docker_queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as fs_promises from "fs/promises"
import * as hasura from "./hasura";
import * as utils from "./utils";
import yaml from "js-yaml";
import { Mutex } from "async-mutex"

export interface queue_element {
contest_id: string;
Expand All @@ -20,24 +21,27 @@ export interface queue_element {
envoy: number;
}

const get_port = async () => {
const max_port_num = process.env.MAX_PORTS ? parseInt(process.env.MAX_PORTS as string) : 6;
const port_mutex = new Mutex();
const get_and_update_port = async (room_id: string) => {
const max_port_num = process.env.MAX_PORTS ? parseInt(process.env.MAX_PORTS as string) : 10;
const start_port = 8888;
const ports_list = await hasura.get_exposed_ports();
for (let i = 0; i < max_port_num; i++) {
const result = start_port + i;
let flag = false;
for (const port_info of ports_list) {
if (port_info.port === result) {
flag = true;
const release = await port_mutex.acquire();
let port = -1;
try {
const ports_list = await hasura.get_exposed_ports();
const porst_set = new Set(ports_list.map((item: any) => item.port));
for (let i = start_port; i < start_port + max_port_num; i++) {
if (!porst_set.has(i)) {
port = i;
await hasura.update_room_port(room_id, port);
hasura.update_room_created_at(room_id, new Date().toISOString());
break;
}
}
if (!flag) {
return result;
}
} finally {
release();
}
return -1;
return port;
}

const upload_contest_files = async (sub_base_dir: string, queue_front: queue_element) => {
Expand Down Expand Up @@ -159,17 +163,13 @@ const docker_cron = async () => {
// try creating containers, if failed, retry next time.
const new_containers: Docker.Container[] = [];
try {
const port = await get_port();
const port = await get_and_update_port(queue_front.room_id);
if (port === -1) {
console.log("no port available")
docker_queue.push(queue_front);
return;
}

// port 始终需要使用
await hasura.update_room_port(queue_front.room_id, port);
await hasura.update_room_created_at(queue_front.room_id, new Date().toISOString());

console.log("room status updated");

if (queue_front.envoy === 1) {
Expand Down
7 changes: 2 additions & 5 deletions src/helpers/hasura.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,21 +423,18 @@ export const count_room_team: any = async (contest_id: string, team_id: string)

/**
* Get all the exposed ports
* @returns {[{room_id, port}]} [{room_id, port}]
* @returns {[{port}]} [{port}]
*/
export const get_exposed_ports: any = async () => {
const query_exposed_ports = await client.request(
gql`
query get_exposed_ports {
contest_room{
contest_room(where: {port: {_is_null: false}}) {
port
room_id
}
}
`
);
// console.log("hasura result: ");
// console.log(query_exposed_ports)
const result = query_exposed_ports.contest_room
return result;
}
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2380,6 +2380,13 @@ astral-regex@^2.0.0:
resolved "https://registry.npmjs.org/astral-regex/-/astral-regex-2.0.0.tgz"
integrity sha512-Z7tMw1ytTXt5jqMcOP+OQteU1VuNK9Y02uuJtKQ1Sv69jXQKKg5cibLwGJow8yzZP+eAc18EmLGPal0bp36rvQ==

async-mutex@^0.5.0:
version "0.5.0"
resolved "https://registry.npmmirror.com/async-mutex/-/async-mutex-0.5.0.tgz#353c69a0b9e75250971a64ac203b0ebfddd75482"
integrity sha512-1A94B18jkJ3DYq284ohPxoXbfTA5HsQ7/Mf4DEhcyLx3Bz27Rh59iScbB6EPiP+B+joue6YCxcMXSbFC1tZKwA==
dependencies:
tslib "^2.4.0"

asynckit@^0.4.0:
version "0.4.0"
resolved "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz"
Expand Down Expand Up @@ -5821,6 +5828,11 @@ tslib@^2.1.0, tslib@^2.3.1, tslib@^2.5.0:
resolved "https://registry.npmjs.org/tslib/-/tslib-2.5.0.tgz"
integrity sha512-336iVw3rtn2BUK7ORdIAHTyxHGRIHVReokCR3XjbckJMK7ms8FysBfhLR8IXnAgy7T0PTPNBWKiH514FOW/WSg==

tslib@^2.4.0:
version "2.6.2"
resolved "https://registry.npmmirror.com/tslib/-/tslib-2.6.2.tgz#703ac29425e7b37cd6fd456e92404d46d1f3e4ae"
integrity sha512-AEYxH93jGFPn/a2iVAwW87VuUIkR1FVUKB77NwMF7nBTDkDrrT/Hpt/IrCJ0QXhW27jTBDcf5ZY7w6RiqTMw2Q==

tsutils@^3.21.0:
version "3.21.0"
resolved "https://registry.npmjs.org/tsutils/-/tsutils-3.21.0.tgz"
Expand Down

0 comments on commit f37e759

Please sign in to comment.