-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JetStream consume error notifications (#220)
* JetStream consume error notifications Notify users of events like timeouts while consuming messages. * Test fixes for ordered consume error handler * Kick GitHub CI * Exception handling for notifications * For normal consumer we are using notification channel since recovery effort is happening with in the consumer subscription. * For Ordered consumer we are using the handler directly in the loop since the consume subscription does not do error recovery and all exceptions are handled with in the main consumer loop. * Cancellation support for notification handlers * Attempt to fix test flapper * Fixing test flapper Using fetch instead of next. It is a little pointless using notifications with next.
- Loading branch information
Showing
8 changed files
with
515 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace NATS.Client.JetStream; | ||
|
||
public interface INatsJSNotification | ||
{ | ||
string Name { get; } | ||
} | ||
|
||
public class NatsJSTimeoutNotification : INatsJSNotification | ||
{ | ||
public string Name => "Timeout"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/NATS.Client.JetStream/Internal/NatsJSNotificationChannel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using System.Threading.Channels; | ||
|
||
namespace NATS.Client.JetStream.Internal; | ||
|
||
internal class NatsJSNotificationChannel : IAsyncDisposable | ||
{ | ||
private readonly Func<INatsJSNotification, CancellationToken, Task> _notificationHandler; | ||
private readonly Action<Exception> _exceptionHandler; | ||
private readonly CancellationToken _cancellationToken; | ||
private readonly Channel<INatsJSNotification> _channel; | ||
private readonly Task _loop; | ||
|
||
public NatsJSNotificationChannel( | ||
Func<INatsJSNotification, CancellationToken, Task> notificationHandler, | ||
Action<Exception> exceptionHandler, | ||
CancellationToken cancellationToken) | ||
{ | ||
_notificationHandler = notificationHandler; | ||
_exceptionHandler = exceptionHandler; | ||
_cancellationToken = cancellationToken; | ||
_channel = Channel.CreateBounded<INatsJSNotification>(new BoundedChannelOptions(128) | ||
{ | ||
AllowSynchronousContinuations = false, | ||
SingleReader = false, | ||
SingleWriter = false, | ||
FullMode = BoundedChannelFullMode.DropOldest, | ||
}); | ||
_loop = Task.Run(NotificationLoop, _cancellationToken); | ||
} | ||
|
||
public void Notify(INatsJSNotification notification) => _channel.Writer.TryWrite(notification); | ||
|
||
public async ValueTask DisposeAsync() | ||
{ | ||
_channel.Writer.TryComplete(); | ||
try | ||
{ | ||
await _loop; | ||
} | ||
catch (OperationCanceledException) | ||
{ | ||
} | ||
} | ||
|
||
private async Task NotificationLoop() | ||
{ | ||
try | ||
{ | ||
while (await _channel.Reader.WaitToReadAsync(_cancellationToken)) | ||
{ | ||
while (_channel.Reader.TryRead(out var notification)) | ||
{ | ||
try | ||
{ | ||
await _notificationHandler(notification, _cancellationToken); | ||
} | ||
catch (Exception e) | ||
{ | ||
_exceptionHandler(e); | ||
} | ||
} | ||
} | ||
} | ||
catch (OperationCanceledException) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.