-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Most active Places Sort by and hot scenes memory leak #581
Open
cyaiox
wants to merge
1
commit into
master
Choose a base branch
from
fix/out-of-memory
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,58 @@ | ||
import Time from "decentraland-gatsby/dist/utils/date/Time" | ||
import env from "decentraland-gatsby/dist/utils/env" | ||
import fetch, { RequestInit } from "node-fetch" | ||
import deepmerge from "deepmerge" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what do you think about using radash that's in decentraland gatsby? |
||
|
||
import { HotScene } from "../Place/types" | ||
|
||
const DEFAULT_HOST_SCENE = [] as HotScene[] | ||
const DEFAULT_HOST_SCENE = Object.freeze([]) as readonly HotScene[] | ||
|
||
let memory = DEFAULT_HOST_SCENE | ||
// Single memory reference for the most recent hot scenes data | ||
let currentHotScenes: HotScene[] = [...DEFAULT_HOST_SCENE] | ||
|
||
export default class RealmProvider { | ||
static Url = env( | ||
"ARCHIPELAGO_URL", | ||
"https://archipelago-ea-stats.decentraland.org/" | ||
) | ||
static UrlLegacy = env( | ||
"REALM_PROVIDER_URL", | ||
"https://realm-provider.decentraland.org/" | ||
) | ||
static Cache = new Map<string, RealmProvider>() | ||
|
||
private url: string | ||
private urlLegacy: string | ||
|
||
constructor(url: string) { | ||
constructor(url: string, urlLegacy: string) { | ||
this.url = url | ||
this.urlLegacy = urlLegacy | ||
} | ||
|
||
// Singleton instance based on URL | ||
static from(url: string) { | ||
static from(url: string, urlLegacy: string) { | ||
if (!this.Cache.has(url)) { | ||
this.Cache.set(url, new RealmProvider(url)) | ||
this.Cache.set(url, new RealmProvider(url, urlLegacy)) | ||
} | ||
return this.Cache.get(url)! | ||
} | ||
|
||
static get() { | ||
return this.from(this.Url) | ||
return this.from(this.Url, this.UrlLegacy) | ||
} | ||
|
||
async getHotScenes(): Promise<HotScene[]> { | ||
async getHotScenes(isLegeacy = false): Promise<HotScene[]> { | ||
const controller = new AbortController() | ||
|
||
const timeoutId = setTimeout(() => controller.abort(), Time.Second * 10) | ||
|
||
try { | ||
const response = await fetch(`${this.url}hot-scenes`, { | ||
signal: controller.signal as RequestInit["signal"], | ||
}) | ||
const response = await fetch( | ||
`${isLegeacy ? this.urlLegacy : this.url}hot-scenes`, | ||
{ | ||
signal: controller.signal as RequestInit["signal"], | ||
} | ||
) | ||
if (!response.ok) { | ||
throw new Error(`Failed to fetch hot scenes: ${response.statusText}`) | ||
} | ||
|
@@ -52,15 +63,68 @@ export default class RealmProvider { | |
} | ||
} | ||
|
||
// TODO: Remove this once the Legacy Explorer is not longer used | ||
export const processScene = ( | ||
scene: HotScene, | ||
sceneMap: Map<string, HotScene>, | ||
isLegacy: boolean = false | ||
) => { | ||
const key = `${scene.baseCoords[0]},${scene.baseCoords[1]}` | ||
|
||
if (sceneMap.has(key)) { | ||
const existingScene = sceneMap.get(key)! | ||
|
||
// If it's legacy data, we want to keep the existing scene data and only update realms and counts | ||
const baseScene = isLegacy ? existingScene : scene | ||
const updateScene = isLegacy ? scene : existingScene | ||
|
||
// First merge everything except usersTotalCount | ||
const { usersTotalCount: baseCount, ...baseRest } = baseScene | ||
const { usersTotalCount: updateCount, ...updateRest } = updateScene | ||
|
||
const mergedScene = { | ||
...deepmerge(baseRest, updateRest, { | ||
arrayMerge: (_, source) => source, | ||
}), | ||
usersTotalCount: (baseCount || 0) + (updateCount || 0), | ||
} | ||
|
||
sceneMap.set(key, mergedScene) | ||
} else { | ||
sceneMap.set(key, deepmerge(scene, { realms: [] })) | ||
} | ||
} | ||
|
||
export const fetchHotScenesAndUpdateCache = async () => { | ||
let scenesMap: Map<string, HotScene> | undefined | ||
|
||
try { | ||
const response = await RealmProvider.get().getHotScenes() | ||
memory = response | ||
const [hotScenesLegacy, hotScenes] = await Promise.all([ | ||
RealmProvider.get().getHotScenes(true), | ||
RealmProvider.get().getHotScenes(), | ||
]) | ||
|
||
// Create a new Map for each operation | ||
scenesMap = new Map<string, HotScene>() | ||
|
||
// Process legacy scenes first | ||
hotScenesLegacy.forEach((scene) => processScene(scene, scenesMap!, true)) | ||
// Then process new scenes, which will take precedence | ||
hotScenes.forEach((scene) => processScene(scene, scenesMap!, false)) | ||
|
||
// Update the current hot scenes with new data | ||
currentHotScenes = Array.from(scenesMap.values()) | ||
} catch (error) { | ||
memory = DEFAULT_HOST_SCENE | ||
currentHotScenes = [...DEFAULT_HOST_SCENE] | ||
} finally { | ||
// Clean up the temporary Map | ||
if (scenesMap) { | ||
scenesMap.clear() | ||
scenesMap = undefined | ||
} | ||
} | ||
} | ||
|
||
export const getHotScenes = () => { | ||
return memory | ||
export const getHotScenes = (): HotScene[] => { | ||
return [...currentHotScenes] | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure that un the
getPlaceMostActiveList
we should useUPDATED_AT
as defaultThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this change is because doesn't exist column
most_active
in the table, so this will sort in SQL usingupdated_at
, then we are applying the sort bymost_active
to the results