Beispiel #1
0
        private async Task RelockingLoop(CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                try
                {
                    var t = DateTime.UtcNow;
                    if ((t - lastRelock).TotalSeconds > 5)
                    {
                        reader.Relock();
                        lastRelock = t;
                    }

                    try
                    {
                        await Task.Delay(1000, cancellationToken).ConfigureAwait(false);
                    }
                    catch (TaskCanceledException)
                    {
                    }
                }
                catch (Exception ex)
                {
                    var eventArgs = new ExceptionThrownEventArgs(ex, ExceptionSite.RelockingLoop);
                    OnExceptionThrown(eventArgs);
                    if (eventArgs.Stop)
                    {
                        Task.Run(() => Stop());
                        break;
                    }

                    try
                    {
                        await Task.Delay(1000, cancellationToken).ConfigureAwait(false);
                    }
                    catch (TaskCanceledException)
                    {
                    }
                }
            }

            EndTask.Start();
        }
Beispiel #2
0
        private async Task ReceivingLoop(Func <Message[], Task> handler, CancellationToken cancellationToken)
        {
            int       delay = options.MinDelayMilliseconds;
            Stopwatch sw    = new Stopwatch();

            while (!cancellationToken.IsCancellationRequested)
            {
                try
                {
                    if (needAfterException)
                    {
                        AfterException();
                        needAfterException = false;
                    }

                    var messages = reader.Read(options.NumPerReed);

                    if (messages?.Length > 0)
                    {
                        sw.Start();

                        try
                        {
                            await handler(messages).ConfigureAwait(false);

                            reader.Complete();
                        }
                        catch
                        {
                            needAfterException = true;
                            AfterException();
                            needAfterException = false;
                        }

                        sw.Stop();

                        if (messages.Length > 1)
                        {
                            delay = delay / messages.Length;

                            if (delay < options.MinDelayMilliseconds)
                            {
                                delay = options.MinDelayMilliseconds;
                            }
                        }
                    }
                    else
                    {
                        delay = delay * 2;

                        if (delay > options.MaxDelayMilliseconds)
                        {
                            delay = options.MaxDelayMilliseconds;
                        }
                    }

                    var actualDelay = delay - (int)sw.ElapsedMilliseconds;
                    sw.Reset();

                    if (actualDelay > 0)
                    {
                        try
                        {
                            await Task.Delay(actualDelay, cancellationToken).ConfigureAwait(false);
                        }
                        catch (TaskCanceledException)
                        {
                        }
                    }
                }
                catch (Exception ex)
                {
                    var eventArgs = new ExceptionThrownEventArgs(ex, ExceptionSite.ReceivingLoop);
                    OnExceptionThrown(eventArgs);
                    if (eventArgs.Stop)
                    {
                        Task.Run(() => Stop());
                        break;
                    }

                    try
                    {
                        await Task.Delay(delay, cancellationToken).ConfigureAwait(false);
                    }
                    catch (TaskCanceledException)
                    {
                    }
                }
            }

            relockingLoopCTS.Cancel();
        }
Beispiel #3
0
        private void OnExceptionThrown(ExceptionThrownEventArgs eventArgs)
        {
            var handler = ExceptionThrown;

            handler?.Invoke(this, eventArgs);
        }