Skip to content

Instantly share code, notes, and snippets.

@behnammohammadi
Last active May 1, 2023 09:15
Show Gist options
  • Select an option

  • Save behnammohammadi/cbb7cf9a22f93afa12f70c4fd7e4ddde to your computer and use it in GitHub Desktop.

Select an option

Save behnammohammadi/cbb7cf9a22f93afa12f70c4fd7e4ddde to your computer and use it in GitHub Desktop.
public async Task SearchProxy()
{
var timeWindow = DateTime.Now.AddMinutes(-5);
_failure.RemoveAll(x => x < timeWindow);
_success.RemoveAll(x => x < timeWindow);
try
{
var response = await _httpClient.GetAsync("api/crawler/search" + Request.QueryString,
new CancellationTokenSource(TimeSpan.FromSeconds(30)).Token);
response.EnsureSuccessStatusCode();
foreach (var header in response.Headers)
Response.Headers.Add(header.Key, string.Join(",", header.Value));
foreach (var header in response.Content.Headers)
Response.Headers.Add(header.Key, string.Join(",", header.Value));
await response.Content.CopyToAsync(Response.Body);
_success.Add(DateTime.Now);
}
catch
{
_failure.Add(DateTime.Now);
if (_lastRestart < timeWindow && _failure.Count > _success.Count)
{
_lastRestart = DateTime.Now;
Restart();
}
throw;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment