예제 #1
0
        public void ChannelOptimizations_Properties_Roundtrip()
        {
            var co = new UnboundedChannelOptions();

            Assert.False(co.SingleReader);
            Assert.False(co.SingleWriter);

            co.SingleReader = true;
            Assert.True(co.SingleReader);
            Assert.False(co.SingleWriter);
            co.SingleReader = false;
            Assert.False(co.SingleReader);

            co.SingleWriter = true;
            Assert.False(co.SingleReader);
            Assert.True(co.SingleWriter);
            co.SingleWriter = false;
            Assert.False(co.SingleWriter);

            co.SingleReader = true;
            co.SingleWriter = true;
            Assert.True(co.SingleReader);
            Assert.True(co.SingleWriter);

            Assert.False(co.AllowSynchronousContinuations);
            co.AllowSynchronousContinuations = true;
            Assert.True(co.AllowSynchronousContinuations);
            co.AllowSynchronousContinuations = false;
            Assert.False(co.AllowSynchronousContinuations);
        }
예제 #2
0
        private static async Task Listen(EndPoint endPoint, int bufferSize)
        {
            var listener = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            listener.Bind(endPoint);
            listener.Listen(100);
            Logger.Info("listening on {}", endPoint);

            var channelOptions = new UnboundedChannelOptions {
                SingleReader = true, SingleWriter = true
            };

            while (true)
            {
                var socket = await listener.AcceptAsync();

                var channel = Channel.CreateUnbounded <double>(channelOptions);
                ReadFromClient(socket, channel.Writer, bufferSize).FireAndForget(
                    e => Logger.Warn("read from client error: {}", new[] { e })
                    );
                WriteToClient(socket, channel.Reader).FireAndForget(
                    e => Logger.Warn("write to client error: {}", new[] { e })
                    );
            }
        }
예제 #3
0
        // [Benchmark]
        public void ThreadingChannel()
        {
            var opt2 = new UnboundedChannelOptions();

            opt2.AllowSynchronousContinuations = false;
            var channel = Channel.CreateUnbounded <int>(opt2);

            Task.WaitAll(
                Task.WhenAll(Enumerable.Range(0, WTaskNum).Select(async(idx) =>
            {
                await Task.Yield();
                for (int i = 0; i < LoopNum / WTaskNum; i++)
                {
                    channel.Writer.TryWrite(i);
                }
            })).ContinueWith(t => channel.Writer.TryComplete())
                ,
                Task.WhenAll(Enumerable.Range(0, TaskNum).Select(async(idx) =>
            {
                await Task.Yield();
                while (await channel.Reader.WaitToReadAsync().ConfigureAwait(false))
                {
                    await Task.Yield();
                    while (channel.Reader.TryRead(out var item))
                    {
                    }
                }
            }))
                );
        }
예제 #4
0
        public void SettingBench()
        {
            var opts = new UnboundedChannelOptions();

            opts.SingleReader = SingleReader;
            opts.SingleWriter = SingleWriter;
            var channel = Channel.CreateUnbounded <int>(opts);

            Task.WaitAll(
                Task.Run(async() =>
            {
                await Task.Yield();
                for (int i = 0; i < LoopNum; i++)
                {
                    channel.Writer.TryWrite(i);
                }
            }).ContinueWith(t => channel.Writer.TryComplete()),
                Task.Run(async() =>
            {
                await Task.Yield();
                while (await channel.Reader.WaitToReadAsync().ConfigureAwait(false))
                {
                    while (channel.Reader.TryRead(out var item))
                    {
                        ;
                    }
                }
            })
                );
        }
