Skip to content

Commit 94f93e4

Browse files
authored
fix(handleSync): substituir array de funções por Promises diretas com… (#69)
… Promise.all Problema 1: `records` armazenava funções assíncronas em vez de Promises. `Promise.allSettled(records)` nunca as executava — apenas resolvia com o valor da função. Agora as Promises são iniciadas diretamente (inPromise e outPromise) e aguardadas com `await Promise.all([inPromise, outPromise])`. Problema 2: `env.CFGATEWAY.put` e `env.MQCFGATEWAY.send` no record OUT estavam sem `await`, tornando-os fire-and-forget. Agora ambos estão dentro de `Promise.all([...])`, garantindo execução completa antes do Worker encerrar. Bônus: inPromise inicia antes do fetch, rodando em paralelo e reduzindo a latência total do path `/sync/`. Closes #66
2 parents e57b96a + 72cd976 commit 94f93e4

1 file changed

Lines changed: 26 additions & 27 deletions

File tree

src/front/.server/mainroute/mainroute.ts

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,25 @@ export async function queueMessage(content: string, url: string, env: Env, lab =
4141
async function handleSync(request: Request, content: string, routeRow: PathRouteRow, env: Env, ctx: ExecutionContext, lab = false, fullpath: string | null = null) {
4242
const route = toPathRoute(routeRow);
4343
const asyncConfig = toPathRouteAsync(route);
44-
const records = [];
45-
46-
// 1. IN - Prepare recording in background
44+
45+
// 1. IN - Start recording immediately (runs concurrently with fetch below)
4746
const inTime = new Date();
4847
const inId = await randomHEX();
49-
50-
records.push(async (_a: unknown) => {
51-
const inFilename = mqfilename(inTime, inId);
52-
await env.CFGATEWAY.put(inFilename, content);
53-
await env.MQCFGATEWAY.send({
48+
const inFilename = mqfilename(inTime, inId);
49+
50+
const inPromise = Promise.all([
51+
env.CFGATEWAY.put(inFilename, content),
52+
env.MQCFGATEWAY.send({
5453
id: inId,
5554
url: request.url,
5655
filename: inFilename,
5756
type: 'store',
5857
time: inTime.getTime(),
5958
lab
60-
} as MQCFGATEWAYMessage, { contentType: 'json' });
61-
});
62-
63-
// 2. DESTINY - Perform fetch
59+
} as MQCFGATEWAYMessage, { contentType: 'json' })
60+
]);
61+
62+
// 2. DESTINY - Perform fetch (runs in parallel with inPromise)
6463
const headers = new Headers();
6564
const headersRecord = toHeadersRecord(route.headersDestiny);
6665
for (const [key, value] of Object.entries(headersRecord)) {
@@ -69,22 +68,22 @@ async function handleSync(request: Request, content: string, routeRow: PathRoute
6968
if (route.contentTypeDestiny) {
7069
headers.set('Content-Type', route.contentTypeDestiny);
7170
}
72-
71+
7372
const destinyResponse = await fetch(asyncConfig.destiny! + (!fullpath ? '' : fullpath), {
7473
method: asyncConfig.methodDestiny || request.method,
7574
headers: headers,
7675
body: content || null
7776
});
78-
77+
7978
const destinyTime = new Date();
8079
const destinyBody = await destinyResponse.text();
81-
82-
records.push(async (_a: unknown) => {
83-
84-
// OUT log
85-
const outId = await randomHEX();
86-
const outFilename = mqfilename(destinyTime, outId);
87-
env.CFGATEWAY.put(outFilename, destinyBody);
80+
81+
// OUT log - starts after fetch completes (needs destinyBody)
82+
const outId = await randomHEX();
83+
const outFilename = mqfilename(destinyTime, outId);
84+
85+
const outPromise = Promise.all([
86+
env.CFGATEWAY.put(outFilename, destinyBody),
8887
env.MQCFGATEWAY.send({
8988
id: outId,
9089
parent: inId,
@@ -93,18 +92,18 @@ async function handleSync(request: Request, content: string, routeRow: PathRoute
9392
time: destinyTime.getTime(),
9493
type: 'out',
9594
lab
96-
} as MQCFGATEWAYMessage, { contentType: 'json' });
97-
});
98-
95+
} as MQCFGATEWAYMessage, { contentType: 'json' })
96+
]);
97+
9998
// Return response to client
10099
const responseHeaders = new Headers();
101100
const contentType = destinyResponse.headers.get('Content-Type');
102101
if (contentType) {
103102
responseHeaders.set('Content-Type', contentType);
104103
}
105-
106-
await Promise.allSettled(records);
107-
104+
105+
await Promise.all([inPromise, outPromise]);
106+
108107
return new Response(destinyBody, {
109108
status: destinyResponse.status,
110109
headers: responseHeaders

0 commit comments

Comments
 (0)