Skip to content
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

Add queue for http server kv requests #331

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions http/cloudflare-kv.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { EventEmitter } from 'node:events';

import fetchWithTimeout from '../utils/fetch-with-timeout.mjs';

const completeEmitter = new EventEmitter();

const accountId = '424ad63426a1ae47d559873f929eb9fc';

const productionNamespaceId = '2e6feba88a9e4097b6d2209191ed4ae5';
const devNameSpaceID = '17fd725f04984e408d4a70b37c817171';

const requestLimit = 6;

let pending = [];
const queue = [];

const checkQueue = async () => {
if (pending.length >= requestLimit) {
return;
}
if (queue.length < 1) {
return;
}
const kvName = queue.shift();
pending.push(kvName);

const namespaceId = process.env.ENVIRONMENT === 'production' ? productionNamespaceId : devNameSpaceID;
const url = `https://api.cloudflare.com/client/v4/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${kvName}`;
let response;
try {
response = await fetchWithTimeout(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.CLOUDFLARE_TOKEN}`,
},
timeout: 9000,
});
completeEmitter.emit(kvName, response);
} catch (error) {
//response = new Response(null, {status: 500, statusText: error.message});
queue.unshift(kvName);
} finally {
pending = pending.filter(kv => kv !== kvName);
}
checkQueue();
};

const cloudflareKv = {
get: async (kvName) => {
return new Promise((resolve) => {
completeEmitter.once(kvName, resolve);
if (!pending.includes(kvName) && !queue.includes(kvName)) {
queue.push(kvName);
}
checkQueue();
});
},
};

export default cloudflareKv;
18 changes: 3 additions & 15 deletions http/env-binding.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ import { EventEmitter } from 'node:events';
import { v4 as uuidv4} from 'uuid';

import cacheMachine from '../utils/cache-machine.mjs';

const accountId = '424ad63426a1ae47d559873f929eb9fc';

const productionNamespaceId = '2e6feba88a9e4097b6d2209191ed4ae5';
const devNameSpaceID = '17fd725f04984e408d4a70b37c817171';
import cloudflareKv from './cloudflare-kv.mjs';

const emitter = new EventEmitter();

Expand Down Expand Up @@ -43,15 +39,7 @@ async function messageParentProcess(message) {
}

async function getDataPrimary(kvName, format) {
const namespaceId = process.env.ENVIRONMENT === 'production' ? productionNamespaceId : devNameSpaceID;
const url = `https://api.cloudflare.com/client/v4/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${kvName}`;
const response = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.CLOUDFLARE_TOKEN}`,
},
});
const response = await cloudflareKv.get(kvName);
if (response.status === 404) {
return null;
}
Expand All @@ -68,7 +56,7 @@ async function getDataPrimary(kvName, format) {
}

async function getDataWorker(kvName, format) {
return messageParentProcess({action: 'getKv', kvName});
return messageParentProcess({action: 'getKv', kvName, timeout: 25000});
}

const DATA_CACHE = {
Expand Down
40 changes: 40 additions & 0 deletions http/test-kv.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'dotenv/config';

import DataAPI from '../datasources/index.mjs';
import cloudflareKv from './cloudflare-kv.mjs';

const data = new DataAPI();

const getKv = async (kvName) => {
const response = await cloudflareKv.get(kvName);
if (response.status !== 200) {
console.error('error', kvName, `${response.status} ${response.statusText}`);
return;
}
console.log(response.status, kvName, (await response.text()).length);
};

for (const workerName in data.worker) {
const worker = data.worker[workerName];
for (const gameMode of worker.gameModes) {
let kvName = worker.kvName;
let suffix = '';
if (gameMode !== 'regular') {
suffix = `_${gameMode}`;
}
try {
if (worker.kvs) {
for (const hexKey in worker.kvs) {
const splitWorker = worker.kvs[hexKey];
const fullKvName = `${splitWorker.kvName}${suffix}`;
getKv(fullKvName);
}
} else {
const fullKvName = `${kvName}${suffix}`;
getKv(fullKvName);
}
} catch (error) {
console.error(kvName, gameMode, error);
}
}
}
Loading