/
NatsJSOrderedConsumer.cs
343 lines (298 loc) · 12.9 KB
/
NatsJSOrderedConsumer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
using System.Runtime.CompilerServices;
using Microsoft.Extensions.Logging;
using NATS.Client.Core;
using NATS.Client.JetStream.Models;
namespace NATS.Client.JetStream;
/// <summary>
/// NATS JetStream ordered consumer.
/// </summary>
public class NatsJSOrderedConsumer : INatsJSConsumer
{
private readonly ILogger<NatsJSOrderedConsumer> _logger;
private readonly string _stream;
private readonly NatsJSContext _context;
private readonly NatsJSOrderedConsumerOpts _opts;
private readonly CancellationToken _cancellationToken;
private ulong _fetchSeq;
private string _fetchConsumerName = string.Empty;
/// <summary>
/// Creates a new NATS JetStream ordered consumer.
/// </summary>
/// <param name="stream">Name of the stream.</param>
/// <param name="context">NATS JetStream context.</param>
/// <param name="opts">Consumer options.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> used to cancel consume and fetch operations.</param>
public NatsJSOrderedConsumer(string stream, NatsJSContext context, NatsJSOrderedConsumerOpts opts, CancellationToken cancellationToken)
{
_logger = context.Connection.Opts.LoggerFactory.CreateLogger<NatsJSOrderedConsumer>();
_stream = stream;
_context = context;
_opts = opts;
_cancellationToken = cancellationToken;
// For ordered consumer we start with an empty consumer info object
// since consumers are created and updated during fetch and consume.
Info = new ConsumerInfo()
{
Name = string.Empty,
StreamName = string.Empty,
};
}
/// <summary>
/// Consumer info object created during consume and fetch operations.
/// </summary>
public ConsumerInfo Info { get; private set; }
/// <summary>
/// Consume messages from the stream in order.
/// </summary>
/// <param name="serializer">Serializer to use for the message type.</param>
/// <param name="opts">Consume options.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> used to cancel consume operation.</param>
/// <typeparam name="T">Serialized message data type.</typeparam>
/// <returns>Asynchronous enumeration which can be used in a <c>await foreach</c> loop.</returns>
/// <exception cref="NatsJSProtocolException">There was a JetStream server error.</exception>
public async IAsyncEnumerable<NatsJSMsg<T>> ConsumeAsync<T>(
INatsDeserialize<T>? serializer = default,
NatsJSConsumeOpts? opts = default,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
opts ??= _context.Opts.DefaultConsumeOpts;
var consumerName = string.Empty;
var notificationHandler = opts.NotificationHandler;
try
{
cancellationToken = CancellationTokenSource.CreateLinkedTokenSource(_cancellationToken, cancellationToken).Token;
ulong seq = 0;
while (!cancellationToken.IsCancellationRequested)
{
var consumer = await RecreateConsumer(consumerName, seq, cancellationToken);
consumerName = consumer.Info.Name;
_logger.LogInformation(NatsJSLogEvents.NewConsumer, "Created {ConsumerName} with sequence {Seq}", consumerName, seq);
NatsJSProtocolException? protocolException = default;
await using (var cc = await consumer.OrderedConsumeInternalAsync(serializer, opts, cancellationToken))
{
// Keep subscription alive (since it's a wek ref in subscription manager) until we're done.
using var anchor = _context.Connection.RegisterSubAnchor(cc);
while (true)
{
// We have to check every call to WaitToReadAsync and TryRead for
// protocol exceptions individually because we can't yield return
// within try-catch.
try
{
var read = await cc.Msgs.WaitToReadAsync(cancellationToken).ConfigureAwait(false);
if (!read)
break;
}
catch (OperationCanceledException)
{
break;
}
catch (NatsJSProtocolException pe)
{
protocolException = pe;
goto CONSUME_LOOP;
}
catch (NatsJSConnectionException e)
{
_logger.LogWarning(NatsJSLogEvents.Retry, "{Error}. Retrying...", e.Message);
goto CONSUME_LOOP;
}
catch (NatsJSTimeoutException e)
{
notificationHandler?.Invoke(new NatsJSTimeoutNotification(), cancellationToken);
_logger.LogWarning(NatsJSLogEvents.Retry, "{Error}. Retrying...", e.Message);
goto CONSUME_LOOP;
}
while (true)
{
NatsJSMsg<T> msg;
var canRead = cc.Msgs.TryRead(out msg);
if (!canRead)
break;
if (msg.Metadata is not { } metadata)
continue;
seq = metadata.Sequence.Stream;
yield return msg;
}
}
}
CONSUME_LOOP:
_logger.LogWarning(NatsJSLogEvents.Internal, "Consumer loop exited");
if (protocolException != null)
{
if (protocolException
is { HeaderCode: 409, HeaderMessage: NatsHeaders.Messages.ConsumerDeleted }
or { HeaderCode: 404 })
{
// Ignore missing consumer errors and let the
// consumer be recreated above.
}
else
{
await TryDeleteConsumer(consumerName, cancellationToken);
throw protocolException;
}
}
if (await TryDeleteConsumer(consumerName, cancellationToken))
{
consumerName = string.Empty;
}
}
}
finally
{
if (!string.IsNullOrWhiteSpace(consumerName))
await TryDeleteConsumer(consumerName, cancellationToken);
}
}
/// <summary>
/// Fetch messages from the stream in order.
/// </summary>
/// <param name="opts">Fetch options.</param>
/// <param name="serializer">Serializer to use for the message type.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> used to cancel fetch operation.</param>
/// <typeparam name="T">Serialized message data type.</typeparam>
/// <returns>Asynchronous enumeration which can be used in a <c>await foreach</c> loop.</returns>
public async IAsyncEnumerable<NatsJSMsg<T>> FetchAsync<T>(
NatsJSFetchOpts opts,
INatsDeserialize<T>? serializer = default,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
cancellationToken = CancellationTokenSource.CreateLinkedTokenSource(_cancellationToken, cancellationToken).Token;
var consumer = await RecreateConsumer(_fetchConsumerName, _fetchSeq, cancellationToken);
_fetchConsumerName = consumer.Info.Name;
await foreach (var msg in consumer.FetchAsync(opts, serializer, cancellationToken))
{
if (msg.Metadata is not { } metadata)
continue;
_fetchSeq = metadata.Sequence.Stream;
yield return msg;
}
var deleted = await TryDeleteConsumer(_fetchConsumerName, cancellationToken);
if (deleted)
_fetchConsumerName = string.Empty;
}
/// <inheritdoc />
public async IAsyncEnumerable<NatsJSMsg<T>> FetchNoWaitAsync<T>(
NatsJSFetchOpts opts,
INatsDeserialize<T>? serializer = default,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
cancellationToken = CancellationTokenSource.CreateLinkedTokenSource(_cancellationToken, cancellationToken).Token;
var consumer = await RecreateConsumer(_fetchConsumerName, _fetchSeq, cancellationToken);
_fetchConsumerName = consumer.Info.Name;
try
{
await foreach (var msg in consumer.FetchNoWaitAsync(opts, serializer, cancellationToken))
{
if (msg.Metadata is not { } metadata)
continue;
_fetchSeq = metadata.Sequence.Stream;
yield return msg;
}
}
finally
{
var deleted = await TryDeleteConsumer(_fetchConsumerName, cancellationToken);
if (deleted)
_fetchConsumerName = string.Empty;
}
}
/// <summary>
/// Get the next message from the stream in order.
/// </summary>
/// <param name="serializer">Serializer to use for the message type.</param>
/// <param name="opts">Next options.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> used to cancel the underlying fetch operation.</param>
/// <typeparam name="T">Serialized message data type.</typeparam>
/// <returns>The next NATS JetStream message in order.</returns>
public async ValueTask<NatsJSMsg<T>?> NextAsync<T>(INatsDeserialize<T>? serializer = default, NatsJSNextOpts? opts = default, CancellationToken cancellationToken = default)
{
opts ??= _context.Opts.DefaultNextOpts;
var fetchOpts = new NatsJSFetchOpts
{
MaxMsgs = 1,
IdleHeartbeat = opts.IdleHeartbeat,
Expires = opts.Expires,
NotificationHandler = opts.NotificationHandler,
};
await foreach (var msg in FetchAsync(fetchOpts, serializer, cancellationToken))
{
return msg;
}
return default;
}
/// <summary>
/// For ordered consumer this is a no-op.
/// </summary>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> used to cancel the API call.</param>
public ValueTask RefreshAsync(CancellationToken cancellationToken = default) => ValueTask.CompletedTask;
private async Task<NatsJSConsumer> RecreateConsumer(string consumer, ulong seq, CancellationToken cancellationToken)
{
var consumerOpts = _opts;
if (seq > 0)
{
consumerOpts = _opts with
{
OptStartSeq = seq + 1,
DeliverPolicy = ConsumerConfigDeliverPolicy.ByStartSequence,
OptStartTime = default,
};
if (consumer != string.Empty)
{
for (var i = 1; ; i++)
{
cancellationToken.ThrowIfCancellationRequested();
try
{
await _context.DeleteConsumerAsync(_stream, consumer, cancellationToken);
break;
}
catch (NatsJSApiNoResponseException)
{
}
catch (NatsJSApiException apiException)
{
if (apiException.Error.Code == 404)
{
break;
}
}
if (i == _opts.MaxResetAttempts)
{
throw new NatsJSException("Maximum number of delete attempts reached.");
}
}
}
}
ConsumerInfo info;
for (var i = 0; ; i++)
{
try
{
info = await _context.CreateOrderedConsumerInternalAsync(_stream, consumerOpts, cancellationToken);
break;
}
catch (NatsJSApiNoResponseException)
{
}
if (i == _opts.MaxResetAttempts)
{
throw new NatsJSException("Maximum number of create attempts reached.");
}
}
Info = info;
return new NatsJSConsumer(_context, info);
}
private async ValueTask<bool> TryDeleteConsumer(string consumerName, CancellationToken cancellationToken)
{
try
{
return await _context.DeleteConsumerAsync(_stream, consumerName, cancellationToken);
}
catch
{
return false;
}
}
}