From 2d3711bebce34752f1952aa6db57fc2e8cc4088c Mon Sep 17 00:00:00 2001 From: Ziya Suzen Date: Thu, 14 Dec 2023 19:57:19 +0000 Subject: [PATCH] Handle JetStream publish no-responders (#288) During cluster reconnect we often get no-responders exceptions. This change makes sure JetStream publish method will retry in that case too. --- src/NATS.Client.JetStream/NatsJSContext.cs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/NATS.Client.JetStream/NatsJSContext.cs b/src/NATS.Client.JetStream/NatsJSContext.cs index 9ed556ef6..d6779ed2c 100644 --- a/src/NATS.Client.JetStream/NatsJSContext.cs +++ b/src/NATS.Client.JetStream/NatsJSContext.cs @@ -135,23 +135,30 @@ public async ValueTask PublishAsync( Timeout = Connection.Opts.RequestTimeout, // If JetStream is disabled, a no responders error will be returned + // No responders error might also happen when reconnecting to cluster ThrowIfNoResponders = true, }, cancellationToken) .ConfigureAwait(false); - while (await sub.Msgs.WaitToReadAsync(cancellationToken).ConfigureAwait(false)) + try { - while (sub.Msgs.TryRead(out var msg)) + while (await sub.Msgs.WaitToReadAsync(cancellationToken).ConfigureAwait(false)) { - if (msg.Data == null) + while (sub.Msgs.TryRead(out var msg)) { - throw new NatsJSException("No response data received"); - } + if (msg.Data == null) + { + throw new NatsJSException("No response data received"); + } - return msg.Data; + return msg.Data; + } } } + catch (NatsNoRespondersException) + { + } if (i < retryMax) {