-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite.ts
146 lines (126 loc) · 3.97 KB
/
write.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { v } from "convex/values";
import { map } from "modern-async";
import { MutationCtx, internalMutation, mutation } from "../_generated/server";
import {
getActiveSessionsCounter,
getNumberOfWaiting,
getWaitlistSession,
newSessionPosition,
newSessionStatus,
validateSessionIsActive,
} from "./read";
// Defaults to 5 minutes.
const ACTIVE_SESSION_TIMEOUT_MS =
+(process.env.ACTIVE_SESSION_TIMEOUT_SECONDS ?? 5 * 60) * 1000;
// Defaults to 1 minute.
const WAITING_SESSION_TIMEOUT_MS =
+(process.env.WAITING_SESSION_TIMEOUT_SECONDS ?? 60) * 1000;
export const join = mutation({
args: {
sessionId: v.string(),
},
handler: async (ctx, { sessionId }) => {
const existingSession = await getWaitlistSession(ctx, sessionId);
if (existingSession !== null) {
if (existingSession.status === "waiting") {
// waiting user came back, reset their lastActive
await ctx.db.patch(existingSession._id, { lastActive: null });
}
return;
}
const status = await newSessionStatus(ctx);
const position = await newSessionPosition(ctx);
await ctx.db.insert("waitlist", {
status,
position,
sessionId,
lastActive: status === "active" ? Date.now() : null,
});
if (status === "active") {
await changeActiveSessionsCounter(ctx, 1);
}
},
});
export const leave = mutation({
args: {
sessionId: v.string(),
},
handler: async (ctx, { sessionId }) => {
const existingSession = await getWaitlistSession(ctx, sessionId);
if (existingSession === null) {
// The sesion was deleted already
return;
}
if (existingSession.status === "active") {
// Ignore, active sessions expire based on user activity
}
await ctx.db.patch(existingSession._id, { lastActive: Date.now() });
},
});
export async function validateSessionAndRefreshLastActive(
ctx: MutationCtx,
sessionId: string
) {
await validateSessionIsActive(ctx, sessionId);
await refreshLastActive(ctx, sessionId);
}
export async function refreshLastActive(ctx: MutationCtx, sessionId: string) {
const existingSession = await getWaitlistSession(ctx, sessionId);
if (existingSession === null) {
throw new Error("Unexpected `refreshLastActive` for invalid sessionId");
}
await ctx.db.patch(existingSession._id, { lastActive: Date.now() });
}
export const updateWaitlist = internalMutation({
args: {},
handler: async (ctx) => {
const numWaiting = await getNumberOfWaiting(ctx);
if (numWaiting <= 0) {
return;
}
await deleteStaleSessions(ctx, "waiting", WAITING_SESSION_TIMEOUT_MS);
const staleActiveCount = await deleteStaleSessions(
ctx,
"active",
ACTIVE_SESSION_TIMEOUT_MS
);
const ready = await ctx.db
.query("waitlist")
.withIndex("byStatus", (q) => q.eq("status", "waiting"))
.take(staleActiveCount);
const now = Date.now();
await map(ready, ({ _id }) =>
ctx.db.patch(_id, { status: "active", lastActive: now })
);
await changeActiveSessionsCounter(ctx, ready.length - staleActiveCount);
},
});
async function deleteStaleSessions(
ctx: MutationCtx,
status: "active" | "waiting",
timeoutMs: number
) {
const now = Date.now();
const lastActiveCutoff = now - timeoutMs;
const stale = await ctx.db
.query("waitlist")
.withIndex("byLastActive", (q) =>
q
.eq("status", status)
.gt("lastActive", null)
.lt("lastActive", lastActiveCutoff)
)
// Handling the extreme edge case that too many people become inactive all at once
.take(1000);
await map(stale, ({ _id }) => ctx.db.delete(_id));
return stale.length;
}
async function changeActiveSessionsCounter(ctx: MutationCtx, change: number) {
const counter = await getActiveSessionsCounter(ctx);
const count = Math.max(0, (counter?.count ?? 0) + change);
if (counter === null) {
await ctx.db.insert("waitlistCounters", { name: "active", count });
} else {
await ctx.db.patch(counter._id, { count });
}
}