Skip to content

fix: Retry flags requests on 502 and 504#209

Merged
marandaneto merged 4 commits into
mainfrom
fix/flags-retry-502-504
Jul 2, 2026
Merged

fix: Retry flags requests on 502 and 504#209
marandaneto merged 4 commits into
mainfrom
fix/flags-retry-502-504

Conversation

@marandaneto

Copy link
Copy Markdown
Member

💡 Motivation and Context

Remote /flags/?v=2 evaluations retried transient transport failures via feature_flag_request_max_retries, but HTTP 502/504 responses were surfaced immediately. The SDK compliance harness expects /flags to retry transient gateway responses while keeping retry behavior scoped to feature flag evaluation.

This replaces the fork-based PR #208 with an equivalent branch pushed directly to PostHog/posthog-ruby.

💚 How did you test it?

ruby -c lib/posthog/feature_flags.rb
bundle exec rspec spec/posthog/flags_spec.rb
bundle exec rubocop lib/posthog/feature_flags.rb spec/posthog/flags_spec.rb

Validation from the original branch:

  • ruby -c lib/posthog/feature_flags.rbSyntax OK
  • bundle exec rspec spec/posthog/flags_spec.rb80 examples, 0 failures
  • bundle exec rubocop lib/posthog/feature_flags.rb spec/posthog/flags_spec.rb2 files inspected, no offenses detected

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran pnpm changeset to generate a changeset file

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

Implemented by an AI coding agent under human direction. The change keeps HTTP status-code retries scoped to the /flags remote evaluation request path and adds focused specs for 502/504 retry behavior, non-retry statuses, and max-retries opt-out. A changeset covers both posthog-ruby and posthog-rails, since Rails consumes the Ruby SDK behavior.

@marandaneto marandaneto self-assigned this Jul 2, 2026
@marandaneto marandaneto marked this pull request as ready for review July 2, 2026 11:55
@marandaneto marandaneto requested a review from a team as a code owner July 2, 2026 11:55
@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown

posthog-ruby Compliance Report

Date: 2026-07-02 12:22:20 UTC
Duration: 98009ms

✅ All Tests Passed!

45/45 tests passed


Capture Tests

29/29 tests passed

View Details
Test Status Duration
Format Validation.Event Has Required Fields 110ms
Format Validation.Event Has Uuid 106ms
Format Validation.Event Has Lib Properties 109ms
Format Validation.Distinct Id Is String 107ms
Format Validation.Token Is Present 108ms
Format Validation.Custom Properties Preserved 106ms
Format Validation.Event Has Timestamp 108ms
Retry Behavior.Retries On 503 5414ms
Retry Behavior.Does Not Retry On 400 2109ms
Retry Behavior.Does Not Retry On 401 2109ms
Retry Behavior.Respects Retry After Header 8117ms
Retry Behavior.Implements Backoff 15622ms
Retry Behavior.Retries On 500 5214ms
Retry Behavior.Retries On 502 5212ms
Retry Behavior.Retries On 504 5212ms
Retry Behavior.Max Retries Respected 15522ms
Deduplication.Generates Unique Uuids 112ms
Deduplication.Preserves Uuid On Retry 5213ms
Deduplication.Preserves Uuid And Timestamp On Retry 10318ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 5214ms
Deduplication.No Duplicate Events In Batch 112ms
Deduplication.Different Events Have Different Uuids 107ms
Compression.Sends Gzip When Enabled 107ms
Batch Format.Uses Proper Batch Structure 107ms
Batch Format.Flush With No Events Sends Nothing 5ms
Batch Format.Multiple Events Batched Together 110ms
Error Handling.Does Not Retry On 403 2110ms
Error Handling.Does Not Retry On 413 2109ms
Error Handling.Retries On 408 5212ms

Feature_Flags Tests

16/16 tests passed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id 109ms
Request Payload.Flags Request Uses V2 Query Param 109ms
Request Payload.Flags Request Hits Flags Path Not Decide 109ms
Request Payload.Flags Request Omits Authorization Header 108ms
Request Payload.Token In Flags Body Matches Init 108ms
Request Payload.Groups Round Trip 108ms
Request Payload.Groups Default To Empty Object 107ms
Request Payload.Person Properties Distinct Id Auto Populated When Caller Omits It 108ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False 107ms
Request Payload.Disable Geoip Omitted Defaults To False 108ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key 108ms
Request Lifecycle.No Flags Request On Init Alone 3ms
Request Lifecycle.No Flags Request On Normal Capture 106ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests 111ms
Request Lifecycle.Mock Response Value Is Returned To Caller 106ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event 110ms

@greptile-apps

greptile-apps Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Comments Outside Diff (1)

  1. spec/posthog/flags_spec.rb, line 342-352 (link)

    P2 Missing HTTP-status retry coverage for max_retries > 1

    The "set to a higher count" context (max_retries = 3) only exercises the Errno::ECONNRESET exception path — it has no equivalent test for the new HTTP 502/504 retry branch. Without it there is no verification that attempts <= @feature_flag_request_max_retries fires the correct number of times for status-code retries, and a future refactor could silently break the exhaustion path for those codes. A parameterised example over [502, 504] that stubs persistent gateway errors and asserts 4 total requests (1 + 3 retries) would give the same coverage that already exists for exception retries.

    Rule Used: When implementing retry logic, include comprehensi... (source)

    Learned From
    PostHog/posthog#32651

    Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Reviews (1): Last reviewed commit: "chore: include rails SDK in flags retry ..." | Re-trigger Greptile

@marandaneto

Copy link
Copy Markdown
Member Author

Addressed Greptile outside-diff comment from #209 (comment) in 5eefd01 by adding parameterized 502/504 coverage for feature_flag_request_max_retries = 3. The new examples assert 4 total requests (initial + 3 retries), the final exhausted response, and 3 backoff sleeps. Validation passed: bundle exec rspec spec/posthog/flags_spec.rb (82 examples, 0 failures), bundle exec rubocop spec/posthog/flags_spec.rb, and ruby -c lib/posthog/feature_flags.rb.

@marandaneto marandaneto merged commit 1376f19 into main Jul 2, 2026
19 checks passed
@marandaneto marandaneto deleted the fix/flags-retry-502-504 branch July 2, 2026 13:28
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.

2 participants