Skip to content

Commit efab777

Browse files
committed
fix lastPing errors
1 parent fd759c4 commit efab777

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/hooks.server.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,26 @@ export const handle: Handle = async ({ event, resolve }) => {
1515

1616
if(session != null && user != null){
1717
try{
18+
const now = new Date();
1819
await prisma.session.update({
1920
where:{
2021
id: session.id
2122
},
2223
data: {
23-
last_use: new Date(Date.now()),
24+
last_use: now,
2425
ip: event.getClientAddress()
2526
}
2627
});
27-
await prisma.user.update({
28+
// Avoid hammering the user row on every request and reduce update conflicts.
29+
const minPingAgeMs = 60_000;
30+
const cutoff = new Date(now.getTime() - minPingAgeMs);
31+
await prisma.user.updateMany({
2832
where: {
29-
username: user.username
33+
username: user.username,
34+
lastPing: { lt: cutoff }
3035
},
3136
data: {
32-
lastPing: new Date(Date.now()),
37+
lastPing: now,
3338
}
3439
});
3540
}catch(error_message){

0 commit comments

Comments
 (0)