Skip to content
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

Revert to long polling in the ServalClientHelper. Fixes #98. #143

Merged
merged 8 commits into from
Oct 2, 2023
29 changes: 19 additions & 10 deletions tests/Serval.E2ETests/ServalClientHelper.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

public class ServalClientHelper
Expand All @@ -20,7 +21,7 @@ public ServalClientHelper(string audience, string prefix = "SCE_", bool ignoreSS
else
_httpClient = new HttpClient();
_httpClient.BaseAddress = new Uri(env["hostUrl"]);
_httpClient.Timeout = TimeSpan.FromSeconds(10);
_httpClient.Timeout = TimeSpan.FromSeconds(60);
dataFilesClient = new DataFilesClient(_httpClient);
translationEnginesClient = new TranslationEnginesClient(_httpClient);
_httpClient.DefaultRequestHeaders.Add(
Expand Down Expand Up @@ -112,20 +113,28 @@ public async Task<TranslationBuild> StartBuildAsync(string engineId)
public async Task BuildEngine(string engineId)
{
var newJob = await StartBuildAsync(engineId);
int revision = newJob.Revision;
await translationEnginesClient.GetBuildAsync(engineId, newJob.Id, newJob.Revision);
int pollIntervalMs = 500; // start throttle at 0.5 seconds
while (true)
{
var result = await translationEnginesClient.GetBuildAsync(engineId, newJob.Id);
if (!(result.State == JobState.Active || result.State == JobState.Pending))
try
{
// build completed
break;
var result = await translationEnginesClient.GetBuildAsync(engineId, newJob.Id, revision + 1);
if (!(result.State == JobState.Active || result.State == JobState.Pending))
{
// build completed
break;
}
revision = result.Revision;
}
catch (ServalApiException e)
{
if (e.StatusCode != 408)
throw;

// Throttle requests
await Task.Delay(500);
}
// Throttle requests
await Task.Delay(pollIntervalMs);
// increase throttle exponentially to 10 seconds
pollIntervalMs = (int)Math.Min(pollIntervalMs * 1.2, 10_000);
}
}

Expand Down