예제 #5
0
        public void AllowAsyncBench()
        {
            var opts = new UnboundedChannelOptions();

            opts.AllowSynchronousContinuations = AllowAsync;
            var channel = Channel.CreateUnbounded <int>(opts);
            {
                Task.WaitAll(
                    Task.WhenAll(Enumerable.Range(0, WriteTaskNum).Select(async(idx) =>
                {
                    for (int i = 0; i < LoopNum / WriteTaskNum; i++)
                    {
                        await Task.Yield();
                        channel.Writer.TryWrite(i);
                    }
                    return(0);
                })).ContinueWith(t => channel.Writer.TryComplete())
                    ,
                    Task.WhenAll(Enumerable.Range(0, ReadTaskNum).Select(async(idx) =>
                {
                    while (await channel.Reader.WaitToReadAsync().ConfigureAwait(false))
                    {
                        while (channel.Reader.TryRead(out var item))
                        {
                            await Task.Yield();
                        }
                    }
                }))
                    );
            }
        }
예제 #6
0
        public void ChannelBench()
        {
            var opt2 = new UnboundedChannelOptions();

            opt2.AllowSynchronousContinuations = false;
            opt2.SingleReader = IsSingleReader;
            var channel = Channel.CreateUnbounded <int>(opt2);

            Task.WaitAll(
                Task.WhenAll(Enumerable.Range(0, TaskNum).Select(async idx =>
            {
                await channel.Writer.WaitToWriteAsync();
                for (int i = 0; i < LoopNum / TaskNum; i++)
                {
                    channel.Writer.TryWrite(1);
                }
            })).ContinueWith(t => channel.Writer.Complete())
                ,
                Task.Run(() =>
            {
                for (int i = 0; i < LoopNum; i++)
                {
                    while (!channel.Reader.TryRead(out var item))
                    {
                    }
                }
            })
                );
        }
예제 #7
0
        public Subscription(uint extension)
        {
            var options = new UnboundedChannelOptions();

            options.SingleReader = true;
            Queue     = Channel.CreateUnbounded <NotificationMessage>(options);
            Extension = extension;
        }
예제 #8
0
        public ChannelQueue(int capacity = int.MaxValue)
        {
            _capacity = capacity;
            var options = new UnboundedChannelOptions();

            _channel = Channel.CreateUnbounded <T>(options);
            _items   = 0;
        }
예제 #9
0
        public Task <object> CreateChannel(string type, UnboundedChannelOptions options)
        {
            var method = this.GetChannelMethod(Type.GetType(type), false, true);

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

            return(Task.FromResult(channel));
        }
 /// <summary>
 /// Creates a new UnboundedSingleConsumerChannelStrategy.
 /// </summary>
 public UnboundedSingleConsumerChannelStrategy()
 {
     options = new UnboundedChannelOptions()
     {
         AllowSynchronousContinuations = false,
         SingleWriter = false,
         SingleReader = true
     };
 }
예제 #11
0
        public ChannelBlock(UnboundedChannelOptions options, Func <TOut, TOut> optionalfirstStep = null)
        {
            Guard.AgainstNull(options, nameof(options));
            _logger  = LogHelper.LoggerFactory.CreateLogger <ChannelBlock <TOut> >();
            _channel = Channel.CreateUnbounded <TOut>(options);

            _targetBlock           = new TransformBlock <TOut, TOut>(optionalfirstStep ?? ((input) => input));
            _sourceFromTargetBlock = (ISourceBlock <TOut>)_targetBlock;
            Completion             = _targetBlock.Completion;
        }
예제 #12
0
    public async Task Run()
    {
        var unboundedChannelOptions = new UnboundedChannelOptions();

        unboundedChannelOptions.SingleWriter = true;

        var channel       = Channel.CreateUnbounded <int>(unboundedChannelOptions);
        var channelWriter = channel.Writer;

        await Task.WhenAll(Task.Run(() => Produce(channelWriter, 1000)), Task.Run(() => Read(channel.Reader, "R1")), Task.Run(() => Read(channel.Reader, "R2")));
    }
예제 #13
0
        public DejarixLoggerProvider()
        {
            var options = new UnboundedChannelOptions
            {
                SingleReader = true,
                SingleWriter = false,
                AllowSynchronousContinuations = false
            };

            _channel = Channel.CreateUnbounded <string>(options);
        }
