public override async Task <string> ProcessRequestAsync(string query, TimeSpan timeout) { if (Cache.ContainsKey(query)) { return(Cache[query]); } var webRequests = ReplicaAddresses.Select(uri => CreateRequest(uri + "?query=" + query)); var resultTasks = new List <Task <string> >(); foreach (var request in webRequests) { Log.InfoFormat("Processing {0}", request.RequestUri); resultTasks.Add(ProcessRequestAsync(request)); } var resultTask = Task.WhenAny(resultTasks); await Task.WhenAny(resultTask, Task.Delay(timeout)); if (!resultTask.IsCompleted && !resultTask.IsFaulted) { throw new TimeoutException(); } var requestResult = resultTask.Result.Result; Cache[query] = requestResult; return(requestResult); }
public override async Task <string> ProcessRequestAsync(string query, TimeSpan timeout) { var webRequests = ReplicaAddresses.Select(uri => CreateRequest(uri + "?query=" + query)).ToList(); var resultTasks = new List <Task <string> >(); foreach (var request in webRequests) { Log.InfoFormat("Processing {0}", request.RequestUri); resultTasks.Add(ProcessRequestAsync(request)); } var result = Task.WhenAny(resultTasks); await Task.WhenAny(result, Task.Delay(timeout)); if (!result.IsCompleted && !result.IsFaulted) { throw new TimeoutException(); } for (var i = 0; i < resultTasks.Count; i++) { if (resultTasks[i].IsCompleted) { continue; } var cancellation = CreateRequest(ReplicaAddresses[i] + "?query=" + query); cancellation.Method = "DELETE"; ProcessRequestAsync(cancellation); } return(result.Result.Result); }
protected override async Task <string> ProcessRequestAsyncInternal(string query, TimeSpan timeout) { // в случаае, если одна реплика не работает, то ъждать ответа от остальных var tasks = ReplicaAddresses.Select(async addr => { var request = CreateRequest($"{addr}?query={query}", timeout); return(await ProcessRequestAsync(request).WithTimeout(timeout)); }); return(await await TaskExtensions.WhenAnySucceded(tasks)); }
public override async Task <string> ProcessRequestAsync(string query, TimeSpan timeout) { return((await WaitForAnyNonFaultedTaskAsync( ReplicaAddresses.Select(replica => Task.Run(async() => { var webRequest = CreateRequest(replica + "?query=" + query); Log.InfoFormat("Processing {0}", webRequest.RequestUri); var resultTask = ProcessRequestAsync(webRequest); await Task.WhenAny(resultTask, Task.Delay(timeout)); return resultTask.Result; })).ToArray()))?.Result); }
public override async Task <string> ProcessRequestAsync(string query, TimeSpan timeout) { var newTimeout = TimeSpan.FromMilliseconds((double)timeout.TotalMilliseconds / ReplicaAddresses.Length); return((await WaitForAnyNonFaultedTaskAsync( ReplicaAddresses.Select(replica => Task.Run(async() => { var webRequest = CreateRequest(replica + "?query=" + query); Log.InfoFormat("Processing {0}", webRequest.RequestUri); var resultTask = ProcessRequestAsync(webRequest); await Task.WhenAny(resultTask, Task.Delay(newTimeout)); //Console.WriteLine("1" + resultTask.IsCompleted); if (resultTask.IsCompleted) { return resultTask.Result; } //Console.WriteLine("2" + resultTask.IsCompleted); await Task.WhenAny(resultTask); //Console.WriteLine("3" + resultTask.IsCompleted); return resultTask.Result; })).ToArray()))?.Result); var bigTask = await WaitForAnyNonFaultedTaskAsync(Task.Run(async() => { foreach (var replica in RandomizeReplicas()) { var webRequest = CreateRequest(replica + "?query=" + query); Log.InfoFormat("Processing {0}", webRequest.RequestUri); var resultTask = ProcessRequestAsync(webRequest); await Task.WhenAny(resultTask, Task.Delay(newTimeout)); if (!resultTask.IsFaulted && resultTask.IsCompleted) { return(resultTask.Result); } } throw new Exception("No Replica Answered"); })); if (bigTask == null || bigTask.IsFaulted) { return(null); } return(bigTask.Result); }
public override async Task <string> ProcessRequestAsync(string query, TimeSpan timeout) { var queryTasks = ReplicaAddresses .Select(uri => CreateRequest(uri + "?query=" + query)) .Select(ProcessRequestAsync) .Concat(new[] { Task.Delay(timeout) }) .ToArray(); var task = await Task.WhenAny(queryTasks); var result = (task as Task <string>)?.Result; if (result == null) { throw new TimeoutException(); } return(result); }
public override async Task <string> ProcessRequestAsync(string query, TimeSpan timeout) { try { var tasks = ReplicaAddresses.Select(async uri => { var webRequest = CreateRequest(uri + "?query=" + query); Log.InfoFormat($"Processing {webRequest.RequestUri}"); var resultTask = ProcessRequestAsync(webRequest); await Task.WhenAny(resultTask, Task.Delay(timeout)); if (!resultTask.IsCompletedSuccessfully) { throw new TimeoutException(); } return(resultTask.Result); }).ToArray(); while (tasks.Length > 0) { var task = await Task.WhenAny(tasks); if (!task.IsCompletedSuccessfully) { tasks = tasks.Except(new[] { task }).ToArray(); } else { return(task.Result); } } throw new TimeoutException(); } catch (AggregateException e) { if (e.InnerException != null) { throw e.InnerException; } throw; } }
public override async Task <string> ProcessRequestAsync(string query, TimeSpan timeout) { var tasks = ReplicaAddresses .Select(uri => CreateRequest($"{uri}?query={query}")) .Select(request => { Log.InfoFormat($"Processing {request.RequestUri}"); return(ProcessRequestAsync(request)); }) .ToArray(); var resultTask = Task.WhenAny(tasks); await Task.WhenAny(resultTask, Task.Delay(timeout)); if (!resultTask.IsCompleted) { throw new TimeoutException(); } return(resultTask.Result.Result); }
public override async Task <string> ProcessRequestAsync(string query, TimeSpan timeout) { var tasks = ReplicaAddresses.Select(async(uri) => { var webRequest = CreateRequest(uri + "?query=" + query); Console.WriteLine($"Processing {webRequest.RequestUri}"); return(await ProcessRequestAsync(webRequest)); }); var timeoutTask = Task.Delay(timeout); tasks.Append(timeoutTask); var resultTask = await Task.WhenAny(tasks); if (!resultTask.IsCompleted) { throw new TimeoutException(); } return(resultTask.Result); }
public async override Task <string> ProcessRequestAsync(string query, TimeSpan timeout) { var responseTasks = new Dictionary <WebRequest, Task <string> >(); var globalTimer = Stopwatch.StartNew(); foreach (var webRequest in ReplicaAddresses.Select(uri => CreateRequest(uri + "?query=" + query))) { Log.InfoFormat($"Processing {webRequest.RequestUri} {DateTime.Now.TimeOfDay}"); var interRequestsDelay = new TimeSpan((timeout - globalTimer.Elapsed).Ticks / (ReplicaAddresses.Length - responseTasks.Count)); if (interRequestsDelay < TimeSpan.Zero) { throw new TimeoutException(); } responseTasks[webRequest] = ProcessRequestAsync(webRequest); await Task.WhenAny(responseTasks.Values.Concat(new[] { Task.Delay(interRequestsDelay) })); var responseTask = responseTasks.Values.FirstOrDefault(t => t.IsCompleted); if (responseTask == null) { continue; } foreach (var webRequestToAbort in responseTasks.Where(t => !t.Value.IsCompleted).Select(t => t.Key)) { webRequestToAbort.Abort(); Log.InfoFormat("WebRequest to {0} was aborted", webRequestToAbort.RequestUri); } return(responseTask.Result); } throw new TimeoutException(); }
public async override Task <string> ProcessRequestAsync(string query, TimeSpan timeout) { var webRequests = ReplicaAddresses .Select(uri => CreateRequest(uri + "?query=" + query)) .ToArray(); foreach (var request in webRequests) { Log.InfoFormat("Processing {0}", request); } var requestTasks = webRequests .Select(async request => await ProcessRequestAsync(request)) .ToList(); while (requestTasks.Count > 0) { var resultTask = await Task.WhenAny(requestTasks); await Task.WhenAny(resultTask, Task.Delay(timeout)); if (!resultTask.IsCompleted) { throw new TimeoutException(); } if (!resultTask.IsFaulted) { return(resultTask.Result); } requestTasks.Remove(resultTask); } throw new TimeoutException(); }