From 610fb60d49991af7b263e9165ef0465dab9ffd96 Mon Sep 17 00:00:00 2001 From: dangered wolf Date: Tue, 22 Aug 2023 01:45:28 -0400 Subject: [PATCH] Performance tuning: Don't fetch guest token cache with elongator --- src/fetch.ts | 30 ++++++++++++++++++++++-------- src/server.ts | 10 ++++++---- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/fetch.ts b/src/fetch.ts index 12ab957..73b2ee6 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -8,7 +8,13 @@ let wasElongatorDisabled = false; declare const globalThis: { fetchCompletedTime: number; -} +}; + +const generateSnowflake = () => { + const epoch = 1288834974657n; /* Twitter snowflake epoch */ + const timestamp = BigInt(Date.now()) - epoch; + return String((timestamp << 22n) | 696969n); +}; globalThis.fetchCompletedTime = 0; @@ -76,8 +82,12 @@ export const twitterFetch = async ( let activate: Response | null = null; - if (!newTokenGenerated) { + if (!newTokenGenerated && !useElongator) { + const timeBefore = performance.now(); const cachedResponse = await cache.match(guestTokenRequestCacheDummy.clone()); + const timeAfter = performance.now(); + + console.log(`Searched cache for token, took ${timeAfter - timeBefore}ms`); if (cachedResponse) { console.log('Token cache hit'); @@ -88,7 +98,7 @@ export const twitterFetch = async ( } } - if (newTokenGenerated || activate === null) { + if (newTokenGenerated || (activate === null && !useElongator)) { /* We have a guest token that we can use to call API AFAIK there is no limit to how many guest tokens you can request. @@ -110,10 +120,10 @@ export const twitterFetch = async ( continue; } - const guestToken = activateJson.guest_token; + /* Elongator doesn't need guestToken, so we just make up a snowflake */ + const guestToken = activateJson?.guest_token || generateSnowflake(); console.log(newTokenGenerated ? 'Activated guest:' : 'Using guest:', activateJson); - console.log('Guest token:', guestToken); /* Just some cookies to mimick what the Twitter Web App would send */ headers['Cookie'] = [ @@ -139,7 +149,9 @@ export const twitterFetch = async ( headers: headers }); const performanceEnd = performance.now(); - console.log(`Elongator request successful after ${performanceEnd - performanceStart}ms`); + console.log( + `Elongator request successful after ${performanceEnd - performanceStart}ms` + ); } else { const performanceStart = performance.now(); apiRequest = await fetch(url, { @@ -147,7 +159,9 @@ export const twitterFetch = async ( headers: headers }); const performanceEnd = performance.now(); - console.log(`Guest API request successful after ${performanceEnd - performanceStart}ms`); + console.log( + `Guest API request successful after ${performanceEnd - performanceStart}ms` + ); } response = await apiRequest?.json(); @@ -208,7 +222,7 @@ export const twitterFetch = async ( continue; } /* If we've generated a new token, we'll cache it */ - if (event && newTokenGenerated) { + if (event && newTokenGenerated && activate) { const cachingResponse = new Response(await activate.clone().text(), { headers: { ...tokenHeaders, diff --git a/src/server.ts b/src/server.ts index 2975f63..bf70d00 100644 --- a/src/server.ts +++ b/src/server.ts @@ -12,7 +12,7 @@ import { handleProfile } from './user'; declare const globalThis: { fetchCompletedTime: number; -} +}; const router = Router(); @@ -29,8 +29,8 @@ const statusRequest = async ( /* Let's return our HTML version for wayback machine (we can add other archivers too in future) */ if ( - ['archive.org', 'Wayback Machine'].some(service => - request.headers.get('Via')?.includes?.(service) + ['archive.org', 'Wayback Machine'].some( + service => request.headers.get('Via')?.includes?.(service) ) ) { console.log('Request from archive.org'); @@ -450,7 +450,9 @@ export const cacheWrapper = async ( const endTime = performance.now(); const timeSinceFetch = endTime - (globalThis.fetchCompletedTime || 0); const timeSinceStart = endTime - startTime; - console.log(`Request took ${timeSinceStart}ms, of which ${timeSinceFetch}ms was CPU time after last fetch`); + console.log( + `Request took ${timeSinceStart}ms, of which ${timeSinceFetch}ms was CPU time after last fetch` + ); return response; /* Telegram sends this from Webpage Bot, and Cloudflare sends it if we purge cache, and we respect it.