public async Task <ResponseTimeInformation> ExecuteAllPendingLazyOperationsAsync(CancellationToken token = default(CancellationToken))
        {
            using (AsyncTaskHolder())
            {
                var requests = new List <GetRequest>();
                for (int i = 0; i < PendingLazyOperations.Count; i++)
                {
                    var req = PendingLazyOperations[i].CreateRequest(Context);
                    if (req == null)
                    {
                        PendingLazyOperations.RemoveAt(i);
                        i--; // so we'll recheck this index
                        continue;
                    }

                    requests.Add(req);
                }

                if (requests.Count == 0)
                {
                    return(new ResponseTimeInformation());
                }

                try
                {
                    var sw = Stopwatch.StartNew();

                    IncrementRequestCount();

                    var responseTimeDuration = new ResponseTimeInformation();

                    while (await ExecuteLazyOperationsSingleStep(responseTimeDuration, requests, sw, token).ConfigureAwait(false))
                    {
                        await TimeoutManager.WaitFor(TimeSpan.FromMilliseconds(100), token).ConfigureAwait(false);
                    }

                    responseTimeDuration.ComputeServerTotal();


                    foreach (var pendingLazyOperation in PendingLazyOperations)
                    {
                        Action <object> value;
                        if (OnEvaluateLazy.TryGetValue(pendingLazyOperation, out value))
                        {
                            value(pendingLazyOperation.Result);
                        }
                    }

                    sw.Stop();
                    responseTimeDuration.TotalClientDuration = sw.Elapsed;
                    return(responseTimeDuration);
                }
                finally
                {
                    PendingLazyOperations.Clear();
                }
            }
        }
Beispiel #2
0
        internal Lazy <int> AddLazyCountOperation(ILazyOperation operation)
        {
            PendingLazyOperations.Add(operation);
            var lazyValue = new Lazy <int>(() =>
            {
                ExecuteAllPendingLazyOperations();
                return(operation.QueryResult.TotalResults);
            });

            return(lazyValue);
        }
Beispiel #3
0
        public ResponseTimeInformation ExecuteAllPendingLazyOperations()
        {
            var requests = new List <GetRequest>();

            for (int i = 0; i < PendingLazyOperations.Count; i++)
            {
                var req = PendingLazyOperations[i].CreateRequest(Context);
                if (req == null)
                {
                    PendingLazyOperations.RemoveAt(i);
                    i--; // so we'll recheck this index
                    continue;
                }
                requests.Add(req);
            }

            if (requests.Count == 0)
            {
                return(new ResponseTimeInformation());
            }

            try
            {
                var sw = Stopwatch.StartNew();

                IncrementRequestCount();

                var responseTimeDuration = new ResponseTimeInformation();

                while (ExecuteLazyOperationsSingleStep(responseTimeDuration, requests, sw))
                {
                    Thread.Sleep(100);
                }

                responseTimeDuration.ComputeServerTotal();


                foreach (var pendingLazyOperation in PendingLazyOperations)
                {
                    Action <object> value;
                    if (OnEvaluateLazy.TryGetValue(pendingLazyOperation, out value))
                    {
                        value(pendingLazyOperation.Result);
                    }
                }
                responseTimeDuration.TotalClientDuration = sw.Elapsed;
                return(responseTimeDuration);
            }
            finally
            {
                PendingLazyOperations.Clear();
            }
        }
Beispiel #4
0
        internal Lazy <Task <int> > AddLazyCountOperation(ILazyOperation operation, CancellationToken token = default(CancellationToken))
        {
            PendingLazyOperations.Add(operation);
            var lazyValue = new Lazy <Task <int> >(() => ExecuteAllPendingLazyOperationsAsync(token)
                                                   .ContinueWith(t =>
            {
                if (t.Exception != null)
                {
                    throw new InvalidOperationException("Could not perform lazy count", t.Exception);
                }
                return(operation.QueryResult.TotalResults);
            }, token));

            return(lazyValue);
        }
Beispiel #5
0
        internal Lazy <T> AddLazyOperation <T>(ILazyOperation operation, Action <T> onEval)
        {
            PendingLazyOperations.Add(operation);
            var lazyValue = new Lazy <T>(() =>
            {
                ExecuteAllPendingLazyOperations();
                return(GetOperationResult <T>(operation.Result));
            });

            if (onEval != null)
            {
                OnEvaluateLazy[operation] = theResult => onEval(GetOperationResult <T>(theResult));
            }

            return(lazyValue);
        }
Beispiel #6
0
        public async Task <ResponseTimeInformation> ExecuteAllPendingLazyOperationsAsync(CancellationToken token = default(CancellationToken))
        {
            if (PendingLazyOperations.Count == 0)
            {
                return(new ResponseTimeInformation());
            }

            try
            {
                var sw = Stopwatch.StartNew();

                IncrementRequestCount();

                var responseTimeDuration = new ResponseTimeInformation();

                while (await ExecuteLazyOperationsSingleStep(responseTimeDuration).WithCancellation(token).ConfigureAwait(false))
                {
                    await TimeoutManager.WaitFor(TimeSpan.FromMilliseconds(100), token).ConfigureAwait(false);
                }

                responseTimeDuration.ComputeServerTotal();


                foreach (var pendingLazyOperation in PendingLazyOperations)
                {
                    Action <object> value;
                    if (OnEvaluateLazy.TryGetValue(pendingLazyOperation, out value))
                    {
                        value(pendingLazyOperation.Result);
                    }
                }
                responseTimeDuration.TotalClientDuration = sw.Elapsed;
                return(responseTimeDuration);
            }
            finally
            {
                PendingLazyOperations.Clear();
            }
        }
Beispiel #7
0
        internal Lazy <Task <T> > AddLazyOperation <T>(ILazyOperation operation, Action <T> onEval, CancellationToken token = default(CancellationToken))
        {
            PendingLazyOperations.Add(operation);
            var lazyValue = new Lazy <Task <T> >(() =>
                                                 ExecuteAllPendingLazyOperationsAsync(token)
                                                 .ContinueWith(t =>
            {
                if (t.Exception != null)
                {
                    throw new InvalidOperationException("Could not perform add lazy operation", t.Exception);
                }

                return(GetOperationResult <T>(operation.Result));
            }, token));

            if (onEval != null)
            {
                OnEvaluateLazy[operation] = theResult => onEval(GetOperationResult <T>(theResult));
            }

            return(lazyValue);
        }
Beispiel #8
0
        private bool ExecuteLazyOperationsSingleStep(ResponseTimeInformation responseTimeInformation)
        {
            //WIP - Not final
            var requests          = PendingLazyOperations.Select(x => x.CreateRequest(Context)).ToList();
            var multiGetOperation = new MultiGetOperation(this);
            var multiGetCommand   = multiGetOperation.CreateRequest(requests);

            RequestExecutor.Execute(multiGetCommand, Context, sessionInfo: SessionInfo);
            var responses = multiGetCommand.Result;

            for (var i = 0; i < PendingLazyOperations.Count; i++)
            {
                long   totalTime;
                string tempReqTime;
                var    response = responses[i];

                response.Headers.TryGetValue(Constants.Headers.RequestTime, out tempReqTime);

                long.TryParse(tempReqTime, out totalTime);

                responseTimeInformation.DurationBreakdown.Add(new ResponseTimeItem
                {
                    Url      = requests[i].UrlAndQuery,
                    Duration = TimeSpan.FromMilliseconds(totalTime)
                });

                if (response.RequestHasErrors())
                {
                    throw new InvalidOperationException("Got an error from server, status code: " + (int)response.StatusCode + Environment.NewLine + response.Result);
                }

                PendingLazyOperations[i].HandleResponse(response);
                if (PendingLazyOperations[i].RequiresRetry)
                {
                    return(true);
                }
            }
            return(false);
        }
Beispiel #9
0
        public ResponseTimeInformation ExecuteAllPendingLazyOperations()
        {
            if (PendingLazyOperations.Count == 0)
            {
                return(new ResponseTimeInformation());
            }

            try
            {
                var sw = Stopwatch.StartNew();

                IncrementRequestCount();

                var responseTimeDuration = new ResponseTimeInformation();

                while (ExecuteLazyOperationsSingleStep(responseTimeDuration))
                {
                    Thread.Sleep(100);
                }

                responseTimeDuration.ComputeServerTotal();


                foreach (var pendingLazyOperation in PendingLazyOperations)
                {
                    Action <object> value;
                    if (OnEvaluateLazy.TryGetValue(pendingLazyOperation, out value))
                    {
                        value(pendingLazyOperation.Result);
                    }
                }
                responseTimeDuration.TotalClientDuration = sw.Elapsed;
                return(responseTimeDuration);
            }
            finally
            {
                PendingLazyOperations.Clear();
            }
        }