From 7afe165979419f1fd96f6356f32ded35cb143de9 Mon Sep 17 00:00:00 2001 From: mat Date: Sun, 3 Oct 2021 12:08:41 -0500 Subject: attempt to fix memory leak --- src/constants.ts | 6 +++--- src/hypixelCached.ts | 20 ++++++++++---------- src/index.ts | 31 ++++++++++++++++++++----------- 3 files changed, 33 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/constants.ts b/src/constants.ts index 13fc21d..3cc77d2 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -76,7 +76,7 @@ const fileCache = new NodeCache({ */ function fetchFile(path: string): Promise { return new Promise(resolve => { - queue.enqueue(async() => { + queue.enqueue(async () => { if (fileCache.has(path)) return resolve(fileCache.get(path)!) @@ -137,7 +137,7 @@ export let fetchJSONConstant = async function fetchJSONConstant(filename: string } /** Add stats to skyblock-constants. This has caching so it's fine to call many times */ -export let addJSONConstants = async function addJSONConstants(filename: string, addingValues: string[], unit: string='stat'): Promise { +export let addJSONConstants = async function addJSONConstants(filename: string, addingValues: string[], unit: string = 'stat'): Promise { if (addingValues.length === 0) return // no stats provided, just return let file: GithubFile = await fetchFile(filename) @@ -268,7 +268,7 @@ export async function setConstantValues(newValues: constantValues) { const commitMessage = 'Update values' try { await editFile(file, commitMessage, JSON.stringify(updatedStats, null, 2)) - } catch {} + } catch { } } diff --git a/src/hypixelCached.ts b/src/hypixelCached.ts index 07566f9..a80f4d2 100644 --- a/src/hypixelCached.ts +++ b/src/hypixelCached.ts @@ -10,7 +10,7 @@ import * as mojang from './mojang.js' import NodeCache from 'node-cache' import { debug } from './index.js' import LRUCache from 'lru-cache' -import { TLSSocket } from 'tls' +// import { TLSSocket } from 'tls' // cache usernames for 30 minutes /** uuid: username */ @@ -22,9 +22,9 @@ export const usernameCache = new NodeCache({ useClones: false, }) -usernameCache.setMaxListeners(50) -// @ts-ignore for some reason the typings don't have setMaxListeners but it does -TLSSocket.setMaxListeners(50) +// usernameCache.setMaxListeners(50) +// // @ts-ignore for some reason the typings don't have setMaxListeners but it does +// TLSSocket.setMaxListeners(50) export const basicProfilesCache = new NodeCache({ @@ -107,8 +107,8 @@ export async function uuidFromUser(user: string): Promise { } // check if the username is a value - const uuidToUsername: { [ key: string ]: string | Promise } = usernameCache.mget(usernameCache.keys()) - for (const [ uuid, username ] of Object.entries(uuidToUsername)) { + const uuidToUsername: { [key: string]: string | Promise } = usernameCache.mget(usernameCache.keys()) + for (const [uuid, username] of Object.entries(uuidToUsername)) { if (username && (username).toLowerCase && user.toLowerCase() === (username).toLowerCase()) return uuid } @@ -119,7 +119,7 @@ export async function uuidFromUser(user: string): Promise { // set it as waitForCacheSet (a promise) in case uuidFromUser gets called while its fetching mojang usernameCache.set(undashedUser, waitForCacheSet(usernameCache, user, user)) - + // not cached, actually fetch mojang api now let { uuid, username } = await mojang.profileFromUser(user) if (!uuid) { @@ -186,7 +186,7 @@ export async function fetchPlayer(user: string): Promise { // clone in case it gets modified somehow later playerCache.set(playerUuid, cleanPlayer) usernameCache.set(playerUuid, cleanPlayer.username) - + const cleanBasicPlayer = Object.assign({}, cleanPlayer) delete cleanBasicPlayer.profiles basicPlayerCache.set(playerUuid, cleanBasicPlayer) @@ -202,7 +202,7 @@ export async function fetchBasicPlayer(user: string): Promise { }) const startTime = Date.now() -app.get('/', async(req, res) => { +app.get('/', async (req, res) => { const currentTime = Date.now() res.json({ ok: true, @@ -40,11 +41,19 @@ app.get('/', async(req, res) => { finishedCachingRawLeaderboards, leaderboardUpdateMemberQueueSize: leaderboardUpdateMemberQueue.size, leaderboardUpdateProfileQueueSize: leaderboardUpdateProfileQueue.size, + + usernameCacheSize: usernameCache.keys().length, + basicProfilesCacheSize: basicProfilesCache.keys().length, + playerCacheSize: playerCache.keys().length, + basicPlayerCacheSize: basicPlayerCache.keys().length, + profileCacheSize: profileCache.keys().length, + profilesCacheSize: profilesCache.keys().length, + profileNameCacheSize: profileNameCache.keys().length, // key: getKeyUsage() }) }) -app.get('/player/:user', async(req, res) => { +app.get('/player/:user', async (req, res) => { try { const user = await fetchUser( { user: req.params.user }, @@ -61,7 +70,7 @@ app.get('/player/:user', async(req, res) => { } }) -app.get('/discord/:id', async(req, res) => { +app.get('/discord/:id', async (req, res) => { try { res.json( await fetchAccountFromDiscord(req.params.id) @@ -72,7 +81,7 @@ app.get('/discord/:id', async(req, res) => { } }) -app.get('/player/:user/:profile', async(req, res) => { +app.get('/player/:user/:profile', async (req, res) => { try { const profile = await fetchMemberProfile(req.params.user, req.params.profile, req.query.customization as string === 'true') if (profile) @@ -85,7 +94,7 @@ app.get('/player/:user/:profile', async(req, res) => { } }) -app.get('/player/:user/:profile/leaderboards', async(req, res) => { +app.get('/player/:user/:profile/leaderboards', async (req, res) => { try { res.json( await fetchMemberLeaderboardSpots(req.params.user, req.params.profile) @@ -96,7 +105,7 @@ app.get('/player/:user/:profile/leaderboards', async(req, res) => { } }) -app.get('/leaderboard/:name', async(req, res) => { +app.get('/leaderboard/:name', async (req, res) => { try { res.json( await fetchLeaderboard(req.params.name) @@ -107,7 +116,7 @@ app.get('/leaderboard/:name', async(req, res) => { } }) -app.get('/leaderboards', async(req, res) => { +app.get('/leaderboards', async (req, res) => { try { res.json( await fetchAllLeaderboardsCategorized() @@ -118,7 +127,7 @@ app.get('/leaderboards', async(req, res) => { } }) -app.get('/constants', async(req, res) => { +app.get('/constants', async (req, res) => { try { res.json( await constants.fetchConstantValues() @@ -129,7 +138,7 @@ app.get('/constants', async(req, res) => { } }) -app.post('/accounts/createsession', async(req, res) => { +app.post('/accounts/createsession', async (req, res) => { try { const { code } = req.body const codeExchange = await discord.exchangeCode(`${mainSiteUrl}/loggedin`, code) @@ -149,7 +158,7 @@ app.post('/accounts/createsession', async(req, res) => { } }) -app.post('/accounts/session', async(req, res) => { +app.post('/accounts/session', async (req, res) => { try { const { uuid } = req.body const session = await fetchSession(uuid) @@ -164,7 +173,7 @@ app.post('/accounts/session', async(req, res) => { }) -app.post('/accounts/update', async(req, res) => { +app.post('/accounts/update', async (req, res) => { // it checks against the key, so it's kind of secure if (req.headers.key !== process.env.key) return console.log('bad key!') try { -- cgit