Skip to content

Add gzip compression for responses larger than 1KB#448

Open
ayo-ola0710 wants to merge 1 commit intoSoroScan:mainfrom
ayo-ola0710:feature/gzip-responses
Open

Add gzip compression for responses larger than 1KB#448
ayo-ola0710 wants to merge 1 commit intoSoroScan:mainfrom
ayo-ola0710:feature/gzip-responses

Conversation

@ayo-ola0710
Copy link
Copy Markdown
Contributor

📌 Summary

Implements gzip compression for REST and GraphQL responses to reduce network payload size (#320).

🚀 What was done

  • Created a custom ConditionalGZipMiddleware in django-backend/soroscan/middleware.py.
  • Inherited from Django's built-in GZipMiddleware to cleanly leverage standard compression logic while enforcing our specific threshold.
  • Placed the middleware strategically within the MIDDLEWARE list in settings.py so it processes responses late in the request lifecycle.
  • Included robust tests to verify the compression applies accurately.

🧠 Key Logic

  • Conditional Compression: Hooked into the response processing to ensure we only compress payloads larger than 1KB. Small responses bypass the compression step to avoid CPU overhead and the situation where compression increases the payload size.
  • Double Compression Avoidance: Leverages existing headers to check if the Content-Encoding header has already been set.
  • Client Support Validation: Native Django middleware handles the Accept-Encoding: gzip requirement seamlessly.

🧪 Tests

  • test_compresses_large_response: Verifies responses > 1KB get gzipped and have the correct headers.
  • test_does_not_compress_small_response: Ensures responses < 1KB remain uncompressed to avoid performance impact.
  • test_does_not_compress_if_already_compressed: Guarantees we do not double-compress an already gzipped response.

Closes #320

- Created ConditionalGZipMiddleware extending GZipMiddleware

- Configured middleware to only compress responses > 1KB

- Added ConditionalGZipMiddlewareTests to verify compression logic
@drips-wave
Copy link
Copy Markdown

drips-wave Bot commented Apr 28, 2026

@ayo-ola0710 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

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.

Add gzip compression to REST and GraphQL responses

1 participant