Skip to content

Commit 9150e55

Browse files
authored
fix: add distributed lock to dedicated server assignment (#139)
Co-authored-by: Flegma <Flegma@users.noreply.github.com>
1 parent c4ee3d7 commit 9150e55

1 file changed

Lines changed: 87 additions & 67 deletions

File tree

src/matches/match-assistant/match-assistant.service.ts

Lines changed: 87 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,21 @@ export class MatchAssistantService {
232232
});
233233

234234
if (match.options.prefer_dedicated_server) {
235-
const assignedDedicated = await this.assignDedicatedServer(
236-
match.id,
237-
match.region,
238-
);
239-
240-
if (assignedDedicated) {
241-
await this.startMatch(matchId);
242-
return;
235+
try {
236+
const assignedDedicated = await this.assignDedicatedServer(
237+
match.id,
238+
match.region,
239+
);
240+
241+
if (assignedDedicated) {
242+
await this.startMatch(matchId);
243+
return;
244+
}
245+
} catch (error) {
246+
this.logger.error(
247+
`[${matchId}] unable to assign dedicated server`,
248+
error,
249+
);
243250
}
244251
}
245252

@@ -272,9 +279,16 @@ export class MatchAssistantService {
272279
return;
273280
}
274281

275-
if (await this.assignDedicatedServer(match.id, match.region)) {
276-
await this.startMatch(matchId);
277-
return;
282+
try {
283+
if (await this.assignDedicatedServer(match.id, match.region)) {
284+
await this.startMatch(matchId);
285+
return;
286+
}
287+
} catch (error) {
288+
this.logger.error(
289+
`[${matchId}] unable to assign dedicated server`,
290+
error,
291+
);
278292
}
279293

280294
this.logger.log(
@@ -322,72 +336,78 @@ export class MatchAssistantService {
322336
matchId: string,
323337
region: string,
324338
): Promise<boolean> {
325-
const { servers } = await this.hasura.query({
326-
servers: {
327-
__args: {
328-
limit: 1,
329-
where: {
330-
connected: {
331-
_eq: true,
332-
},
333-
enabled: {
334-
_eq: true,
335-
},
336-
is_dedicated: {
337-
_eq: true,
338-
},
339-
type: {
340-
_eq: "Ranked",
341-
},
342-
reserved_by_match_id: {
343-
_is_null: true,
344-
},
345-
region: {
346-
_eq: region,
339+
return this.cache.lock(
340+
`assign-dedicated-server:${region}`,
341+
async () => {
342+
const { servers } = await this.hasura.query({
343+
servers: {
344+
__args: {
345+
limit: 1,
346+
where: {
347+
connected: {
348+
_eq: true,
349+
},
350+
enabled: {
351+
_eq: true,
352+
},
353+
is_dedicated: {
354+
_eq: true,
355+
},
356+
type: {
357+
_eq: "Ranked",
358+
},
359+
reserved_by_match_id: {
360+
_is_null: true,
361+
},
362+
region: {
363+
_eq: region,
364+
},
365+
},
347366
},
367+
id: true,
348368
},
349-
},
350-
id: true,
351-
},
352-
});
369+
});
353370

354-
const server = servers.at(0);
371+
const server = servers.at(0);
355372

356-
if (!server) {
357-
return false;
358-
}
373+
if (!server) {
374+
return false;
375+
}
359376

360-
this.logger.log(`[${matchId}] assigning on dedicated server`);
377+
this.logger.log(`[${matchId}] assigning on dedicated server`);
361378

362-
await this.hasura.mutation({
363-
update_matches_by_pk: {
364-
__args: {
365-
pk_columns: {
366-
id: matchId,
367-
},
368-
_set: {
369-
server_id: server.id,
379+
await this.hasura.mutation({
380+
update_matches_by_pk: {
381+
__args: {
382+
pk_columns: {
383+
id: matchId,
384+
},
385+
_set: {
386+
server_id: server.id,
387+
},
388+
},
389+
__typename: true,
370390
},
371-
},
372-
__typename: true,
373-
},
374-
});
391+
});
375392

376-
await this.hasura.mutation({
377-
update_servers_by_pk: {
378-
__args: {
379-
pk_columns: {
380-
id: server.id,
381-
},
382-
_set: {
383-
reserved_by_match_id: matchId,
393+
await this.hasura.mutation({
394+
update_servers_by_pk: {
395+
__args: {
396+
pk_columns: {
397+
id: server.id,
398+
},
399+
_set: {
400+
reserved_by_match_id: matchId,
401+
},
402+
},
403+
__typename: true,
384404
},
385-
},
386-
__typename: true,
387-
},
388-
});
405+
});
389406

390-
return true;
407+
return true;
408+
},
409+
10,
410+
);
391411
}
392412

393413
private async assignOnDemandServer(matchId: string): Promise<boolean> {

0 commit comments

Comments
 (0)