예제 #14
0
        public LogMessageChannel()
        {
            var options = new UnboundedChannelOptions
            {
                SingleReader = true,
                SingleWriter = false
            };

            _channel = Channel.CreateUnbounded <LogMessageContext>(options);
            _channel.Reader.Completion.ContinueWith(_ => Completed = true);
        }
예제 #15
0
        public static (ChannelCommunicator client, ChannelCommunicator server) Create()
        {
            var options = new UnboundedChannelOptions
            {
                SingleReader = true,
                SingleWriter = true,
            };
            var upstream   = Channel.CreateUnbounded <object>(options);
            var downstream = Channel.CreateUnbounded <object>(options);

            return(new ChannelCommunicator(downstream.Reader, upstream.Writer), new ChannelCommunicator(upstream.Reader, downstream.Writer));
        }
예제 #16
0
        /// <summary>
        /// Inits the specified source.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="loggerName">Name of the logger.</param>
        /// <param name="configXml">The config XML.</param>
        public void Init(IGenericCommunicationService source, string loggerName, XElement configXml)
        {
            this.source        = source;
            this.log           = source.Logger;
            this.name          = loggerName;
            this.messageFormat = source.Serializer.MessageFormat;

            if (dataStreamQueue == null)
            {
                UnboundedChannelOptions channelOptions = new UnboundedChannelOptions();
                channelOptions.SingleReader = true;
                channelOptions.SingleWriter = false;

                dataStreamQueue = Channel.CreateUnbounded <StreamLogItem>(channelOptions);
                queueWriter     = dataStreamQueue.Writer;
            }
            else
            {
                throw new InvalidOperationException("Logger \"" + name + "\" already initialized!");
            }

            // load config
            XElement targetDirElement;

            if (configXml != null &&
                (targetDirElement = configXml.Element("TargetDir")) != null)
            {
                this.TargetDir = targetDirElement.Value;
            }
            else
            {
                if (!string.IsNullOrEmpty(this.TargetDir))
                {
                    this.TargetDir = Path.GetFullPath(this.TargetDir);
                }
                else
                {
                    this.TargetDir = Environment.CurrentDirectory;
                }
            }

            XElement keepStreamLogsDaysElement;

            if (configXml != null &&
                (keepStreamLogsDaysElement = configXml.Element("KeepStreamLogsDays")) != null)
            {
                keepStreamLogsDays = int.Parse(keepStreamLogsDaysElement.Value);
            }

            // start logging
            Task.Run(StoreLogItemsThread);
        }
예제 #17
0
 public ChannelBlockEngine(
     Func <TIn, Task <TOut> > workBodyAsync,
     int maxDegreeOfParallelism,
     bool ensureOrdered,
     UnboundedChannelOptions options,
     TaskScheduler taskScheduler = null,
     CancellationToken token     = default) :
     this(workBodyAsync, maxDegreeOfParallelism, ensureOrdered, taskScheduler)
 {
     _channelBlock = new ChannelBlock <TIn>(options);
     _channelBlock.LinkTo(_workBlock);
     _channelBlock.StartReadChannel(token);
 }
        internal static IServiceCollection RegisterOutputChannel(this IServiceCollection services)
        {
            return(services.AddSingleton <FunctionsHostOutputChannel>(s =>
            {
                UnboundedChannelOptions outputOptions = new UnboundedChannelOptions
                {
                    SingleWriter = false,
                    SingleReader = true,
                    AllowSynchronousContinuations = true
                };

                return new FunctionsHostOutputChannel(System.Threading.Channels.Channel.CreateUnbounded <StreamingMessage>(outputOptions));
            }));
        }
