diff options
author | mat <github@matdoes.dev> | 2021-10-03 12:08:41 -0500 |
---|---|---|
committer | mat <github@matdoes.dev> | 2021-10-03 12:08:41 -0500 |
commit | 7afe165979419f1fd96f6356f32ded35cb143de9 (patch) | |
tree | 9fd58fb5721804c1f470e7ae0ac13e7eb37b3d27 /src | |
parent | 1291f0f32b6f626666b6fb86b8d87e3c4293cb7c (diff) | |
download | skyblock-api-7afe165979419f1fd96f6356f32ded35cb143de9.tar.gz skyblock-api-7afe165979419f1fd96f6356f32ded35cb143de9.tar.bz2 skyblock-api-7afe165979419f1fd96f6356f32ded35cb143de9.zip |
attempt to fix memory leak
Diffstat (limited to 'src')
-rw-r--r-- | src/constants.ts | 6 | ||||
-rw-r--r-- | src/hypixelCached.ts | 20 | ||||
-rw-r--r-- | src/index.ts | 31 |
3 files changed, 33 insertions, 24 deletions
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<GithubFile> { 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<void> { +export let addJSONConstants = async function addJSONConstants(filename: string, addingValues: string[], unit: string = 'stat'): Promise<void> { 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<string | undefined> { } // check if the username is a value - const uuidToUsername: { [ key: string ]: string | Promise<KeyValue> } = usernameCache.mget(usernameCache.keys()) - for (const [ uuid, username ] of Object.entries(uuidToUsername)) { + const uuidToUsername: { [key: string]: string | Promise<KeyValue> } = usernameCache.mget(usernameCache.keys()) + for (const [uuid, username] of Object.entries(uuidToUsername)) { if (username && (<string>username).toLowerCase && user.toLowerCase() === (<string>username).toLowerCase()) return uuid } @@ -119,7 +119,7 @@ export async function uuidFromUser(user: string): Promise<string | undefined> { // 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<CleanPlayer | null> { // 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<CleanPlayer | null if (basicPlayerCache.has(playerUuid)) return basicPlayerCache.get(playerUuid)! - + const player = await fetchPlayer(playerUuid) if (!player) { console.debug('no player? this should never happen, perhaps the uuid is invalid or the player hasn\'t played hypixel', playerUuid) @@ -390,7 +390,7 @@ export async function fetchProfileName(user: string, profile: string): Promise<s if (debug) console.debug('Cache miss: fetchProfileName', user, profile) const basicProfiles = await fetchBasicProfiles(playerUuid) - + if (!basicProfiles) return null let profileName: string | null = null diff --git a/src/index.ts b/src/index.ts index 1327771..d0e4fd8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,6 +5,7 @@ import * as constants from './constants.js' import * as discord from './discord.js' import express from 'express' import { getKeyUsage } from './hypixelApi.js' +import { basicPlayerCache, basicProfilesCache, playerCache, profileCache, profileNameCache, profilesCache, usernameCache } from './hypixelCached.js' const app = express() @@ -32,7 +33,7 @@ app.use((req, res, next) => { }) 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 { |