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)); }
public async Task PubAsync(string subject, string body, string replyTo = null) { ThrowIfDisposed(); var cmdPayload = PubCmd.Generate(subject, body, replyTo); await _connection.WithWriteLockAsync(async writer => { await writer.WriteAsync(cmdPayload).ConfigureAwait(false); if (ShouldAutoFlush) { await writer.FlushAsync().ConfigureAwait(false); } }).ConfigureAwait(false); }
public void Pub(string subject, IPayload body, string replyTo = null) { ThrowIfDisposed(); var cmdPayload = PubCmd.Generate(subject, body, replyTo); _connection.WithWriteLock(writer => { writer.Write(cmdPayload); if (ShouldAutoFlush) { writer.Flush(); } }); }
public async Task PubAsync(string subject, IPayload body, string replyTo = null) => await _pubPayloadAsync(PubCmd.Generate(subject, body, replyTo)).ConfigureAwait(false);
public async Task PubAsync(string subject, byte[] body, string replyTo = null) => await _pubBytesAsync(PubCmd.Generate(subject, body, replyTo)).ConfigureAwait(false);
public void Pub(string subject, IPayload body, string replyTo = null) => _pubPayloadSync(PubCmd.Generate(subject, body, replyTo));
public void Pub(string subject, byte[] body, string replyTo = null) => _pubBytesSync(PubCmd.Generate(subject, body, replyTo));