Skip to content

Commit 3427be3

Browse files
authored
Merge pull request #22 from thaddeus/kv-request-queue
Kv request queue
2 parents dba36c3 + aa4b361 commit 3427be3

File tree

10 files changed

+245
-146
lines changed

10 files changed

+245
-146
lines changed

.github/workflows/branch-deploy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
runs-on: ubuntu-latest
3232

3333
steps:
34-
- uses: github/branch-deploy@v9.4.0
34+
- uses: github/branch-deploy@v9.7.0
3535
id: branch-deploy
3636
with:
3737
admins: the-hideout/core-contributors

.github/workflows/deploy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ jobs:
6666
steps:
6767
# https://github.com/github/branch-deploy/blob/d3c24bd92505e623615b75ffdfac5ed5259adbdb/docs/merge-commit-strategy.md
6868
- name: deployment check
69-
uses: github/branch-deploy@v9.4.0
69+
uses: github/branch-deploy@v9.7.0
7070
id: deployment-check
7171
with:
7272
merge_deploy_mode: "true"

.github/workflows/unlock-on-merge.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616

1717
steps:
1818
- name: unlock on merge
19-
uses: github/branch-deploy@v9.4.0
19+
uses: github/branch-deploy@v9.7.0
2020
id: unlock-on-merge
2121
with:
2222
unlock_on_merge_mode: "true" # <-- indicates that this is the "Unlock on Merge Mode" workflow

http/cloudflare-kv.mjs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { EventEmitter } from 'node:events';
2+
3+
import fetchWithTimeout from '../utils/fetch-with-timeout.mjs';
4+
5+
const completeEmitter = new EventEmitter();
6+
7+
const accountId = '424ad63426a1ae47d559873f929eb9fc';
8+
9+
const productionNamespaceId = '2e6feba88a9e4097b6d2209191ed4ae5';
10+
const devNameSpaceID = '17fd725f04984e408d4a70b37c817171';
11+
12+
const requestLimit = 6;
13+
14+
let pending = [];
15+
const queue = [];
16+
17+
const checkQueue = async () => {
18+
if (pending.length >= requestLimit) {
19+
return;
20+
}
21+
if (queue.length < 1) {
22+
return;
23+
}
24+
const kvName = queue.shift();
25+
pending.push(kvName);
26+
27+
const namespaceId = process.env.ENVIRONMENT === 'production' ? productionNamespaceId : devNameSpaceID;
28+
const url = `https://api.cloudflare.com/client/v4/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${kvName}`;
29+
let response;
30+
try {
31+
response = await fetchWithTimeout(url, {
32+
method: 'GET',
33+
headers: {
34+
'Content-Type': 'application/json',
35+
Authorization: `Bearer ${process.env.CLOUDFLARE_TOKEN}`,
36+
},
37+
timeout: 9000,
38+
});
39+
completeEmitter.emit(kvName, response);
40+
} catch (error) {
41+
//response = new Response(null, {status: 500, statusText: error.message});
42+
queue.unshift(kvName);
43+
} finally {
44+
pending = pending.filter(kv => kv !== kvName);
45+
}
46+
checkQueue();
47+
};
48+
49+
const cloudflareKv = {
50+
get: async (kvName) => {
51+
return new Promise((resolve) => {
52+
completeEmitter.once(kvName, resolve);
53+
if (!pending.includes(kvName) && !queue.includes(kvName)) {
54+
queue.push(kvName);
55+
}
56+
checkQueue();
57+
});
58+
},
59+
};
60+
61+
export default cloudflareKv;

http/env-binding.mjs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ import { EventEmitter } from 'node:events';
66
import { v4 as uuidv4} from 'uuid';
77

88
import cacheMachine from '../utils/cache-machine.mjs';
9-
10-
const accountId = '424ad63426a1ae47d559873f929eb9fc';
11-
12-
const productionNamespaceId = '2e6feba88a9e4097b6d2209191ed4ae5';
13-
const devNameSpaceID = '17fd725f04984e408d4a70b37c817171';
9+
import cloudflareKv from './cloudflare-kv.mjs';
1410

1511
const emitter = new EventEmitter();
1612

@@ -43,15 +39,7 @@ async function messageParentProcess(message) {
4339
}
4440

4541
async function getDataPrimary(kvName, format) {
46-
const namespaceId = process.env.ENVIRONMENT === 'production' ? productionNamespaceId : devNameSpaceID;
47-
const url = `https://api.cloudflare.com/client/v4/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${kvName}`;
48-
const response = await fetch(url, {
49-
method: 'GET',
50-
headers: {
51-
'Content-Type': 'application/json',
52-
Authorization: `Bearer ${process.env.CLOUDFLARE_TOKEN}`,
53-
},
54-
});
42+
const response = await cloudflareKv.get(kvName);
5543
if (response.status === 404) {
5644
return null;
5745
}
@@ -68,7 +56,7 @@ async function getDataPrimary(kvName, format) {
6856
}
6957

7058
async function getDataWorker(kvName, format) {
71-
return messageParentProcess({action: 'getKv', kvName});
59+
return messageParentProcess({action: 'getKv', kvName, timeout: 25000});
7260
}
7361

7462
const DATA_CACHE = {

http/package-lock.json

Lines changed: 19 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

http/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
"nodemon": "^3.1.3"
1818
},
1919
"dependencies": {
20-
"@graphql-tools/merge": "9.0.4",
20+
"@graphql-tools/merge": "9.0.7",
21+
"@graphql-tools/schema": "10.0.6",
2122
"@sentry/cli": "^2.32.2",
2223
"@sentry/node": "^8.18.0",
2324
"@sentry/profiling-node": "^8.18.0",
24-
"@graphql-tools/schema": "10.0.4",
25-
"graphql-yoga": "^5.6.3",
25+
"graphql-yoga": "^5.7.0",
2626
"dotenv": "^16.4.5",
2727
"uuid": "^10.0.0"
2828
},

http/test-kv.mjs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import 'dotenv/config';
2+
3+
import DataAPI from '../datasources/index.mjs';
4+
import cloudflareKv from './cloudflare-kv.mjs';
5+
6+
const data = new DataAPI();
7+
8+
const getKv = async (kvName) => {
9+
const response = await cloudflareKv.get(kvName);
10+
if (response.status !== 200) {
11+
console.error('error', kvName, `${response.status} ${response.statusText}`);
12+
return;
13+
}
14+
console.log(response.status, kvName, (await response.text()).length);
15+
};
16+
17+
for (const workerName in data.worker) {
18+
const worker = data.worker[workerName];
19+
for (const gameMode of worker.gameModes) {
20+
let kvName = worker.kvName;
21+
let suffix = '';
22+
if (gameMode !== 'regular') {
23+
suffix = `_${gameMode}`;
24+
}
25+
try {
26+
if (worker.kvs) {
27+
for (const hexKey in worker.kvs) {
28+
const splitWorker = worker.kvs[hexKey];
29+
const fullKvName = `${splitWorker.kvName}${suffix}`;
30+
getKv(fullKvName);
31+
}
32+
} else {
33+
const fullKvName = `${kvName}${suffix}`;
34+
getKv(fullKvName);
35+
}
36+
} catch (error) {
37+
console.error(kvName, gameMode, error);
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)