Ejemplo n.º 1
0
 public RawOutboundEnvelope(
     IEnumerable <MessageHeader>?headers,
     IProducerEndpoint endpoint,
     IOffset?offset = null,
     IDictionary <string, string>?additionalLogData = null)
     : this(null, headers, endpoint, offset, additionalLogData)
 {
 }
Ejemplo n.º 2
0
        public async IAsyncEnumerable <IOutboundEnvelope> ProcessMessage(IOutboundEnvelope envelope)
        {
            Check.NotNull(envelope, nameof(envelope));

            if (envelope.RawMessage == null)
            {
                throw new InvalidOperationException("RawMessage is null");
            }

            var settings    = envelope.Endpoint.Chunk;
            var chunkSize   = settings?.Size ?? int.MaxValue;
            var bufferArray = ArrayPool <byte> .Shared.Rent(chunkSize);

            var bufferMemory = bufferArray.AsMemory(0, chunkSize);

            var chunksCount = envelope.RawMessage.CanSeek
                ? (int?)Math.Ceiling(envelope.RawMessage.Length / (double)chunkSize)
                : null;

            IOffset?firstChunkOffset = null;

            int chunkIndex = 0;

            var readBytesCount = await envelope.RawMessage.ReadAsync(bufferMemory).ConfigureAwait(false);

            while (readBytesCount > 0)
            {
                var chunkEnvelope = CreateChunkEnvelope(
                    chunkIndex,
                    chunksCount,
                    bufferMemory.Slice(0, readBytesCount).ToArray(),
                    envelope);

                if (chunkIndex > 0)
                {
                    chunkEnvelope.Headers.AddOrReplace(DefaultMessageHeaders.FirstChunkOffset, firstChunkOffset?.Value);
                }

                // Read the next chunk and check if we were at the end of the stream (to not rely on Length property)
                readBytesCount = await envelope.RawMessage.ReadAsync(bufferMemory).ConfigureAwait(false);

                if (readBytesCount == 0)
                {
                    chunkEnvelope.Headers.AddOrReplace(DefaultMessageHeaders.IsLastChunk, true.ToString());
                }

                yield return(chunkEnvelope);

                // Read and store the offset of the first chunk, after it has been produced (after yield return)
                if (chunkIndex == 0)
                {
                    firstChunkOffset = chunkEnvelope.Offset;
                }

                chunkIndex++;
            }
        }
Ejemplo n.º 3
0
 public RawOutboundEnvelope(
     Stream?rawMessage,
     IEnumerable <MessageHeader>?headers,
     IProducerEndpoint endpoint,
     IOffset?offset = null,
     IDictionary <string, string>?additionalLogData = null)
     : base(rawMessage, headers, endpoint, additionalLogData)
 {
     Offset = offset;
 }
Ejemplo n.º 4
0
        public void RecordConsumedMessage(IOffset?offset)
        {
            if (Status == ConsumerStatus.Connected)
            {
                ChangeStatus(ConsumerStatus.Consuming);
            }

            ConsumedMessagesCount++;
            LatestConsumedMessageTimestamp = DateTime.Now;
            LatestConsumedMessageOffset    = offset;
        }
Ejemplo n.º 5
0
        public int CompareTo(IOffset?other)
        {
            if (other == null)
            {
                return(0);
            }

            long thisValue  = long.Parse(Value, CultureInfo.InvariantCulture);
            long otherValue = long.Parse(other.Value, CultureInfo.InvariantCulture);

            return(thisValue.CompareTo(otherValue));
        }
Ejemplo n.º 6
0
        /// <inheritdoc cref="IComparable{T}.CompareTo" />
        public int CompareTo(IOffset?other)
        {
            if (ReferenceEquals(this, other))
            {
                return(0);
            }

            if (other is null)
            {
                return(1);
            }

            return(other is KafkaOffset otherKafkaOffset
                ? CompareTo(otherKafkaOffset)
                : throw new ArgumentException($"Object must be of type {nameof(KafkaOffset)}"));
        }
