-
Notifications
You must be signed in to change notification settings - Fork 30
feat(flags): Add ETag support for local evaluation polling #139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Use conditional requests with If-None-Match header to reduce bandwidth when polling for feature flag definitions. The server returns 304 Not Modified when flags haven't changed, avoiding unnecessary data transfer. - Store ETag from server responses - Send If-None-Match header on subsequent requests - Handle 304 Not Modified by keeping cached flags - Clear ETag on quota limit (402) or when server stops sending it
Move defer res.Body.Close() immediately after the error check to ensure the response body is closed for all status codes, not just 200 OK. Previously, early returns for 304 Not Modified and 402 Payment Required would skip the defer, causing connection pool leaks over time.
- Use GetFeatureFlags() which blocks until initial fetch completes - Use waitForCondition() helper with polling for ForceReload operations - Use atomic.Value instead of mutex for thread-safe string storage - Tests are now faster (0.06s vs 1.22s) and more reliable
Verify that the stored ETag is preserved when the server returns a 304 Not Modified response without including an ETag header. This is an edge case but ensures defensive behavior.
There was a problem hiding this comment.
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 HTTP conditional request support using ETags to the feature flags polling mechanism for local evaluation. When flag definitions haven't changed, the server can return a 304 Not Modified response, allowing the SDK to skip reprocessing and reduce bandwidth consumption.
Key changes:
- Added
flagsEtagfield toFeatureFlagsPollerto store the current ETag - Modified
fetchNewFeatureFlags()to sendIf-None-Matchheaders and handle 304 responses - Added comprehensive test coverage for various ETag scenarios
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| featureflags.go | Implements ETag storage, conditional request headers, and 304 response handling in the feature flags polling logic |
| featureflags_etag_test.go | Adds comprehensive test coverage for ETag functionality including storage, conditional requests, 304 handling, ETag updates, and edge cases |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| return | ||
| } | ||
| defer cancel() | ||
| defer res.Body.Close() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved this call here because before:
- If status was
402(quota limit) → returned early, body never closed (leak!) - If status was not
200→ returned early, body never closed (leak!)
Adds an interactive example to demonstrate ETag support for local evaluation polling. The example polls every 5 seconds and logs the If-None-Match header sent and the response status (200 vs 304) to show when cached flags are used.
Per review feedback, this makes the tests simpler.
Add support for HTTP conditional requests using ETags to reduce bandwidth when polling for feature flag definitions. When flag definitions haven't changed, the server returns 304 Not Modified and the SDK skips processing.
Based on PostHog/posthog-python#381