Skip to content

Commit f50c80e

Browse files
committed
fix: make flags retry count configurable
1 parent 6bc124b commit f50c80e

5 files changed

Lines changed: 32 additions & 2 deletions

File tree

.changeset/quiet-flags-retry.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
'PostHog': patch
33
---
44

5-
Retry feature flag requests after transient network errors only.
5+
Retry feature flag requests after transient network errors only. The feature flag request retry count defaults to 1 and can be set to 0 to disable retries.

src/PostHog/Config/PostHogOptions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ public string? ProjectApiKey
173173
/// </remarks>
174174
public int MaxRetries { get; set; } = 3;
175175

176+
/// <summary>
177+
/// The maximum number of retries for feature flag requests after transient network errors. (Default: 1)
178+
/// Set to 0 to disable feature flag request retries.
179+
/// </summary>
180+
public int FeatureFlagRequestMaxRetries { get; set; } = 1;
181+
176182
/// <summary>
177183
/// The initial delay between retries. (Default: 1 second)
178184
/// </summary>

src/PostHog/Library/HttpClientExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ internal static class HttpClientExtensions
5555
PostHogOptions options,
5656
CancellationToken cancellationToken)
5757
{
58-
var maxRetries = options.MaxRetries;
58+
var maxRetries = options.FeatureFlagRequestMaxRetries;
5959
var currentDelay = options.InitialRetryDelay;
6060
var maxDelay = options.MaxRetryDelay;
6161
var attempt = 0;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
#nullable enable
2+
PostHog.PostHogOptions.FeatureFlagRequestMaxRetries.get -> int
3+
PostHog.PostHogOptions.FeatureFlagRequestMaxRetries.set -> void

tests/UnitTests/Library/HttpClientExtensionsTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,28 @@ public async Task RetriesOnConnectionResetThenSucceeds()
590590
Assert.Equal(2, handler.RequestCount);
591591
}
592592

593+
[Fact]
594+
public async Task DoesNotRetryWhenFeatureFlagRequestMaxRetriesIsZero()
595+
{
596+
var handler = new FakeRetryHttpMessageHandler();
597+
handler.AddException(new HttpRequestException("Connection reset", new SocketException((int)SocketError.ConnectionReset)));
598+
handler.AddResponse(HttpStatusCode.OK, new { flags = new { } });
599+
using var httpClient = CreateHttpClient(handler);
600+
var options = CreateOptions();
601+
options.FeatureFlagRequestMaxRetries = 0;
602+
var timeProvider = new FakeTimeProvider();
603+
604+
await Assert.ThrowsAsync<HttpRequestException>(() =>
605+
httpClient.PostJsonWithNetworkRetryAsync<FlagsApiResult>(
606+
FlagsUrl,
607+
new { api_key = "test", distinct_id = "user-1" },
608+
timeProvider,
609+
options,
610+
CancellationToken.None));
611+
612+
Assert.Equal(1, handler.RequestCount);
613+
}
614+
593615
[Fact]
594616
public async Task DoesNotRetryConnectionRefused()
595617
{

0 commit comments

Comments
 (0)