public bool Offer(T e, long timeout, TimeUnit unit)
        {
            var data    = ToData(e);
            var request = QueueOfferCodec.EncodeRequest(GetName(), data, unit.ToMillis(timeout));

            return(Invoke(request, m => QueueOfferCodec.DecodeResponse(m).response));
        }
        private async Task <bool> TryEnqueueAsync(T item, TimeSpan timeToWait, CancellationToken cancellationToken)
        {
            var itemData = ToSafeData(item);

            var timeToWaitMilliseconds = (long)timeToWait.TotalMilliseconds;
            var requestMessage         = QueueOfferCodec.EncodeRequest(Name, itemData, timeToWaitMilliseconds);
            var responseMessage        = await Cluster.Messaging.SendToPartitionOwnerAsync(requestMessage, PartitionId, cancellationToken).CfAwait();

            return(QueueOfferCodec.DecodeResponse(responseMessage).Response);
        }
Пример #3
0
        private async Task <bool> EnqueueAsync(T item, TimeSpan timeToWait, bool doThrow, CancellationToken cancellationToken)
        {
            var itemData = ToSafeData(item);

            var timeToWaitMilliseconds = timeToWait.TimeoutMilliseconds(0);
            var requestMessage         = timeToWaitMilliseconds < 0
                ? QueuePutCodec.EncodeRequest(Name, itemData)
                : QueueOfferCodec.EncodeRequest(Name, itemData, timeToWaitMilliseconds);

            ClientMessage responseMessage;

            try
            {
                responseMessage = await Cluster.Messaging.SendToKeyPartitionOwnerAsync(requestMessage, PartitionKeyData, cancellationToken).CAF();
            }
            catch
            {
                if (doThrow)
                {
                    throw;
                }
                return(false);
            }

            bool queued;

            if (timeToWaitMilliseconds < 0)
            {
                _      = QueuePutCodec.DecodeResponse(responseMessage);
                queued = true;
            }
            else
            {
                queued = QueueOfferCodec.DecodeResponse(responseMessage).Response;
            }

            if (queued)
            {
                return(true);
            }
            if (doThrow)
            {
                throw new InvalidOperationException("Queue is full.");
            }
            return(false);
        }