feat: enhance queue management with logging and immediate access granting

This commit is contained in:
2025-09-19 19:07:28 +02:00
parent d5a5fd2d64
commit 527a2586b7

View File

@@ -164,13 +164,20 @@ export async function GET(req) {
// compute counts
const activeCount = ev.sockets.size
console.log(`Event ${eventId}: ${activeCount} users, threshold: ${eventThreshold}`)
// turn on queue if threshold reached
if (activeCount >= eventThreshold) ev.queueOn = true
if (activeCount >= eventThreshold) {
ev.queueOn = true
console.log(`Queue activated for event ${eventId}`)
}
// if queueOn and socket not active, add to queue
if (ev.queueOn && !ev.active.has(socket.id)) {
if (!ev.queue.includes(socket.id)) {
ev.queue.push(socket.id)
console.log(`Added ${socket.id} to queue at position ${ev.queue.length}`)
// Save queue position to database
if (connection) {
@@ -186,7 +193,32 @@ export async function GET(req) {
}
}
// evaluate granting
// If queue is NOT active and user doesn't have access, grant it immediately
if (!ev.queueOn && !ev.active.has(socket.id)) {
console.log(`Granting immediate access to ${socket.id} (under threshold)`)
const expiresAt = new Date(Date.now() + TOKEN_TTL_SECONDS * 1000)
const token = jwt.sign({ sid: socket.id, eventId }, process.env.JWT_SECRET || "dev-secret", {
expiresIn: TOKEN_TTL_SECONDS,
})
ev.active.add(socket.id)
// Save to database
if (connection) {
try {
await connection.execute(
'INSERT INTO active_sessions (event_id, socket_id, jwt_token, expires_at) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE jwt_token = VALUES(jwt_token), expires_at = VALUES(expires_at)',
[eventId, socket.id, token, expiresAt]
)
} catch (error) {
console.error('DB error saving active session:', error)
}
}
io.to(socket.id).emit("granted", { token, expiresAt: expiresAt.toISOString() })
}
// evaluate granting (for queue users)
await evaluateQueue(eventId, io)
// send initial update