Пример #1
0
        public static async IAsyncEnumerable <T> AggregateEnumerables <T>(
            IEnumerable <IAsyncEnumerable <T> > enumerables,
            [EnumeratorCancellation] CancellationToken cancellationToken)
        {
            // TODO cancellation
            var options = new BoundedChannelOptions(1000)
            {
                SingleReader = true,
                SingleWriter = false
            };
            var channel      = Channel.CreateBounded <T>(options);
            var writter      = channel.Writer;
            var fillingTasks = enumerables.Select(
                r => Task.Run(async() =>
            {
                await foreach (var post in r)
                {
                    await writter.WriteAsync(post, cancellationToken);
                }
            }
                              ))
                               .ToList();

            await foreach (var p in channel.Reader.ReadAllAsync(cancellationToken))
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    yield break;
                }
                yield return(p);
            }
        }
Пример #2
0
        public ChannelExecutor(int prefetchCount, int concurrencyLimit)
        {
            if (prefetchCount <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(prefetchCount));
            }
            if (concurrencyLimit <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(concurrencyLimit));
            }

            var channelOptions = new BoundedChannelOptions(prefetchCount)
            {
                AllowSynchronousContinuations = true,
                FullMode     = BoundedChannelFullMode.Wait,
                SingleReader = concurrencyLimit == 1,
                SingleWriter = false
            };

            _channel = Channel.CreateBounded <IFuture>(channelOptions);

            _runTasks = new Task[concurrencyLimit];

            for (var i = 0; i < concurrencyLimit; i++)
            {
                _runTasks[i] = Task.Run(RunFromChannel);
            }
        }
        public BufferedChannel(BufferedChannelOptions options)
        {
            BoundedChannelOptions boundedChannelOptions = null;

            if (options != null)
            {
                _bufferSize           = options.BufferSize;
                _flushInterval        = options.FlushInterval;
                boundedChannelOptions = options.BoundedChannelOptions;
            }

            _channel = boundedChannelOptions != null
                ? System.Threading.Channels.Channel.CreateBounded <T>(boundedChannelOptions)
                : System.Threading.Channels.Channel.CreateUnbounded <T>();

            if (_bufferSize <= 0)
            {
                _bufferSize = BufferedChannelOptions.DefaultBufferSize;
            }

            if (_flushInterval <= TimeSpan.Zero)
            {
                _flushInterval = BufferedChannelOptions.DefaultFlushInterval;
            }

            Task.Factory.StartNew(StartChannelReader, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
            Task.Factory.StartNew(StartBufferFlushes, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
        }
Пример #4
0
        public static async Task Main()
        {
            try
            {
                Console.WriteLine($"API = {InferenceEngineLibrary.GetApiVersion()}");

                using var cts = new CancellationTokenSource();
                var ct = cts.Token;

                using var detector = new Detector(@"C:\Users\nickr\Documents\Intel\OpenVINO\openvino_models\intel\face-detection-0104\FP16\face-detection-0104.xml");
                DumpCoreInformation(detector.Core);

                var boundedOptions = new BoundedChannelOptions(MaxFrames)
                {
                    AllowSynchronousContinuations = false, SingleReader = true, SingleWriter = true
                };
                var emptyChannel     = Channel.CreateBounded <RawFrame>(boundedOptions);
                var populatedChannel = Channel.CreateBounded <RawFrame>(boundedOptions);

                var decoderThread = new Thread(() => DecoderThread(emptyChannel.Reader, populatedChannel.Writer, ct));
                decoderThread.Start();

                await detector.ProcessAsync(populatedChannel.Reader, emptyChannel.Writer, "GPU", ct);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex}");
            }
        }
Пример #5
0
    public KuduSession(
        KuduClient client,
        KuduSessionOptions options,
        ILoggerFactory loggerFactory,
        long txnId)
    {
        _client  = client;
        _options = options;
        _txnId   = txnId;
        _logger  = loggerFactory.CreateLogger <KuduSession>();

        var channelOptions = new BoundedChannelOptions(options.Capacity)
        {
            SingleWriter = false,
            SingleReader = true,
            FullMode     = BoundedChannelFullMode.Wait,
            AllowSynchronousContinuations = false
        };

        var channel = Channel.CreateBounded <KuduOperation>(channelOptions);

        _writer = channel.Writer;
        _reader = channel.Reader;

        _singleFlush = new SemaphoreSlim(1, 1);
        _flushCts    = new CancellationTokenSource();
        _flushTcs    = new TaskCompletionSource(
            TaskCreationOptions.RunContinuationsAsynchronously);

        _consumeTask = ConsumeAsync();
    }
Пример #6
0
        public async Task ConnectTest2()
        {
            var options = new BoundedChannelOptions(1)
            {
                AllowSynchronousContinuations = true,
            };
            var cp1 = ChannelPair.CreateTwisted(
                Channel.CreateBounded <int>(options),
                Channel.CreateBounded <int>(options));
            var cp2 = ChannelPair.CreateTwisted(
                Channel.CreateBounded <int>(options),
                Channel.CreateBounded <int>(options));
            var _ = cp1.Channel2.ConnectAsync(cp2.Channel1,
                                              m => {
                Out.WriteLine($"-> {m}");
                return(m);
            },
                                              m => {
                Out.WriteLine($"<- {m}");
                return(m);
            }
                                              );

            await PassThroughTest(cp1.Channel1, cp2.Channel2);
            await PassThroughTest(cp2.Channel2, cp1.Channel1);
        }
Пример #7
0
        public ChannelQueuing(BoundedChannelOptions options)
        {
            var channel = Channel.CreateBounded <T>(options);

            Producer = new Producer <T>(channel.Writer);
            Consumer = new Consumer <T>(channel.Reader);
        }
            public ReceiveTransportAgent(GrpcReceiveEndpointContext context, string queueName)
            {
                _context   = context;
                _queueName = queueName;

                _dispatcher = context.CreateReceivePipeDispatcher();

                var outputOptions = new BoundedChannelOptions(context.PrefetchCount)
                {
                    SingleWriter = true,
                    SingleReader = true,
                    AllowSynchronousContinuations = true
                };

                _channel = Channel.CreateBounded <GrpcTransportMessage>(outputOptions);

                if (context.ConcurrentMessageLimit.HasValue)
                {
                    _limiter = new ConcurrencyLimiter(context.ConcurrentMessageLimit.Value);
                }

                _consumeDispatcher = Task.Run(() => StartDispatcher());

                var startup = Task.Run(() => Startup());

                SetReady(startup);
            }
Пример #9
0
        public Task <object> CreateChannel(string type, int capacity, BoundedChannelOptions options)
        {
            var method = this.GetChannelMethod(Type.GetType(type), true, true);

            var channel = method.Invoke(method, new object[] { capacity, options });

            return(Task.FromResult(channel));
        }
        public BackgroundTaskQueue(int capacity)
        {
            var option = new BoundedChannelOptions(capacity)
            {
                FullMode = BoundedChannelFullMode.Wait
            };

            _queue = Channel.CreateBounded <Func <CancellationToken, ValueTask> >(option);
        }
Пример #11
0
        /// <summary>Creates a channel subject to the provided options.</summary>
        /// <typeparam name="T">Specifies the type of data in the channel.</typeparam>
        /// <param name="options">Options that guide the behavior of the channel.</param>
        /// <param name="itemDropped">Delegate that will be called when item is being dropped from channel. See <see cref="BoundedChannelFullMode"/>.</param>
        /// <returns>The created channel.</returns>
        public static Channel <T> CreateBounded <T>(BoundedChannelOptions options, Action <T>?itemDropped)
        {
            if (options is null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            return(new BoundedChannel <T>(options.Capacity, options.FullMode, !options.AllowSynchronousContinuations, itemDropped));
        }
Пример #12
0
 public BackgroundServiceQueue()
 {
     var options = new BoundedChannelOptions(3)
     {
         FullMode = BoundedChannelFullMode.Wait
     };
     
     _channel = Channel.CreateBounded<string>(options);
 }
        public EgressOperationQueue()
        {
            var options = new BoundedChannelOptions(256)
            {
                FullMode = BoundedChannelFullMode.Wait
            };

            _queue = Channel.CreateBounded <EgressRequest>(options);
        }
        public SubmissionToCheckQueue()
        {
            var options = new BoundedChannelOptions(100)
            {
                FullMode = BoundedChannelFullMode.Wait
            };

            _queue = Channel.CreateBounded <Submission>(options);
        }
Пример #15
0
        /// <summary>Creates a channel with the specified maximum capacity.</summary>
        /// <typeparam name="T">Specifies the type of data in the channel.</typeparam>
        /// <param name="options">Options that guide the behavior of the channel.</param>
        /// <returns>The created channel.</returns>
        public static Channel <T> CreateBounded <T>(BoundedChannelOptions options)
        {
            if (options == null)
            {
                throw new ArgumentOutOfRangeException(nameof(options));
            }

            return(new BoundedChannel <T>(options.Capacity, options.FullMode, !options.AllowSynchronousContinuations));
        }
        private void MultithreadedAnalyzeTargets(
            TOptions options,
            TContext rootContext,
            IEnumerable <Skimmer <TContext> > skimmers,
            ISet <string> disabledSkimmers)
        {
            options.Threads = options.Threads > 0 ?
                              options.Threads :
                              (Debugger.IsAttached) ? 1 : Environment.ProcessorCount;

            var channelOptions = new BoundedChannelOptions(2000)
            {
                SingleWriter = true,
                SingleReader = false,
            };

            _fileEnumerationChannel = Channel.CreateBounded <int>(channelOptions);

            channelOptions = new BoundedChannelOptions(2000)
            {
                SingleWriter = false,
                SingleReader = true,
            };
            _resultsWritingChannel = Channel.CreateBounded <int>(channelOptions);

            var sw = Stopwatch.StartNew();

            var workers = new Task <bool> [options.Threads];

            for (int i = 0; i < options.Threads; i++)
            {
                workers[i] = AnalyzeTargetAsync(skimmers, disabledSkimmers);
            }

            Task <bool> findFiles    = FindFilesAsync(options, rootContext);
            Task <bool> writeResults = WriteResultsAsync(rootContext);

            // FindFiles is single-thread and will close its write channel
            findFiles.Wait();

            Task.WhenAll(workers)
            .ContinueWith(_ => _resultsWritingChannel.Writer.Complete())
            .Wait();

            writeResults.Wait();

            Console.WriteLine();

            if (rootContext.Traces.HasFlag(DefaultTraces.ScanTime))
            {
                Console.WriteLine($"Done. {_fileContexts.Count:n0} files scanned in {sw.Elapsed}.");
            }
            else
            {
                Console.WriteLine($"Done. {_fileContexts.Count:n0} files scanned.");
            }
        }
Пример #17
0
        public IntimidatorIntimidationsCacheChannel()
        {
            var options = new BoundedChannelOptions(100)
            {
                SingleWriter = false,
                SingleReader = true
            };

            _IntimidatorIntimidationsSummeryChannel = Channel.CreateBounded <int>(options);
        }
Пример #18
0
        /// <inheritdoc cref="IChannelProvider"/>
        public Channel <T> ProvisionBoundedChannel <T>(BoundedChannelOptions boundedChannelOptions)
        {
            // This method and interface introduced to increase testability of the code.
            if (boundedChannelOptions == null)
            {
                throw new ArgumentNullException(nameof(boundedChannelOptions));
            }

            return(Channel.CreateBounded <T>(boundedChannelOptions));
        }
Пример #19
0
        public NotificationProcessChannel()
        {
            var options = new BoundedChannelOptions(maxMessagesInChannel)
            {
                SingleReader = true,
                SingleWriter = false
            };

            _channel = Channel.CreateBounded <PullRequestUpdate>(options);
        }
 /// <summary>
 /// Creates a new BoundedSingleConsumerChannelStrategy with a given capacity.
 /// </summary>
 /// <param name="bufferCapacity">Capacity of the queue.</param>
 public BoundedSingleConsumerChannelStrategy(int bufferCapacity)
 {
     options = new BoundedChannelOptions(bufferCapacity)
     {
         AllowSynchronousContinuations = false,
         FullMode     = BoundedChannelFullMode.DropOldest,
         SingleWriter = true,
         SingleReader = true
     };
 }
Пример #21
0
        public EmailSendingChannel()
        {
            var options = new BoundedChannelOptions(MaxMessagesInChannel)
            {
                SingleWriter = false,
                SingleReader = true
            };

            _channel = Channel.CreateBounded <EmailData>(options);
        }
Пример #22
0
        public static IChannelManager Create()
        {
            var boundedChannelOptions = new BoundedChannelOptions(5)
            {
                FullMode = BoundedChannelFullMode.Wait
            };
            var channel = Channel.CreateBounded <string>(boundedChannelOptions);

            return(new ChannelManager(channel, new ProducerManagerCreator(), new ConsumerManagerCreator()));
        }
Пример #23
0
        public BatchProcessingChannel()
        {
            var options = new BoundedChannelOptions(MaxMessagesInChannel)
            {
                SingleWriter = false,
                SingleReader = true
            };

            _channel = Channel.CreateBounded <Guid>(options);
        }
Пример #24
0
        public MessageChannel()
        {
            var options = new BoundedChannelOptions(_capacity)
            {
                SingleReader = true,
                SingleWriter = true
            };

            _channel = Channel.CreateBounded <Message>(options);
        }
Пример #25
0
        static EventDispatchChannel()
        {
            var options = new BoundedChannelOptions(int.MaxValue)
            {
                SingleWriter = false,
                SingleReader = true
            };

            _channel = Channel.CreateBounded <T>(options);
        }
Пример #26
0
        public ChannelBlock(BoundedChannelOptions options, Func <TOut, TOut> optionalfirstStep = null)
        {
            Guard.AgainstNull(options, nameof(options));
            _logger  = LogHelper.LoggerFactory.CreateLogger <ChannelBlock <TOut> >();
            _channel = Channel.CreateBounded <TOut>(options);

            _targetBlock           = new TransformBlock <TOut, TOut>(optionalfirstStep ?? ((input) => input));
            _sourceFromTargetBlock = (ISourceBlock <TOut>)_targetBlock;
            Completion             = _targetBlock.Completion;
        }
Пример #27
0
        public BoundedMessageChannel(ILogger <BoundedMessageChannel <BusMessage> > logger)
        {
            _logger = logger;
            var options = new BoundedChannelOptions(MaxMessagesInChannel)
            {
                SingleReader = false,
                SingleWriter = true,
            };

            _channel = Channel.CreateBounded <ConsumeResult <Ignore, BusMessage> >(options);
        }
Пример #28
0
        private ChannelHelper()
        {
            var channelOptions = new BoundedChannelOptions(1000)
            {
                FullMode = BoundedChannelFullMode.DropOldest
            };
            var channel = Channel.CreateBounded <TModel>(channelOptions);

            _writer = channel.Writer;
            _reader = channel.Reader;
        }
Пример #29
0
        public FileProcessingChannel(ILogger <FileProcessingChannel> logger)
        {
            var options = new BoundedChannelOptions(MaxMessagesInChannel)
            {
                SingleWriter = false,
                SingleReader = true
            };

            _channel = Channel.CreateBounded <string>(options);
            _logger  = logger;
        }
Пример #30
0
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="capacity">存储器最多能够处理多少消息,超过该容量进入等待写入</param>
        public ChannelEventSourceStorer(int capacity)
        {
            // 配置通道,设置超出默认容量后进入等待
            var boundedChannelOptions = new BoundedChannelOptions(capacity)
            {
                FullMode = BoundedChannelFullMode.Wait
            };

            // 创建有限容量通道
            _channel = Channel.CreateBounded <IEventSource>(boundedChannelOptions);
        }