Exemplo n.º 1
0
 public ProducerConfiguration(
     int requestParallelization = Defaults.RequestParallelization,
     int batchSize          = Defaults.BatchSize,
     TimeSpan?batchMaxDelay = null,
     TimeSpan?stopTimeout   = null,
     ISendMessageConfiguration sendDefaults = null)
 {
     RequestParallelization = requestParallelization;
     BatchSize     = Math.Max(1, batchSize);
     BatchMaxDelay = batchMaxDelay ?? TimeSpan.FromMilliseconds(Defaults.BatchMaxDelayMilliseconds);
     StopTimeout   = stopTimeout ?? TimeSpan.FromSeconds(Defaults.DefaultStopTimeoutSeconds);
     SendDefaults  = sendDefaults ?? new SendMessageConfiguration();
 }
Exemplo n.º 2
0
 /// <summary>
 /// Send a message to the given topic.
 /// </summary>
 /// <param name="producer">The message producer</param>
 /// <param name="message">The message to send.</param>
 /// <param name="topicName">The name of the kafka topic to send the messages to.</param>
 /// <param name="configuration">The configuration for sending the messages (ie acks, ack Timeout and codec)</param>
 /// <param name="cancellationToken">The token for cancellation</param>
 /// <returns>List of ProduceTopic response from each partition sent to or empty list if acks = 0.</returns>
 public static Task <IEnumerable <ProduceResponse.Topic> > SendAsync(this IProducer producer, Message message, string topicName, ISendMessageConfiguration configuration, CancellationToken cancellationToken)
 {
     return(producer.SendAsync(new[] { message }, topicName, configuration, cancellationToken));
 }
Exemplo n.º 3
0
        /// <summary>
        /// Send a message to the given topic.
        /// </summary>
        /// <param name="producer">The message producer</param>
        /// <param name="message">The message to send.</param>
        /// <param name="topicName">The name of the kafka topic to send the messages to.</param>
        /// <param name="partition">The partition to send messages to, or <value>null</value> for any.</param>
        /// <param name="configuration">The configuration for sending the messages (ie acks, ack Timeout and codec)</param>
        /// <param name="cancellationToken">The token for cancellation</param>
        /// <returns>List of ProduceTopic response from each partition sent to or empty list if acks = 0.</returns>
        public static async Task <ProduceResponse.Topic> SendMessageAsync(this IProducer producer, Message message, string topicName, int?partition, ISendMessageConfiguration configuration, CancellationToken cancellationToken)
        {
            var result = await producer.SendMessagesAsync(new[] { message }, topicName, partition, configuration, cancellationToken).ConfigureAwait(false);

            return(result.SingleOrDefault());
        }
Exemplo n.º 4
0
        /// <inheritdoc />
        public async Task <ProduceResponse.Topic> SendAsync(IEnumerable <Message> messages, string topicName, int partitionId, ISendMessageConfiguration configuration, CancellationToken cancellationToken)
        {
            var produceTopicTask = new ProduceTask(topicName, partitionId, messages, configuration ?? Configuration.SendDefaults, cancellationToken);

            Interlocked.Add(ref _sendingMessageCount, produceTopicTask.Messages.Count);
            try {
                await _produceMessageQueue.EnqueueAsync(produceTopicTask, cancellationToken).ConfigureAwait(false);

                return(await produceTopicTask.Tcs.Task.ConfigureAwait(false));
            } catch (InvalidOperationException ex) {
                throw new ObjectDisposedException("Cannot send messages after Stopped or Disposed", ex);
            } finally {
                Interlocked.Add(ref _sendingMessageCount, -produceTopicTask.Messages.Count);
                produceTopicTask.Dispose();
            }
        }
Exemplo n.º 5
0
 public ProduceTask(string topicName, int partitionId, IEnumerable <Message> messages, ISendMessageConfiguration configuration, CancellationToken cancellationToken)
     : base(cancellationToken)
 {
     Partition  = new TopicPartition(topicName, partitionId);
     Messages   = messages as IList <Message> ?? messages.ToList();
     Codec      = configuration.Codec;
     Acks       = configuration.Acks;
     AckTimeout = configuration.AckTimeout;
 }
Exemplo n.º 6
0
        /// <inheritdoc />
        public async Task <IEnumerable <ProduceResponse.Topic> > SendAsync(IEnumerable <Message> messages, string topicName, ISendMessageConfiguration configuration, CancellationToken cancellationToken)
        {
            if (_disposeCount > 0)
            {
                throw new ObjectDisposedException("Cannot send messages after Stopped or Disposed");
            }

            var topic = await Router.GetTopicMetadataAsync(topicName, cancellationToken);

            var partitionedMessages =
                from message in messages
                group message by Configuration.PartitionSelector.Select(topic, message.Key).partition_id
                into partition
                select new { PartitionId = partition.Key, Messages = partition };

            var sendPartitions = partitionedMessages.Select(p => SendAsync(p.Messages, topicName, p.PartitionId, configuration, cancellationToken)).ToArray();
            await Task.WhenAll(sendPartitions);

            return(sendPartitions.Select(p => p.Result));
        }
Exemplo n.º 7
0
        /// <inheritdoc />
        public async Task <ImmutableList <ProduceResponse.Topic> > SendMessagesAsync(IEnumerable <Message> messages, string topicName, int?partition, ISendMessageConfiguration configuration, CancellationToken cancellationToken)
        {
            var produceTopicTasks = messages.Select(message => new ProduceTopicTask(topicName, partition, message, configuration ?? Configuration.SendDefaults, cancellationToken)).ToArray();

            Interlocked.Add(ref _sendingMessageCount, produceTopicTasks.Length);
            try {
                await _produceMessageQueue.EnqueueRangeAsync(produceTopicTasks, cancellationToken);

                var topics = await Task.WhenAll(produceTopicTasks.Select(x => x.Tcs.Task)).ConfigureAwait(false);

                return(topics.ToImmutableList());
            } catch (InvalidOperationException ex) {
                throw new ObjectDisposedException("Cannot send messages after Stopped or Disposed", ex);
            } finally {
                Interlocked.Add(ref _sendingMessageCount, -produceTopicTasks.Length);
            }
        }
Exemplo n.º 8
0
 public ProduceTopicTask(string topicName, int?partition, Message message, ISendMessageConfiguration configuration, CancellationToken cancellationToken)
     : base(cancellationToken)
 {
     TopicName  = topicName;
     Partition  = partition;
     Message    = message;
     Codec      = configuration.Codec;
     Acks       = configuration.Acks;
     AckTimeout = configuration.AckTimeout;
 }