Skip to content

Add fastify plugin to get body from cache#1171

Merged
MarceloRGonc merged 32 commits into
mainfrom
mg/OPS-2396
Aug 27, 2025
Merged

Add fastify plugin to get body from cache#1171
MarceloRGonc merged 32 commits into
mainfrom
mg/OPS-2396

Conversation

@MarceloRGonc

Copy link
Copy Markdown
Contributor

Part of OPS-2396.

Additional Notes

  • Adds Fastify Plugin that converts body with cache key to the request from cache

Copilot AI review requested due to automatic review settings August 19, 2025 16:59
@linear

linear Bot commented Aug 19, 2025

Copy link
Copy Markdown

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a Fastify plugin that enables caching and retrieval of request bodies, supporting scenarios where large request bodies need to be temporarily stored in cache and accessed via a key reference.

  • Implements cache-based body storage and retrieval functions (saveRequestBody, getRequestBody)
  • Creates a Fastify plugin that automatically converts body access keys to actual cached bodies
  • Provides comprehensive test coverage for all functionality

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
packages/server/shared/src/lib/request-oversize-body-handler.ts Core implementation with cache operations and Fastify plugin
packages/server/shared/test/request-oversize-body-handler.test.ts Complete test suite covering all functions and plugin behavior

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread packages/server/shared/src/lib/request-oversize-body-handler.ts Outdated
Comment on lines +44 to +53
app.addHook('preValidation', async (request, reply) => {
if (!isBodyAccessKeyRequest(request.body)) {
return;
}

const resourceKey = (request.body as { bodyAccessKey: string })
.bodyAccessKey;

request.body = await getRequestBody(resourceKey);
});

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will validate if the body was saved in the cache and pick it up to the request

Comment on lines +8 to +15
export const BodyAccessKeyRequest = Type.Object(
{
bodyAccessKey: Type.String(),
},
{ additionalProperties: false },
);

export type BodyAccessKeyRequest = Static<typeof BodyAccessKeyRequest>;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the body of a request saved in cache

@maor-rozenfeld

Copy link
Copy Markdown
Contributor

Did you manage to get it to work without crashing redis?

): Promise<string> {
const startTime = performance.now();

const bodyAccessKey = `req:${requestId}`;

@MarceloRGonc MarceloRGonc Aug 22, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests, we save the compressed or packed version it depends on the size

compressedSize: 28380276 bytes -> 28.380276 MB
packedSize: 37833793 bytes -> 37.833793 MB
rawSize: 37834156 -> 37.834156 MB

Request From Server: (I think we can improve here, I will open a ticket)
compressedSize: 866586 bytes -> 28.380276 MB
packedSize: 24157497 bytes -> 37.833793 MB
rawSize: 26177868 -> 37.834156 MB

Request from Engine
compressedSize: 866586 bytes -> 0.866586MB
packedSize: 24157497 bytes -> 24.157497MB
rawSize: 26177868 bytes -> 26.177868MB

@MarceloRGonc
MarceloRGonc changed the base branch from main to mg/OPS-2460 August 25, 2025 13:57
@MarceloRGonc
MarceloRGonc marked this pull request as draft August 25, 2025 13:57
Base automatically changed from mg/OPS-2460 to main August 26, 2025 11:09
Comment on lines +79 to +84
logger.error(
'Redis maximum memory reached while saving request body to cache.',
{
errorMessage: error.message,
},
);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the message we get when Redis reaches the maximum

@MarceloRGonc
MarceloRGonc marked this pull request as ready for review August 26, 2025 13:07

@MarceloRGonc MarceloRGonc left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maor-rozenfeld We are no longer crashing redis.

@sonarqubecloud

Copy link
Copy Markdown

@MarceloRGonc
MarceloRGonc merged commit ae1bc73 into main Aug 27, 2025
18 of 19 checks passed
@MarceloRGonc
MarceloRGonc deleted the mg/OPS-2396 branch August 27, 2025 09:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants