From fda3ccc6198bf1ecb48e1b095ab1c82f40f714f1 Mon Sep 17 00:00:00 2001 From: dangered wolf Date: Thu, 19 Oct 2023 01:40:59 -0400 Subject: [PATCH] Assign mosaic load balancing based on id --- src/helpers/mosaic.ts | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/helpers/mosaic.ts b/src/helpers/mosaic.ts index 44e5c60..e686269 100644 --- a/src/helpers/mosaic.ts +++ b/src/helpers/mosaic.ts @@ -1,16 +1,26 @@ import { Constants } from '../constants'; +const getDomain = (twitterId: string): string | null => { + const mosaicDomains = Constants.MOSAIC_DOMAIN_LIST; + + if (mosaicDomains.length === 0) { + return null; + } + + let hash = 0; + for (let i = 0; i < twitterId.length; i++) { + const char = twitterId.charCodeAt(i); + hash = (hash << 5) - hash + char; + } + return mosaicDomains[Math.abs(hash) % mosaicDomains.length]; +} + /* Handler for mosaic (multi-image combiner) */ export const handleMosaic = async ( mediaList: APIPhoto[], id: string ): Promise => { - const mosaicDomains = Constants.MOSAIC_DOMAIN_LIST; - let selectedDomain: string | null = null; - while (selectedDomain === null && mosaicDomains.length > 0) { - const domain = mosaicDomains[Math.floor(Math.random() * mosaicDomains.length)]; - selectedDomain = domain; - } + const selectedDomain: string | null = getDomain(id); /* Fallback if there are no Mosaic servers */ if (selectedDomain === null) {