Last active
May 1, 2023 09:15
-
-
Save behnammohammadi/cbb7cf9a22f93afa12f70c4fd7e4ddde to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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