Skip to content

Commit

Permalink
Handle JetStream publish no-responders (#288)
Browse files Browse the repository at this point in the history
During cluster reconnect we often get no-responders exceptions. This change
makes sure JetStream publish method will retry in that case too.
  • Loading branch information
mtmk authored Dec 14, 2023
1 parent 686e6d7 commit 2d3711b
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/NATS.Client.JetStream/NatsJSContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,23 +135,30 @@ public async ValueTask<PubAckResponse> PublishAsync<T>(
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)
{
Expand Down

0 comments on commit 2d3711b

Please sign in to comment.