Example #1
0
        public override async Task <string> ProcessRequestAsync(string query, TimeSpan timeout)
        {
            timeout = TimeSpan.FromSeconds(timeout.TotalSeconds / ReplicaAddresses.Length);

            var shuffledAddresses = ReplicaAddresses.Shuffle();
            var filtredAddresses  = shuffledAddresses.Where(server => DateTime.Now - mutedServers[server] > TimeInBan).ToList();

            if (filtredAddresses.Count == 0)
            {
                filtredAddresses = shuffledAddresses.OrderBy(server => DateTime.Now - mutedServers[server]).ToList();
            }

            var webRequests = filtredAddresses.Select(uri => CreateRequest(uri + "?query=" + query));

            foreach (var request in webRequests)
            {
                Log.InfoFormat("Processing {0}", request.RequestUri);
                var task = ProcessRequestAsync(request);
                await Task.WhenAny(task, Task.Delay(timeout));

                if (task.IsCompleted && !task.IsFaulted)
                {
                    return(task.Result);
                }
                mutedServers[request.Host] = DateTime.Now;
            }

            throw new TimeoutException();
        }
        public override async Task <string> ProcessRequestAsync(string query, TimeSpan timeout)
        {
            var time = TimeSpan.FromSeconds(timeout.TotalSeconds / ReplicaAddresses.Length);

            var shuffledAddresses = ReplicaAddresses.Shuffle();
            var webRequests       = shuffledAddresses.Select(uri => CreateRequest(uri + "?query=" + query));

            foreach (var request in webRequests)
            {
                Log.InfoFormat("Processing {0}", request.RequestUri);
                var task = ProcessRequestAsync(request);
                await Task.WhenAny(task, Task.Delay(time));

                if (task.IsCompleted && !task.IsFaulted)
                {
                    return(task.Result);
                }
            }

            throw new TimeoutException();
        }
Example #3
0
        public override async Task <string> ProcessRequestAsync(string query, TimeSpan timeout)
        {
            ReplicaAddresses.Shuffle(rand);
            var timeoutOneTask   = new TimeSpan(timeout.Ticks / ReplicaAddresses.Length);
            var timerForAllTasks = Task.Delay(timeout);

            var resultTasks = new List <Task> {
                timerForAllTasks
            };

            foreach (var uri in ReplicaAddresses)
            {
                var timer      = Task.Delay(timeoutOneTask);
                var webRequest = CreateRequest(uri + "?query=" + query);
                Log.InfoFormat("Processing {0}", webRequest.RequestUri);
                resultTasks.Add(ProcessRequestAsync(webRequest));
                var taskWithTimer = resultTasks.Concat(new[] { timer }).ToList();
                var completed     = await Task.WhenAny(taskWithTimer);

                if (timerForAllTasks.IsCompleted)
                {
                    throw new TimeoutException();
                }
                if (timer.IsCompleted)
                {
                    continue;
                }
                if (!completed.IsFaulted)
                {
                    return(((Task <string>)completed).Result);
                }

                resultTasks.Remove(completed);
            }

            throw new TimeoutException();
        }