-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #331 from the-hideout/http-kv-queue
Add queue for http server kv requests
- Loading branch information
Showing
3 changed files
with
104 additions
and
15 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -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; |
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 |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |