Ejemplo n.º 1
0
        private async Task ProcessBacklog()
        {
            while (true)
            {
                if (_disposed)
                {
                    return;
                }

                try
                {
                    if (!IsConnected)
                    {
                        await TryConnectAsync(CancellationToken.None).ConfigureAwait(false);
                    }

                    var backlogItems = _backlog.DequeueAll();

                    // Items cancelled will be taken care of by the CancellationToken.Register in the SendOrQueue method
                    var validItems = backlogItems.Where(item => !item.CancellationToken.IsCancellationRequested)
                                     .ToList();

                    var pipelinedCommand = validItems
                                           .Select(backlogItem => backlogItem.RedisCommand).ToList()
                                           .ToPipelinedCommand();

                    try
                    {
                        await Write(pipelinedCommand);

                        foreach (var backlogItem in validItems)
                        {
                            await backlogItem.SetResult();
                        }
                    }
                    catch (Exception e)
                    {
                        foreach (var validItem in validItems)
                        {
                            validItem.SetException(e);
                        }

                        DisposeNetwork();
                    }
                }
                catch
                {
                    DisposeNetwork();
                }
            }
        }
Ejemplo n.º 2
0
        protected IEnumerator <object> SendTask()
        {
            var sleep = new Sleep(SendInterval);

            Dictionary <string, object> prefs = new Dictionary <string, object>();

            yield return(Preferences.GetAll().Bind(() => prefs));

            List <string> allItems = new List <string>();

            var oldQueue = Queue;

            Queue = new BlockingQueue <string>();
            if (oldQueue != null)
            {
                Queue.EnqueueMultiple(oldQueue.DequeueAll());
            }

            while (true)
            {
                var nextItem = Queue.Dequeue();

                using (nextItem)
                    yield return(nextItem);

                yield return(sleep);

                allItems.Clear();
                allItems.Add(nextItem.Result);
                allItems.AddRange(Queue.DequeueAll());

                yield return(new Start(
                                 Send(prefs, allItems.ToArray()), TaskExecutionPolicy.RunAsBackgroundTask
                                 ));
            }
        }
        protected IEnumerator<object> SendTask()
        {
            var sleep = new Sleep(SendInterval);

            Dictionary<string, object> prefs = new Dictionary<string, object>();
            yield return Preferences.GetAll().Bind(() => prefs);

            List<string> allItems = new List<string>();

            var oldQueue = Queue;
            Queue = new BlockingQueue<string>();
            if (oldQueue != null)
                Queue.EnqueueMultiple(oldQueue.DequeueAll());

            while (true) {
                var nextItem = Queue.Dequeue();

                using (nextItem)
                    yield return nextItem;

                yield return sleep;

                allItems.Clear();
                allItems.Add(nextItem.Result);
                allItems.AddRange(Queue.DequeueAll());

                yield return new Start(
                    Send(prefs, allItems.ToArray()), TaskExecutionPolicy.RunAsBackgroundTask
                );
            }
        }