Пример #1
0
        private async ValueTask <IMessage <T> > FetchSingleMessageFromBroker()
        {
            // Just being cautious
            if (IncomingMessages.Count > 0)
            {
                _log.Error("The incoming message queue should never be greater than 0 when Queue size is 0");
                IncomingMessages.Empty();
            }

            IMessage <T> message;

            try
            {
                // if cnx is null or if the connection breaks the connectionOpened function will send the flow again
                _waitingOnReceiveForZeroQueueSize = true;
                if (Connected())
                {
                    IncreaseAvailablePermits(_clientCnx);
                }
                do
                {
                    message = await IncomingMessages.ReceiveAsync();

                    _lastDequeuedMessageId = message.MessageId;
                    var msgCnx = ((Message <T>)message).Cnx();
                    // synchronized need to prevent race between connectionOpened and the check "msgCnx == cnx()"
                    // if message received due to an old flow - discard it and wait for the message from the
                    // latest flow command
                    if (msgCnx == _clientCnx)
                    {
                        _waitingOnReceiveForZeroQueueSize = false;
                        break;
                    }
                } while(true);

                Stats.UpdateNumMsgsReceived(message);
                return(message);
            }
            catch (Exception e)
            {
                Stats.IncrementNumReceiveFailed();
                throw PulsarClientException.Unwrap(e);
            }
            finally
            {
                // Finally blocked is invoked in case the block on incomingMessages is interrupted
                _waitingOnReceiveForZeroQueueSize = false;
                // Clearing the queue in case there was a race with messageReceived
                IncomingMessages.Empty();
            }
        }
 public Task <Message> ReceiveMessageAsync(CancellationToken cancellationToken)
 {
     return(IncomingMessages.ReceiveAsync(cancellationToken));
 }