예제 #19
0
파일: Channel.cs 프로젝트: naricc/runtime
        /// <summary>Creates an unbounded 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>
        /// <returns>The created channel.</returns>
        public static Channel <T> CreateUnbounded <T>(UnboundedChannelOptions options)
        {
            if (options is null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (options.SingleReader)
            {
                return(new SingleConsumerUnboundedChannel <T>(!options.AllowSynchronousContinuations));
            }

            return(new UnboundedChannel <T>(!options.AllowSynchronousContinuations));
        }
예제 #20
0
        public void ThreadingChannelWriteOnly()
        {
            var opt2 = new UnboundedChannelOptions();

            opt2.AllowSynchronousContinuations = false;
            var channel = Channel.CreateUnbounded <int>(opt2);

            Task.WhenAll(Enumerable.Range(0, WTaskNum).Select(async(idx) =>
            {
                await Task.Yield();
                for (int i = 0; i < LoopNum / WTaskNum; i++)
                {
                    channel.Writer.TryWrite(i);
                }
            })).ContinueWith(t => channel.Writer.TryComplete()).Wait();
        }
예제 #21
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RuntimeHandler"/> class.
        /// </summary>
        /// <param name="options">The test server's options.</param>
        /// <param name="cancellationToken">The cancellation token that is signalled when request listening should stop.</param>
        internal RuntimeHandler(LambdaTestServerOptions options, CancellationToken cancellationToken)
        {
            _cancellationToken = cancellationToken;
            _options           = options;

            // Support multi-threaded access to the request queue, although the default
            // usage scenario would be a single reader and writer from a test method.
            var channelOptions = new UnboundedChannelOptions()
            {
                SingleReader = false,
                SingleWriter = false,
            };

            _requests  = Channel.CreateUnbounded <LambdaTestRequest>(channelOptions);
            _responses = new ConcurrentDictionary <string, Channel <LambdaTestResponse> >(StringComparer.Ordinal);
        }
        public JobProgressUpdater(
            IJobStore jobStore,
            Job job,
            ILogger <JobProgressUpdater> logger)
        {
            _jobStore = EnsureArg.IsNotNull(jobStore, nameof(jobStore));
            _job      = EnsureArg.IsNotNull(job, nameof(job));
            _logger   = EnsureArg.IsNotNull(logger, nameof(logger));
            var channelOptions = new UnboundedChannelOptions
            {
                SingleReader = true,
                SingleWriter = false,
            };

            _progressChannel = Channel.CreateUnbounded <TaskContext>(channelOptions);
        }
예제 #23
0
        public void Setup()
        {
            var options = new UnboundedChannelOptions();

            _producer  = new Producer <int>();
            _consumers = new List <Consumer <int> >();
            for (int i = 0; i < Consumers; ++i)
            {
                _consumers.Add(new Consumer <int>());
            }

            foreach (var consumer in _consumers)
            {
                _producer.LinkTo(consumer, options);
            }
        }
예제 #24
0
        public void ThreadingChannel()
        {
            var opt2 = new UnboundedChannelOptions();

            opt2.AllowSynchronousContinuations = false;
            opt2.SingleReader = true;
            var channel = Channel.CreateUnbounded <int>(opt2);

            for (int i = 0; i < LoopNum; i++)
            {
                channel.Writer.TryWrite(i);
            }
            for (int i = 0; i < LoopNum; i++)
            {
                channel.Reader.TryRead(out var x);
            }
        }
        /// <summary>
        /// Creates a new UnboundedMultiConsumerChannelStrategy.
        /// </summary>
        /// <param name="maxChannelConsumers">Maximum number of consumers allowed or null if unbound.</param>
        public UnboundedMultiConsumerChannelStrategy(uint?maxChannelConsumers)
        {
            offeringOptions = new UnboundedChannelOptions()
            {
                AllowSynchronousContinuations = false,
                SingleWriter = true,
                SingleReader = true
            };

            receivingOptions = new UnboundedChannelOptions()
            {
                AllowSynchronousContinuations = false,
                SingleWriter = false,
                SingleReader = true
            };

            StartReceiving(maxChannelConsumers);
        }
예제 #26
0
        public ChannelExecutor(int concurrencyLimit)
        {
            var channelOptions = new UnboundedChannelOptions
            {
                AllowSynchronousContinuations = true,
                SingleReader = concurrencyLimit == 1,
                SingleWriter = false
            };

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

            _runTasks = new Task[concurrencyLimit];

            for (var i = 0; i < concurrencyLimit; i++)
            {
                _runTasks[i] = Task.Run(RunFromChannel);
            }
        }
예제 #27
0
        public GrpcNode(IMessageFabric messageFabric, NodeContext context)
        {
            _messageFabric = messageFabric;
            _context       = context;

            _remoteTopology = new RemoteNodeTopology(this, messageFabric);

            var outputOptions = new UnboundedChannelOptions
            {
                SingleWriter = false,
                SingleReader = true,
                AllowSynchronousContinuations = true
            };

            _channel = System.Threading.Channels.Channel.CreateUnbounded <TransportMessage>(outputOptions);

            Writer = _channel.Writer;
        }
예제 #28
0
        public ChannelExecutor(int concurrencyLimit, bool allowSynchronousContinuations = true)
        {
            _concurrencyLimit = concurrencyLimit;

            var channelOptions = new UnboundedChannelOptions
            {
                AllowSynchronousContinuations = allowSynchronousContinuations,
                SingleReader = true,
                SingleWriter = false
            };

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

            _syncLock = new object();
            _limit    = new SemaphoreSlim(concurrencyLimit);

            _readerTask = Task.Run(() => ReadFromChannel());
        }
예제 #29
0
        public ChannelTaskScheduler(ExtendedActorSystem system)
        {
            //config channel-scheduler
            var config = system.Settings.Config.GetConfig("akka.channel-scheduler");

            _maximumConcurrencyLevel = ThreadPoolConfig.ScaledPoolSize(
                config.GetInt("parallelism-min"),
                config.GetDouble("parallelism-factor", 1.0D),         // the scalar-based factor to scale the threadpool size to
                config.GetInt("parallelism-max"));
            _maximumConcurrencyLevel = Math.Max(_maximumConcurrencyLevel, 1);
            _maxWork = Math.Max(config.GetInt("work-max", _maxWork), 3); //min 3 normal work in work-loop

            _workInterval = config.GetInt("work-interval", _workInterval);
            _workStep     = config.GetInt("work-step", _workStep);

            //create task schedulers
            var channelOptions = new UnboundedChannelOptions()
            {
                AllowSynchronousContinuations = true,
                SingleReader = _maximumConcurrencyLevel == 1,
                SingleWriter = false
            };

            _highScheduler   = new PriorityTaskScheduler(Channel.CreateUnbounded <Task>(channelOptions), TaskSchedulerPriority.AboveNormal);
            _normalScheduler = new PriorityTaskScheduler(Channel.CreateUnbounded <Task>(channelOptions), TaskSchedulerPriority.Normal);
            _lowScheduler    = new PriorityTaskScheduler(Channel.CreateUnbounded <Task>(channelOptions), TaskSchedulerPriority.Low);
            _idleScheduler   = new PriorityTaskScheduler(Channel.CreateUnbounded <Task>(channelOptions), TaskSchedulerPriority.Idle);

            //prefill coworker array
            _coworkers = new Task[_maximumConcurrencyLevel - 1];
            for (var i = 0; i < _coworkers.Length; i++)
            {
                _coworkers[i] = Task.CompletedTask;
            }

            //init paused timer
            _timer = new Timer(ScheduleCoWorkers, "timer", Timeout.Infinite, Timeout.Infinite);

            //start main worker
            _controlTask = Task.Factory.StartNew(ControlAsync, _cts.Token,
                                                 TaskCreationOptions.DenyChildAttach | TaskCreationOptions.LongRunning,
                                                 TaskScheduler.Default).Unwrap();
        }
예제 #30
0
        public ChannelExecutor(int concurrencyLimit, bool allowSynchronousContinuations)
        {
            var channelOptions = new UnboundedChannelOptions
            {
                AllowSynchronousContinuations = allowSynchronousContinuations,
                SingleReader = concurrencyLimit == 1,
                SingleWriter = false
            };

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

            _runTasks = new Task[concurrencyLimit];
            _syncLock = new object();

            for (var i = 0; i < concurrencyLimit; i++)
            {
                _runTasks[i] = Task.Run(() => RunFromChannel());
            }
        }