Skip to content

Commit

Permalink
Merge pull request #8 from the-hideout/graphql-yoga
Browse files Browse the repository at this point in the history
serve stale kv data while refreshing
  • Loading branch information
Razzmatazzz authored Aug 10, 2024
2 parents 3cd68b6 + 556f6b5 commit d5fae03
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 16 deletions.
61 changes: 46 additions & 15 deletions http/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,52 @@ const workerCount = parseInt(process.env.WORKERS ?? String(os.cpus().length - 1)
if (cluster.isPrimary && workerCount > 0) {
const kvStore = {};
const kvLoading = {};
const kvRefreshTimeout = {};
const msOneMinute = 1000 * 60;
const msHalfHour = msOneMinute * 30;
const env = getEnv();

const getKv = async (kvName, rejectOnError = true) => {
try {
console.log(`getting ${kvName} data`);
clearTimeout(kvRefreshTimeout[kvName]);
const oldExpiration = kvStore[kvName]?.expiration ?? 0;
kvLoading[kvName] = env.DATA_CACHE.get(kvName, 'json');
const data = await kvLoading[kvName];
kvStore[kvName] = data;
delete kvLoading[kvName];
let refreshTime = msHalfHour;
if (data?.expiration && new Date(data.expiration) > new Date()) {
refreshTime = new Date(data.expiration) - new Date();
if (refreshTime < msOneMinute) {
refreshTime = msOneMinute;
}
}
if (data?.expiration === oldExpiration) {
refreshTime = msOneMinute;
}
kvRefreshTimeout[kvName] = setTimeout(() => {
getKv(kvName, false);
}, refreshTime);
return data;
} catch (error) {
delete kvLoading[kvName];
console.error('Error getting KV from cloudflare', error);
if (error.message !== 'Invalid CLOUDFLARE_TOKEN') {
refreshTime = msOneMinute;
if (!kvStore[kvName]) {
refreshTime = 1000;
}
kvRefreshTimeout[kvName] = setTimeout(() => {
getKv(kvName, false);
}, refreshTime);
}
if (rejectOnError) {
return Promise.reject(error);
}
}
};

console.log(`Starting ${workerCount} workers`);
for (let i = 0; i < workerCount; i++) {
cluster.fork();
Expand All @@ -41,21 +86,7 @@ if (cluster.isPrimary && workerCount > 0) {
} else if (kvLoading[message.kvName]) {
response.data = JSON.stringify(await kvLoading[message.kvName]);
} else {
kvLoading[message.kvName] = env.DATA_CACHE.get(message.kvName, 'json');
const data = await kvLoading[message.kvName];
kvStore[message.kvName] = data;
delete kvLoading[message.kvName];
let refreshTime = 1000 * 60 * 30;
if (data?.expiration && new Date(data.expiration) > new Date()) {
refreshTime = new Date(data.expiration) - new Date();
if (refreshTime < 1000 * 60) {
refreshTime = 60000;
}
}
response.data = JSON.stringify(data);
setTimeout(() => {
delete kvStore[message.kvName];
}, refreshTime);
response.data = JSON.stringify(await getKv(message.kvName));
}
} catch (error) {
response.error = error.message;
Expand Down
1 change: 1 addition & 0 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ async function graphqlHandler(request, env, ctx) {
}),
headers: {
'Content-Type': 'application/json',
'cache-check-complete': 'true',
},
});
if (queryResult.status !== 200) {
Expand Down
2 changes: 1 addition & 1 deletion plugins/plugin-use-cache-machine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function useCacheMachine(env) {
console.log(`Skipping cache check due to SKIP_CACHE`);
return;
}
if (env.CLOUDFLARE_TOKEN) {
if (request.headers.has('cache-check-complete')) {
console.log(`Skipping cache check already performed by worker`);
return;
}
Expand Down

0 comments on commit d5fae03

Please sign in to comment.