Ejemplo n.º 7
0
        public bool Equals(IOffset?other)
        {
            if (ReferenceEquals(this, other))
            {
                return(true);
            }
            if (ReferenceEquals(other, null))
            {
                return(false);
            }

            if (!(other is TestOffset otherRabbitOffset))
            {
                return(false);
            }

            return(Key == otherRabbitOffset.Key && Value == otherRabbitOffset.Value);
        }
Ejemplo n.º 8
0
        /// <inheritdoc cref="IEquatable{T}.Equals(T)" />
        public bool Equals(IOffset?other)
        {
            if (ReferenceEquals(this, other))
            {
                return(true);
            }
            if (ReferenceEquals(other, null))
            {
                return(false);
            }

            if (!(other is RabbitOffset otherRabbitOffset))
            {
                return(false);
            }

            return(ConsumerTag == otherRabbitOffset.ConsumerTag && DeliveryTag == otherRabbitOffset.DeliveryTag);
        }
Ejemplo n.º 9
0
        public async Task TestHandleMessage(
            object message,
            MessageHeaderCollection?headers,
            IOffset?offset = null,
            IMessageSerializer?serializer = null)
        {
            if (serializer == null)
            {
                serializer = new JsonMessageSerializer();
            }

            headers ??= new MessageHeaderCollection();

            var stream = await serializer.SerializeAsync(message, headers, MessageSerializationContext.Empty);

            var buffer = await stream.ReadAllAsync();

            await TestHandleMessage(buffer, headers, offset);
        }
Ejemplo n.º 10
0
        /// <inheritdoc cref="IEquatable{T}.Equals(T)" />
        public bool Equals(IOffset?other)
        {
            if (ReferenceEquals(this, other))
            {
                return(true);
            }
            if (ReferenceEquals(other, null))
            {
                return(false);
            }

            if (!(other is KafkaOffset otherKafkaOffset))
            {
                return(false);
            }

            return(Topic == otherKafkaOffset.Topic &&
                   Partition == otherKafkaOffset.Partition &&
                   Offset == otherKafkaOffset.Offset);
        }
Ejemplo n.º 11
0
        public OutboundEnvelope(
            object?message,
            IEnumerable <MessageHeader>?headers,
            IProducerEndpoint endpoint,
            bool autoUnwrap = false,
            IOffset?offset  = null)
            : base(headers, endpoint, offset)
        {
            _message = message;

            if (Message is byte[] rawMessage)
            {
                RawMessage = new MemoryStream(rawMessage);
            }

            if (Message is Stream stream)
            {
                RawMessage = stream;
            }

            AutoUnwrap = autoUnwrap;
        }
Ejemplo n.º 12
0
        public async Task TestHandleMessage(byte[]?rawMessage, MessageHeaderCollection headers, IOffset?offset = null)
        {
            if (!Broker.IsConnected)
            {
                throw new InvalidOperationException("The broker is not connected.");
            }

            if (!IsConnected)
            {
                throw new InvalidOperationException("The consumer is not ready.");
            }

            await HandleMessageAsync(
                rawMessage,
                headers,
                "test-topic",
                offset ?? new TestOffset(),
                null);
        }
Ejemplo n.º 13
0
 public Task TestConsume(
     byte[]?rawMessage,
     IEnumerable <MessageHeader>?headers = null,
     IOffset?offset = null) =>
 TestHandleMessage(rawMessage, new MessageHeaderCollection(headers), offset);
Ejemplo n.º 14
0
 public Task TestHandleMessage(
     object message,
     IEnumerable <MessageHeader>?headers = null,
     IOffset?offset = null,
     IMessageSerializer?serializer = null) =>
 TestHandleMessage(message, new MessageHeaderCollection(headers), offset, serializer);
Ejemplo n.º 15
0
 public bool Equals(IOffset?other) => false;