コード例 #1
0
        public static IMessagePipeline <TSource, TDestination> Action <TSource, TDestination>(
            this IMessagePipeline <TSource, TDestination> pipeline,
            ILogger log,
            params Func <TDestination, Task>[] handlers)
        {
            return(pipeline.Block(new TransformBlock <TDestination, TDestination>(async x =>
            {
                foreach (var handler in handlers)
                {
                    try
                    {
                        await handler(x);
                    }
                    catch (Exception e)
                    {
                        log?.LogError(e, "Message handler failure");
                    }
                }

                return x;
            },
                                                                                  new ExecutionDataflowBlockOptions
            {
                BoundedCapacity = 1,
                EnsureOrdered = true
            })));
        }
コード例 #2
0
        public static IMessagePipeline <IMessage <T>, IMessageEnumerable <T> > Batch <T>(
            this IMessagePipeline <IMessage <T>, IMessage <T> > pipeline,
            int size,
            TimeSpan time)
        {
            if (size <= 1)
            {
                throw new ArgumentException("Buffer size must be greater that 1");
            }

            var batch = new BatchBlock <IMessage <T> >(size, new GroupingDataflowBlockOptions
            {
                EnsureOrdered   = true,
                BoundedCapacity = size
            });

            var timer = time.TotalMilliseconds > 0
                ? new Timer(_ => batch.TriggerBatch(), null, (int)time.TotalMilliseconds, Timeout.Infinite)
                : null;

            var transform = new TransformBlock <IMessage <T>[], IMessageEnumerable <T> >(x =>
            {
                timer?.Change((int)time.TotalMilliseconds, Timeout.Infinite);
                return(new KafkaMessageEnumerable <T>(x));
            },
                                                                                         new ExecutionDataflowBlockOptions
            {
                EnsureOrdered   = true,
                BoundedCapacity = 1
            });

            batch.LinkTo(transform);

            return(pipeline.Block(DataflowBlock.Encapsulate(batch, transform)));
        }
コード例 #3
0
 public static IMessagePipeline <TSource, IMessageOffset> Commit <TSource>(
     this IMessagePipeline <TSource, IMessageOffset> pipeline)
 {
     return(pipeline.Block(new TransformBlock <IMessageOffset, IMessageOffset>(x =>
     {
         x.Commit(true);
         return x;
     },
                                                                               new ExecutionDataflowBlockOptions
     {
         BoundedCapacity = 1,
         EnsureOrdered = true
     })));
 }
コード例 #4
0
        public static IMessagePipeline <TSource, TDestination> Buffer <TSource, TDestination>(
            this IMessagePipeline <TSource, TDestination> pipeline,
            int size)
        {
            if (size <= 1)
            {
                throw new ArgumentException("Buffer size must be greater that 1");
            }

            return(pipeline.Block(new TransformBlock <TDestination, TDestination>(x => x, new ExecutionDataflowBlockOptions
            {
                BoundedCapacity = size,
                EnsureOrdered = true
            })));
        }