コード例 #1
0
ファイル: CounterMap.cs プロジェクト: rvajustin/notifo
        public static CounterMap ForChannel(string channel, ProcessStatus status, int count = 1)
        {
            var result = new CounterMap();

            switch (status)
            {
            case ProcessStatus.Attempt when SupportPending:
                result.Increment(ChannelAttmept(channel), count);
                break;

            case ProcessStatus.Skipped:
                result.Increment(ChannelSkipped(channel), count);
                break;

            case ProcessStatus.Handled:
                result.Increment(ChannelHandled(channel), count);
                break;

            case ProcessStatus.Failed:
                result.Increment(ChannelFailed(channel), count);
                break;
            }

            return(result);
        }
コード例 #2
0
        private async Task Process()
        {
            try
            {
                await foreach (var batch in writeQueue.Reader.ReadAllAsync())
                {
                    if (batch.Length == 0)
                    {
                        continue;
                    }

                    try
                    {
                        var commands = new List <(T, CounterMap)>();

                        foreach (var group in batch.GroupBy(x => x.Key))
                        {
                            if (group.Count() == 1)
                            {
                                commands.Add((group.Key, group.First().Counters));
                            }
                            else
                            {
                                var merged = new CounterMap();

                                foreach (var item in group)
                                {
                                    foreach (var(key, value) in item.Counters)
                                    {
                                        merged.Increment(key, value);
                                    }
                                }

                                commands.Add((group.Key, merged));
                            }
                        }

                        if (commands.Count > 0)
                        {
                            await store.BatchWriteAsync(commands, default);
                        }
                    }
                    catch (Exception ex)
                    {
                        log.LogError(ex, "Failed to writer counters.");
                    }
                }
            }
            catch (OperationCanceledException)
            {
                return;
            }
        }
コード例 #3
0
ファイル: CounterCollector.cs プロジェクト: rvajustin/notifo
        public ValueTask AddAsync(T group, CounterMap newCounters)
        {
            readerWriterLock.EnterReadLock();
            try
            {
                counters.AddOrUpdate(group, newCounters, (k, c) => c.IncrementWithLock(newCounters));
            }
            finally
            {
                readerWriterLock.ExitReadLock();
            }

            if (counters.Count >= maxSize)
            {
                timer.SkipCurrentDelay();
            }

            return(default);
コード例 #4
0
ファイル: CounterMap.cs プロジェクト: rvajustin/notifo
        public static CounterMap ForNotification(ProcessStatus status, int count = 1)
        {
            var result = new CounterMap();

            switch (status)
            {
            case ProcessStatus.Attempt when SupportPending:
                result.Increment(NotificationsAttempt, count);
                break;

            case ProcessStatus.Handled:
                result.Increment(NotificationsHandled, count);
                break;

            case ProcessStatus.Failed:
                result.Increment(NotificationsFailed, count);
                break;
            }

            return(result);
        }
コード例 #5
0
ファイル: CounterMap.cs プロジェクト: rvajustin/notifo
        public CounterMap IncrementWithLock(CounterMap counters)
        {
            var taken = false;

            slimLock.Enter(ref taken);
            try
            {
                foreach (var(key, value) in counters)
                {
                    Increment(key, value);
                }
            }
            finally
            {
                if (taken)
                {
                    slimLock.Exit();
                }
            }

            return(this);
        }
コード例 #6
0
ファイル: CounterService.cs プロジェクト: djhome50/notifo
 public Task CollectAsync(CounterKey key, CounterMap counters, CancellationToken ct)
 {
     return(Task.WhenAll(targets.Select(x => x.CollectAsync(key, counters, ct))));
 }
コード例 #7
0
ファイル: CounterMap.cs プロジェクト: rvajustin/notifo
 public CounterMap(CounterMap source)
     : base(source)
 {
 }
コード例 #8
0
 public ValueTask AddAsync(T key, CounterMap counters,
                           CancellationToken ct = default)
 {
     return(inputQueue.Writer.WriteAsync(new Job(key, counters), ct));
 }
コード例 #9
0
 sealed record Job(T Key, CounterMap Counters);