Skip to content

Commit deb6034

Browse files
committed
Adjust the backoff strategy calculations + prevent duplicated payload on recursive calls + fix out of range if forced test data set index would be to high
1 parent dbb0609 commit deb6034

3 files changed

Lines changed: 20 additions & 12 deletions

File tree

Assets/Plugins/StreamChat/Core/LowLevelClient/API/Internal/InternalApiClientBase.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,14 @@ private async Task<TResponse> HttpRequest<TResponse>(HttpMethodType httpMethod,
102102
queryParameters = QueryParameters.Default;
103103
}
104104

105-
queryParameters.Append("payload", serializedContent);
105+
if (queryParameters.ContainsKey("paload"))
106+
{
107+
queryParameters["payload"] = serializedContent;
108+
}
109+
else
110+
{
111+
queryParameters.Append("payload", serializedContent);
112+
}
106113
}
107114

108115
var uri = _requestUriFactory.CreateEndpointUri(endpoint, queryParameters);
@@ -307,14 +314,15 @@ private async Task<TResponse> HandleRateLimit<TResponse>(HttpMethodType httpMeth
307314
var delaySeconds = GetBackoffDelay(attempt, httpResponse, out var resetHeaderTimestamp);
308315
var now = (int)new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds();
309316
_logs.Warning($"API CLIENT, TESTS MODE, Rate Limit API Error - Wait for {delaySeconds} seconds. " +
310-
$"Timestamp reset header: {resetHeaderTimestamp}, Current timestamp: {now}, Dif: {now - resetHeaderTimestamp}");
317+
$"Timestamp reset header: {resetHeaderTimestamp}, Current timestamp: {now}, Dif: {resetHeaderTimestamp - now}");
311318
await Task.Delay(delaySeconds * 1000);
312319
return await HttpRequest<TResponse>(httpMethod, endpoint, requestBody, queryParameters, ++attempt);
313320
}
314321

315322
private int GetBackoffDelay(int attempt, HttpResponse httpResponse, out int resetHeaderTimestamp)
316323
{
317324
resetHeaderTimestamp = -1;
325+
// StreamTodo: Backoff based on the header doesn't seem to work. Perhaps concurrency is conflicting with this approach
318326
if (httpResponse.TryGetHeader("x-ratelimit-reset", out var values))
319327
{
320328
var resetTimestamp = values.FirstOrDefault();
@@ -324,14 +332,14 @@ private int GetBackoffDelay(int attempt, HttpResponse httpResponse, out int rese
324332
resetHeaderTimestamp = rateLimitTimestamp;
325333
var now = (int)new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds();
326334
var secondsLeft = rateLimitTimestamp - now;
327-
if (secondsLeft > 0)
328-
{
329-
return secondsLeft + 5;
330-
}
335+
// if (secondsLeft > 0)
336+
// {
337+
// return secondsLeft + 5;
338+
// }
331339
}
332340
}
333341

334-
return 61 + attempt * 10;
342+
return 61 + attempt * 20;
335343
}
336344
}
337345
}

Assets/Plugins/StreamChat/Tests/StatefulClient/BaseStateIntegrationTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,12 @@ protected Task<StreamChatClient> GetConnectedOtherClientAsync()
126126
/// <summary>
127127
/// Use this if state update depends on receiving WS event that might come after the REST call was completed
128128
/// </summary>
129-
protected static async Task WaitWhileTrueAsync(Func<bool> condition, int maxIterations = 500, int maxSeconds = 500)
129+
protected static async Task WaitWhileTrueAsync(Func<bool> condition, int maxSeconds = 1000)
130130
{
131131
var sw = new Stopwatch();
132132
sw.Start();
133133

134-
for (int i = 0; i < maxIterations; i++)
134+
for (int i = 0; i < int.MaxValue; i++)
135135
{
136136
if (!condition())
137137
{
@@ -150,12 +150,12 @@ protected static async Task WaitWhileTrueAsync(Func<bool> condition, int maxIter
150150
throw new TimeoutException("Timeout while waiting for condition");
151151
}
152152

153-
protected static async Task WaitWhileFalseAsync(Func<bool> condition, int maxIterations = 500, int maxSeconds = 500)
153+
protected static async Task WaitWhileFalseAsync(Func<bool> condition, int maxSeconds = 1000)
154154
{
155155
var sw = new Stopwatch();
156156
sw.Start();
157157

158-
for (int i = 0; i < maxIterations; i++)
158+
for (int i = 0; i < int.MaxValue; i++)
159159
{
160160
if (condition())
161161
{

Assets/Plugins/StreamChat/Tests/StreamTestClients.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ private StreamTestClients()
125125
}
126126

127127
AuthCredentials primaryAdminSet;
128-
if (optionalTestDataIndex.HasValue)
128+
if (optionalTestDataIndex.HasValue && optionalTestDataIndex.Value < testAuthDataSet.TestAdminData.Length)
129129
{
130130
Debug.LogWarning($"Using {nameof(optionalTestDataIndex)} to pick credentials test data set: {optionalTestDataIndex}");
131131
primaryAdminSet = testAuthDataSet.TestAdminData[optionalTestDataIndex.Value];

0 commit comments

Comments
 (0)