Skip to content

Commit

Permalink
chore: Re-apply oversized result handling (#377)
Browse files Browse the repository at this point in the history
* Revert "Revert "chore: Enforce 500kb maximum result size (#375)" (#376)"

This reverts commit 063f650.

* fix: Re-apply oversized result handling
  • Loading branch information
johnjcsmith authored Dec 26, 2024
1 parent 2489bf7 commit 13dfb41
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions control-plane/src/modules/calls/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { initServer } from "@ts-rest/fastify";
import * as jobs from "../jobs/jobs";
import { packer } from "../packer";
import { upsertMachine } from "../machines";
import { BadRequestError } from "../../utilities/errors";
import { BadRequestError, NotFoundError } from "../../utilities/errors";
import { createBlob } from "../blobs";
import { logger } from "../observability/logger";
import { recordServicePoll } from "../service-definitions";
import { resumeRun } from "../workflows/workflows";
import { getJob } from "../jobs/jobs";

export const callsRouter = initServer().router(
{
Expand Down Expand Up @@ -75,7 +75,8 @@ export const callsRouter = initServer().router(
},
createCallResult: async (request) => {
const { clusterId, callId } = request.params;
const { result, resultType, meta } = request.body;
let { result, resultType } = request.body;
const { meta } = request.body;

const machine = request.request.getAuth().isMachine();
machine.canAccess({ cluster: { clusterId } });
Expand Down Expand Up @@ -112,6 +113,44 @@ export const callsRouter = initServer().router(
}
}

if (!!result) {
// Max result size 500kb
const data = Buffer.from(JSON.stringify(result));
if (Buffer.byteLength(data) > 500 * 1024) {
logger.info("Call result too large, persisting as blob", {
callId,
})

const call = await getJob({ clusterId, jobId: callId });

if (!call) {
throw new NotFoundError("Call not found");
}


await createBlob({
data: data.toString("base64"),
size: Buffer.byteLength(data),
encoding: "base64",
type: "application/json",
name: "Oversize call result",
clusterId,
runId: call.runId ?? undefined,
jobId: callId ?? undefined,
});

result = {
message: "The result was too large and was returned to the user directly",
};

resultType = "rejection";
}
} else {
logger.warn("Call result is empty", {
callId,
})
}

await Promise.all([
upsertMachine({
clusterId,
Expand Down

0 comments on commit 13dfb41

Please sign in to comment.