Add fastify plugin to get body from cache#1171
Conversation
There was a problem hiding this comment.
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.
| app.addHook('preValidation', async (request, reply) => { | ||
| if (!isBodyAccessKeyRequest(request.body)) { | ||
| return; | ||
| } | ||
|
|
||
| const resourceKey = (request.body as { bodyAccessKey: string }) | ||
| .bodyAccessKey; | ||
|
|
||
| request.body = await getRequestBody(resourceKey); | ||
| }); |
There was a problem hiding this comment.
This will validate if the body was saved in the cache and pick it up to the request
| export const BodyAccessKeyRequest = Type.Object( | ||
| { | ||
| bodyAccessKey: Type.String(), | ||
| }, | ||
| { additionalProperties: false }, | ||
| ); | ||
|
|
||
| export type BodyAccessKeyRequest = Static<typeof BodyAccessKeyRequest>; |
There was a problem hiding this comment.
This is the body of a request saved in cache
|
Did you manage to get it to work without crashing redis? |
| ): Promise<string> { | ||
| const startTime = performance.now(); | ||
|
|
||
| const bodyAccessKey = `req:${requestId}`; |
There was a problem hiding this comment.
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
| logger.error( | ||
| 'Redis maximum memory reached while saving request body to cache.', | ||
| { | ||
| errorMessage: error.message, | ||
| }, | ||
| ); |
There was a problem hiding this comment.
This is the message we get when Redis reaches the maximum
MarceloRGonc
left a comment
There was a problem hiding this comment.
@maor-rozenfeld We are no longer crashing redis.
|



Part of OPS-2396.
Additional Notes