Пример #1
0
        public async ValueTask <Message> Receive(CancellationToken cancellationToken)
        {
            using (await _lock.Lock(cancellationToken).ConfigureAwait(false))
            {
                while (true)
                {
                    if (_sendWhenZero == 0)
                    {
                        await SendFlow(cancellationToken).ConfigureAwait(false);
                    }

                    _sendWhenZero--;

                    var message = _batchHandler.GetNext();

                    if (message is not null)
                    {
                        return(message);
                    }

                    var messagePackage = await _queue.Dequeue(cancellationToken).ConfigureAwait(false);

                    if (!messagePackage.IsValid())
                    {
                        await RejectPackage(messagePackage, cancellationToken).ConfigureAwait(false);

                        continue;
                    }

                    var metadataSize    = messagePackage.GetMetadataSize();
                    var redeliveryCount = messagePackage.RedeliveryCount;
                    var data            = messagePackage.ExtractData(metadataSize);
                    var metadata        = messagePackage.ExtractMetadata(metadataSize);
                    var messageId       = messagePackage.MessageId;

                    return(metadata.ShouldSerializeNumMessagesInBatch()
                        ? _batchHandler.Add(messageId, redeliveryCount, metadata, data)
                        : MessageFactory.Create(new MessageId(messageId), redeliveryCount, metadata, data));
                }
            }
        }
Пример #2
0
        public async ValueTask <Message> Receive(CancellationToken cancellationToken)
        {
            while (true)
            {
                if (_sendWhenZero == 0)
                {
                    await SendFlow();
                }

                _sendWhenZero--;

                var message = _batchHandler.GetNext();
                if (message != null)
                {
                    return(message);
                }

                var messagePackage = await _dequeue.Dequeue(cancellationToken);

                if (!await Validate(messagePackage))
                {
                    continue;
                }

                var messageId = messagePackage.MessageId;
                var data      = messagePackage.Data;

                var metadataSize = data.ReadUInt32(6, true);
                var metadata     = Serializer.Deserialize <PulsarApi.MessageMetadata>(data.Slice(10, metadataSize));
                data = data.Slice(10 + metadataSize);

                if (metadata.NumMessagesInBatch == 1)
                {
                    return(new Message(new MessageId(messageId), metadata, null, data));
                }

                return(_batchHandler.Add(messageId, metadata, data));
            }
        }
        public async ValueTask <Message> Receive(CancellationToken cancellationToken)
        {
            while (true)
            {
                if (_sendWhenZero == 0)
                {
                    await SendFlow(cancellationToken).ConfigureAwait(false);
                }

                _sendWhenZero--;

                var message = _batchHandler.GetNext();

                if (message != null)
                {
                    return(message);
                }

                var messagePackage = await _queue.Dequeue(cancellationToken).ConfigureAwait(false);

                if (!messagePackage.IsValid())
                {
                    await RejectPackage(messagePackage, cancellationToken).ConfigureAwait(false);

                    continue;
                }

                var metadataSize = messagePackage.GetMetadataSize();
                var data         = messagePackage.ExtractData(metadataSize);
                var metadata     = messagePackage.ExtractMetadata(metadataSize);
                var messageId    = messagePackage.MessageId;

                return(metadata.NumMessagesInBatch == 1
                    ? new Message(new MessageId(messageId), metadata, null, data)
                    : _batchHandler.Add(messageId, metadata, data));
            }
        }
Пример #4
0
        public async ValueTask <Message> Receive(CancellationToken cancellationToken)
        {
            using (await _lock.Lock(cancellationToken).ConfigureAwait(false))
            {
                while (true)
                {
                    if (_sendWhenZero == 0)
                    {
                        await SendFlow(cancellationToken).ConfigureAwait(false);
                    }

                    _sendWhenZero--;

                    var message = _batchHandler.GetNext();

                    if (message is not null)
                    {
                        return(message);
                    }

                    var messagePackage = await _queue.Dequeue(cancellationToken).ConfigureAwait(false);

                    if (!messagePackage.ValidateMagicNumberAndChecksum())
                    {
                        await RejectPackage(messagePackage, CommandAck.ValidationErrorType.ChecksumMismatch, cancellationToken).ConfigureAwait(false);

                        continue;
                    }

                    var metadataSize = messagePackage.GetMetadataSize();
                    var metadata     = messagePackage.ExtractMetadata(metadataSize);
                    var data         = messagePackage.ExtractData(metadataSize);

                    if (metadata.Compression != CompressionType.None)
                    {
                        var decompressor = _decompressors[(int)metadata.Compression];
                        if (decompressor is null)
                        {
                            throw new CompressionException($"Support for {metadata.Compression} compression was not found");
                        }

                        try
                        {
                            data = decompressor.Decompress(data, (int)metadata.UncompressedSize);
                        }
                        catch
                        {
                            await RejectPackage(messagePackage, CommandAck.ValidationErrorType.DecompressionError, cancellationToken).ConfigureAwait(false);

                            continue;
                        }
                    }

                    var messageId       = messagePackage.MessageId;
                    var redeliveryCount = messagePackage.RedeliveryCount;

                    return(metadata.ShouldSerializeNumMessagesInBatch()
                        ? _batchHandler.Add(messageId, redeliveryCount, metadata, data)
                        : MessageFactory.Create(messageId.ToMessageId(), redeliveryCount, metadata, data));
                }
            }
        }