コード例 #1
0
ファイル: PriorityClient.cs プロジェクト: DianaArapova/async
        public override async Task <string> ProcessRequestAsync(string query, TimeSpan timeout)
        {
            var replica         = ReplicaAddresses.ToList();
            var priorityReplica = replica
                                  .Select((t, i) => new Tuple <int, string>(Priority[i], t))
                                  .ToList();

            priorityReplica.Sort();
            var timeoutOneTask = new TimeSpan(timeout.Ticks / ReplicaAddresses.Length);

            for (var i = 0; i < priorityReplica.Count; i++)
            {
                var priorityReplicaAddress = priorityReplica[i];
                var replicaAddress         = priorityReplicaAddress.Item2;
                var webRequest             = CreateRequest(replicaAddress + "?query=" + query);
                Log.InfoFormat("Processing {0}", webRequest.RequestUri);

                var resultTask = ProcessRequestAsync(webRequest);
                await Task.WhenAny(resultTask, Task.Delay(timeoutOneTask));

                if (!resultTask.IsCompleted || resultTask.IsFaulted)
                {
                    Priority[i] += 1;
                    continue;
                }

                Priority[i] -= 1;
                return(resultTask.Result);
            }

            throw new TimeoutException();
        }
コード例 #2
0
        public IEnumerable <string> RandomizeReplicas()
        {
            var replicasToList = ReplicaAddresses.ToList();

            while (replicasToList.Count > 0)
            {
                var uri = replicasToList[new Random().Next(replicasToList.Count)];
                replicasToList.Remove(uri);
                yield return(uri);
            }
        }