Ejemplo n.º 1
0
        private void DoSub(SubscriptionInfo subscriptionInfo) => _connection.WithWriteLock(writer =>
        {
            writer.Write(SubCmd.Generate(subscriptionInfo.Subject, subscriptionInfo.Id, subscriptionInfo.QueueGroup));

            if (subscriptionInfo.MaxMessages.HasValue)
            {
                writer.Write(UnsubCmd.Generate(subscriptionInfo.Id, subscriptionInfo.MaxMessages));
            }

            writer.Flush();
        });
Ejemplo n.º 2
0
        private async Task DoSubAsync(SubscriptionInfo subscriptionInfo) => await _connection.WithWriteLockAsync(async writer =>
        {
            await writer.WriteAsync(SubCmd.Generate(subscriptionInfo.Subject, subscriptionInfo.Id, subscriptionInfo.QueueGroup)).ConfigureAwait(false);

            if (subscriptionInfo.MaxMessages.HasValue)
            {
                await writer.WriteAsync(UnsubCmd.Generate(subscriptionInfo.Id, subscriptionInfo.MaxMessages)).ConfigureAwait(false);
            }

            await writer.FlushAsync().ConfigureAwait(false);
        }).ConfigureAwait(false);
Ejemplo n.º 3
0
        private async Task <MsgOp> DoRequestAsync(string subject, byte[] body, int?timeoutMs)
        {
            var requestReplyAddress = $"{Guid.NewGuid():N}";
            var pubCmd = PubCmd.Generate(subject, body, requestReplyAddress);

            var taskComp            = new TaskCompletionSource <MsgOp>();
            var requestSubscription = MsgOpStream.Where(msg => msg.Subject == requestReplyAddress).Subscribe(
                msg => taskComp.SetResult(msg),
                ex => taskComp.SetException(ex));

            var subscriptionInfo = new SubscriptionInfo(requestReplyAddress, maxMessages: 1);
            var subCmd           = SubCmd.Generate(subscriptionInfo.Subject, subscriptionInfo.Id);
            var unsubCmd         = UnsubCmd.Generate(subscriptionInfo.Id, subscriptionInfo.MaxMessages);

            await _connection.WithWriteLockAsync(async writer =>
            {
                await writer.WriteAsync(subCmd).ConfigureAwait(false);
                await writer.WriteAsync(unsubCmd).ConfigureAwait(false);
                await writer.FlushAsync().ConfigureAwait(false);
                await writer.WriteAsync(pubCmd).ConfigureAwait(false);
                await writer.FlushAsync().ConfigureAwait(false);
            }).ConfigureAwait(false);

            Task.WaitAny(new[] { Task.Delay(timeoutMs ?? _connectionInfo.RequestTimeoutMs), taskComp.Task }, _cancellation.Token);
            if (!taskComp.Task.IsCompleted)
            {
                taskComp.SetException(NatsException.RequestTimedOut());
            }

            return(await taskComp.Task
                   .ContinueWith(t =>
            {
                requestSubscription?.Dispose();

                if (!t.IsFaulted)
                {
                    return t.Result;
                }

                var ex = t.Exception?.GetBaseException() ?? t.Exception;
                if (ex == null)
                {
                    return t.Result;
                }

                _logger.Error("Exception while performing request.", ex);

                throw ex;
            })
                   .ConfigureAwait(false));
        }