Skip to content

Commit 3710fec

Browse files
committed
Never batch prisma operations to save on connection count
1 parent bf55c41 commit 3710fec

File tree

6 files changed

+296
-285
lines changed

6 files changed

+296
-285
lines changed

apps/worker/src/rankMethods/rankPlayersElo.ts

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,15 @@ function computeEloDeltas(
153153
export const rankPlayersElo = async (snapshotId: number) => {
154154
const { snapshot, snapshotBefore } = await getSnapshots(snapshotId);
155155

156-
const playerInfoMaps = await Promise.all(
157-
snapshot.clients.map((client) => prisma.playerInfoMap.upsert({
156+
const playerInfoMaps = [];
157+
158+
for (const client of snapshot.clients) {
159+
const playerInfoMap = await prisma.playerInfoMap.upsert({
158160
where: {
159161
playerName_mapId: {
160162
playerName: client.playerName,
161163
mapId: snapshot.mapId,
162-
},
164+
}
163165
},
164166
select: {
165167
id: true,
@@ -172,13 +174,16 @@ export const rankPlayersElo = async (snapshotId: number) => {
172174
mapId: snapshot.mapId,
173175
rating: 0,
174176
},
175-
})),
176-
);
177+
});
178+
playerInfoMaps.push(playerInfoMap);
179+
}
177180

178181
const mapRatings = new Map(playerInfoMaps.map(({ playerName, rating, id }) => [playerName, { rating: rating ?? 0, id }]));
179182

180-
const playerInfoGameTypes = await Promise.all(
181-
snapshot.clients.map((client) => prisma.playerInfoGameType.upsert({
183+
const playerInfoGameTypes = [];
184+
185+
for (const client of snapshot.clients) {
186+
const playerInfoGameType = await prisma.playerInfoGameType.upsert({
182187
where: {
183188
playerName_gameTypeName: {
184189
playerName: client.playerName,
@@ -196,8 +201,9 @@ export const rankPlayersElo = async (snapshotId: number) => {
196201
gameTypeName: snapshot.map.gameTypeName,
197202
rating: 0,
198203
},
199-
})),
200-
);
204+
});
205+
playerInfoGameTypes.push(playerInfoGameType);
206+
}
201207

202208
const gameTypeRatings = new Map(playerInfoGameTypes.map(({ playerName, rating, id }) => [playerName, { rating: rating ?? 0, id }]));
203209

@@ -210,18 +216,20 @@ export const rankPlayersElo = async (snapshotId: number) => {
210216
);
211217

212218
if (eloDeltasMap.size > 0) {
213-
await Promise.all(Array.from(eloDeltasMap.entries()).filter(([, eloDelta]) => eloDelta !== 0).map(([playerName, eloDelta]) =>
214-
prisma.playerInfoMap.update({
215-
where: {
216-
id: mapRatings.get(playerName)?.id ?? 0,
217-
},
218-
data: {
219-
rating: {
220-
increment: eloDelta,
219+
for (const [playerName, eloDelta] of Array.from(eloDeltasMap.entries())) {
220+
if (eloDelta !== 0) {
221+
await prisma.playerInfoMap.update({
222+
where: {
223+
id: mapRatings.get(playerName)?.id ?? 0,
221224
},
222-
},
223-
})
224-
));
225+
data: {
226+
rating: {
227+
increment: eloDelta,
228+
},
229+
},
230+
});
231+
}
232+
}
225233
}
226234

227235
const eloDeltasGameType = computeEloDeltas(
@@ -230,18 +238,20 @@ export const rankPlayersElo = async (snapshotId: number) => {
230238
);
231239

232240
if (eloDeltasGameType.size > 0) {
233-
await Promise.all(Array.from(eloDeltasGameType.entries()).filter(([, eloDelta]) => eloDelta !== 0).map(([playerName, eloDelta]) =>
234-
prisma.playerInfoGameType.update({
235-
where: {
236-
id: gameTypeRatings.get(playerName)?.id ?? 0,
237-
},
238-
data: {
239-
rating: {
240-
increment: eloDelta,
241+
for (const [playerName, eloDelta] of Array.from(eloDeltasGameType.entries())) {
242+
if (eloDelta !== 0) {
243+
await prisma.playerInfoGameType.update({
244+
where: {
245+
id: gameTypeRatings.get(playerName)?.id ?? 0,
241246
},
242-
},
243-
})
244-
));
247+
data: {
248+
rating: {
249+
increment: eloDelta,
250+
},
251+
},
252+
});
253+
}
254+
}
245255
}
246256
}
247257
}

apps/worker/src/rankMethods/rankPlayersTime.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ export const rankPlayersTime = async (snapshotId: number) => {
1717
},
1818
});
1919

20-
const playerInfoMaps = await Promise.all(
21-
snapshot.clients.map((client) => prisma.playerInfoMap.upsert({
20+
const playerInfoMaps = [];
21+
22+
for (const client of snapshot.clients) {
23+
const playerInfoMap = await prisma.playerInfoMap.upsert({
2224
where: {
2325
playerName_mapId: {
2426
playerName: client.playerName,
2527
mapId: snapshot.mapId,
26-
},
28+
}
2729
},
2830
select: {
2931
id: true,
@@ -35,8 +37,9 @@ export const rankPlayersTime = async (snapshotId: number) => {
3537
playerName: client.playerName,
3638
mapId: snapshot.mapId,
3739
},
38-
})),
39-
);
40+
});
41+
playerInfoMaps.push(playerInfoMap);
42+
}
4043

4144
const playerTimes = new Map(playerInfoMaps.map(({ playerName, rating }) => [playerName, rating]));
4245
const newPlayerTimes = new Map<string, number>();
@@ -54,8 +57,8 @@ export const rankPlayersTime = async (snapshotId: number) => {
5457
}
5558
}
5659

57-
await Promise.all(Array.from(newPlayerTimes.entries()).map(([playerName, newTime]) =>
58-
prisma.playerInfoMap.update({
60+
for (const [playerName, newTime] of newPlayerTimes.entries()) {
61+
await prisma.playerInfoMap.update({
5962
where: {
6063
playerName_mapId: {
6164
playerName,
@@ -65,6 +68,6 @@ export const rankPlayersTime = async (snapshotId: number) => {
6568
data: {
6669
rating: newTime,
6770
},
68-
})
69-
));
71+
});
72+
}
7073
}

apps/worker/src/workers/pollGameServer.ts

Lines changed: 74 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ async function changePlayerClans(
7070
// clan, make sure old player clan is accurate.
7171

7272
for (let i = 0; i < 10; i++) {
73-
await Promise.all(Object.entries(playerClans).map(async ([playerName, newClanName_]) => {
73+
for (const [playerName, newClanName_] of Object.entries(playerClans)) {
7474
const newClanName = newClanName_ || null;
7575

7676
const currentPlayer = await prisma.player.findUnique({
@@ -101,7 +101,7 @@ async function changePlayerClans(
101101

102102
delete playerClans[playerName];
103103
}
104-
}));
104+
}
105105

106106
const remainingPlayerCount = Object.keys(playerClans).length;
107107
if (remainingPlayerCount > 0) {
@@ -116,14 +116,14 @@ async function changePlayerClans(
116116
console.error(`${remainingPlayerCount}/${totalPlayerCount} players failed to update`);
117117
}
118118

119-
await Promise.all(Object.entries(clanDelta).map(async ([clanName, delta]) => {
119+
for (const [clanName, delta] of Object.entries(clanDelta)) {
120120
if (delta !== 0) {
121121
await prisma.clan.update({
122122
where: { name: clanName },
123123
data: { activePlayerCount: { increment: delta } },
124124
});
125125
}
126-
}));
126+
}
127127
}
128128

129129
export async function processGameServerInfo(
@@ -180,73 +180,81 @@ export async function processGameServerInfo(
180180
onUpdatePlayerClanHook
181181
);
182182

183-
await Promise.all(uniqClients.map((client) => prisma.playerInfoGameType.upsert({
184-
select: {
185-
id: true,
186-
},
187-
where: {
188-
playerName_gameTypeName: {
183+
for (const client of uniqClients) {
184+
await prisma.playerInfoGameType.upsert({
185+
select: {
186+
id: true,
187+
},
188+
where: {
189+
playerName_gameTypeName: {
190+
playerName: client.name,
191+
gameTypeName: gameServerInfo.gameType,
192+
},
193+
},
194+
update: {},
195+
create: {
189196
playerName: client.name,
190197
gameTypeName: gameServerInfo.gameType,
191198
},
192-
},
193-
update: {},
194-
create: {
195-
playerName: client.name,
196-
gameTypeName: gameServerInfo.gameType,
197-
},
198-
})));
199+
});
200+
}
199201

200-
await Promise.all(uniqClans.map((clan) => prisma.clanInfoGameType.upsert({
201-
select: {
202-
id: true,
203-
},
204-
where: {
205-
clanName_gameTypeName: {
202+
for (const clan of uniqClans) {
203+
await prisma.clanInfoGameType.upsert({
204+
select: {
205+
id: true,
206+
},
207+
where: {
208+
clanName_gameTypeName: {
209+
clanName: clan,
210+
gameTypeName: gameServerInfo.gameType,
211+
},
212+
},
213+
update: {},
214+
create: {
206215
clanName: clan,
207216
gameTypeName: gameServerInfo.gameType,
208217
},
209-
},
210-
update: {},
211-
create: {
212-
clanName: clan,
213-
gameTypeName: gameServerInfo.gameType,
214-
},
215-
})));
218+
});
219+
}
216220

217-
await Promise.all(uniqClients.map((client) => prisma.playerInfoMap.upsert({
218-
select: {
219-
id: true,
220-
},
221-
where: {
222-
playerName_mapId: {
221+
for (const client of uniqClients) {
222+
await prisma.playerInfoMap.upsert({
223+
select: {
224+
id: true,
225+
},
226+
where: {
227+
playerName_mapId: {
228+
playerName: client.name,
229+
mapId: map.id,
230+
},
231+
},
232+
update: {},
233+
create: {
223234
playerName: client.name,
224235
mapId: map.id,
225236
},
226-
},
227-
update: {},
228-
create: {
229-
playerName: client.name,
230-
mapId: map.id,
231-
},
232-
})));
237+
});
238+
}
233239

234-
await Promise.all(uniqClans.map((clan) => prisma.clanInfoMap.upsert({
235-
select: {
236-
id: true,
237-
},
238-
where: {
239-
clanName_mapId: {
240+
for (const clan of uniqClans) {
241+
await prisma.clanInfoMap.upsert({
242+
select: {
243+
id: true,
244+
},
245+
where: {
246+
clanName_mapId: {
247+
clanName: clan,
248+
mapId: map.id,
249+
},
250+
},
251+
update: {},
252+
create: {
240253
clanName: clan,
241254
mapId: map.id,
242255
},
243-
},
244-
update: {},
245-
create: {
246-
clanName: clan,
247-
mapId: map.id,
248-
},
249-
})));
256+
});
257+
}
250258

251259
const snapshot = await prisma.gameServerSnapshot.create({
252260
select: {
@@ -339,18 +347,16 @@ export async function processGameServerInfo(
339347
});
340348
});
341349

342-
await Promise.all(
343-
snapshot.clients.map((client) =>
344-
prisma.player.update({
345-
where: {
346-
name: client.playerName,
347-
},
348-
data: {
349-
lastSeenAt: new Date(),
350-
},
351-
})
352-
)
353-
);
350+
for (const client of snapshot.clients) {
351+
await prisma.player.update({
352+
where: {
353+
name: client.playerName,
354+
},
355+
data: {
356+
lastSeenAt: new Date(),
357+
},
358+
});
359+
}
354360

355361
await prisma.gameServer.update({
356362
where: {

0 commit comments

Comments
 (0)