Пример #1
0
        public async Task UnsubAsync(SubscriptionInfo subscriptionInfo)
        {
            EnsureArg.IsNotNull(subscriptionInfo, nameof(subscriptionInfo));

            ThrowIfDisposed();

            if (!TryRemoveSubscription(subscriptionInfo))
            {
                return;
            }

            if (!IsConnected)
            {
                return;
            }

            var cmdPayload = UnsubCmd.Generate(subscriptionInfo.Id, subscriptionInfo.MaxMessages);

            await _connection.WithWriteLockAsync(async writer =>
            {
                if (!IsConnected)
                {
                    return;
                }

                await writer.WriteAsync(cmdPayload).ConfigureAwait(false);
                await writer.FlushAsync().ConfigureAwait(false);
            }).ConfigureAwait(false);
        }
Пример #2
0
        public void Unsub(SubscriptionInfo subscriptionInfo)
        {
            EnsureArg.IsNotNull(subscriptionInfo, nameof(subscriptionInfo));

            ThrowIfDisposed();

            if (!TryRemoveSubscription(subscriptionInfo))
            {
                return;
            }

            if (!IsConnected)
            {
                return;
            }

            var cmdPayload = UnsubCmd.Generate(subscriptionInfo.Id, subscriptionInfo.MaxMessages);

            _connection.WithWriteLock(writer =>
            {
                if (!IsConnected)
                {
                    return;
                }

                writer.Write(cmdPayload);
                writer.Flush();
            });
        }
Пример #3
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();
        });
Пример #4
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);
Пример #5
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));
        }