Skip to content

Commit

Permalink
self clearing gsihandler caches when user is inactive
Browse files Browse the repository at this point in the history
  • Loading branch information
Geczy committed Oct 6, 2023
1 parent b747fc6 commit f86cdc3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
19 changes: 19 additions & 0 deletions packages/dota/src/dota/GSIServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { newData, processChanges } from './globalEventEmitter.js'
import { emitMinimapBlockerStatus } from './GSIHandler.js'
import { gsiHandlers } from './lib/consts.js'
import { validateToken } from './validateToken.js'
import { checkForInactiveTokens, tokenLastPostTimestamps } from './clearCacheForUser.js'

function handleSocketAuth(socket: Socket, next: (err?: Error) => void) {
const { token } = socket.handshake.auth
Expand Down Expand Up @@ -63,6 +64,21 @@ class GSIServer {
app.use(express.urlencoded({ extended: true, limit: '1mb' }))

app.post('/', validateToken, processChanges('previously'), processChanges('added'), newData)
app.post(
'/',
validateToken,
(req: Request, res: Response, next: () => void) => {
const token = req.body.auth.token as string

// Update the timestamp for this token
tokenLastPostTimestamps.set(token, Date.now())

next()
},
processChanges('previously'),
processChanges('added'),
newData,
)

app.get('/', (req: Request, res: Response) => {
res.status(200).json({ status: 'ok' })
Expand All @@ -74,6 +90,9 @@ class GSIServer {

this.io.use(handleSocketAuth)
this.io.on('connection', handleSocketConnection)

// Set up the repeating timer
setInterval(checkForInactiveTokens, 60 * 1000) // Run every minute
}

init() {
Expand Down
19 changes: 18 additions & 1 deletion packages/dota/src/dota/clearCacheForUser.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
import { getAuthProvider } from '../twitch/lib/getAuthProvider.js'
import { SocketClient } from '../types.js'
import { logger } from '../utils/logger.js'
import { deleteRedisData } from './GSIHandler.js'
import { gsiHandlers, twitchIdToToken } from './lib/consts.js'

// three types of in-memory cache exists
// This will hold the last POST request timestamp for each token
export const tokenLastPostTimestamps: Map<string, number> = new Map()

// Function to check for inactive tokens and delete the corresponding gsiHandler
export async function checkForInactiveTokens() {
const now = Date.now()
const timeoutMillis = 60 * 1000 // 1 minute

for (const [token, timestamp] of tokenLastPostTimestamps.entries()) {
if (now - timestamp > timeoutMillis) {
await clearCacheForUser(gsiHandlers.get(token)?.client)
}
}
}

// three types of in-memory cache exists
export async function clearCacheForUser(client?: SocketClient | null) {
if (!client) return false

Expand All @@ -24,5 +39,7 @@ export async function clearCacheForUser(client?: SocketClient | null) {
await deleteRedisData(client)

gsiHandlers.delete(client.token)

tokenLastPostTimestamps.delete(client.token)
return true
}

0 comments on commit f86cdc3

Please sign in to comment.