| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | using AsiBackbone.Core.Emissions; |
| | | 3 | | using AsiBackbone.Core.Outbox; |
| | | 4 | | |
| | | 5 | | namespace AsiBackbone.Storage.InMemory.Outbox; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// In-memory governance outbox store for tests, samples, and development hosts. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <remarks> |
| | | 11 | | /// This store is not durable across process restarts. Production hosts should use a durable provider such as EF Core or |
| | | 12 | | /// Same-entry status transitions use single-process compare-and-swap updates so tests and local validation do not accid |
| | | 13 | | /// </remarks> |
| | | 14 | | public sealed class InMemoryGovernanceOutboxStore : IAsiBackboneGovernanceOutboxClaimStore |
| | | 15 | | { |
| | 106 | 16 | | private readonly ConcurrentDictionary<string, GovernanceOutboxEntry> entries = new(StringComparer.Ordinal); |
| | | 17 | | |
| | | 18 | | /// <inheritdoc /> |
| | | 19 | | public ValueTask<GovernanceOutboxEntry> EnqueueAsync( |
| | | 20 | | GovernanceEmissionEnvelope envelope, |
| | | 21 | | CancellationToken cancellationToken = default) |
| | | 22 | | { |
| | 100 | 23 | | ArgumentNullException.ThrowIfNull(envelope); |
| | 100 | 24 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 25 | | |
| | 100 | 26 | | var entry = GovernanceOutboxEntry.Create(envelope); |
| | | 27 | | |
| | 100 | 28 | | return !entries.TryAdd(entry.OutboxEntryId, entry) |
| | 100 | 29 | | ? throw new InvalidOperationException($"Outbox entry '{entry.OutboxEntryId}' already exists.") |
| | 100 | 30 | | : ValueTask.FromResult(entry); |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <inheritdoc /> |
| | | 34 | | public ValueTask<GovernanceOutboxEntry> SaveAsync( |
| | | 35 | | GovernanceOutboxEntry entry, |
| | | 36 | | CancellationToken cancellationToken = default) |
| | | 37 | | { |
| | 12 | 38 | | ArgumentNullException.ThrowIfNull(entry); |
| | 12 | 39 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 40 | | |
| | 12 | 41 | | return ValueTask.FromResult(SaveEntry(entry, cancellationToken)); |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <inheritdoc /> |
| | | 45 | | public ValueTask<GovernanceOutboxEntry?> FindByOutboxEntryIdAsync( |
| | | 46 | | string outboxEntryId, |
| | | 47 | | CancellationToken cancellationToken = default) |
| | | 48 | | { |
| | 24 | 49 | | ArgumentException.ThrowIfNullOrWhiteSpace(outboxEntryId); |
| | 24 | 50 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 51 | | |
| | 24 | 52 | | _ = entries.TryGetValue(outboxEntryId.Trim(), out GovernanceOutboxEntry? entry); |
| | | 53 | | |
| | 24 | 54 | | return ValueTask.FromResult(entry); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <inheritdoc /> |
| | | 58 | | public ValueTask<IReadOnlyList<GovernanceOutboxEntry>> FindPendingAsync( |
| | | 59 | | int maxCount = 100, |
| | | 60 | | CancellationToken cancellationToken = default) |
| | | 61 | | { |
| | 40 | 62 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 63 | | |
| | 40 | 64 | | int normalizedMaxCount = NormalizeMaxCount(maxCount); |
| | | 65 | | |
| | 40 | 66 | | IReadOnlyList<GovernanceOutboxEntry> matches = [.. entries.Values |
| | 42 | 67 | | .Where(entry => entry.Status is GovernanceEmissionStatus.Pending) |
| | 32 | 68 | | .OrderBy(entry => entry.CreatedUtc) |
| | 32 | 69 | | .ThenBy(entry => entry.OutboxEntryId) |
| | 40 | 70 | | .Take(normalizedMaxCount)]; |
| | | 71 | | |
| | 40 | 72 | | return ValueTask.FromResult(matches); |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <inheritdoc /> |
| | | 76 | | public ValueTask<IReadOnlyList<GovernanceOutboxEntry>> FindRetryReadyAsync( |
| | | 77 | | DateTimeOffset utcNow, |
| | | 78 | | int maxCount = 100, |
| | | 79 | | CancellationToken cancellationToken = default) |
| | | 80 | | { |
| | 42 | 81 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 82 | | |
| | 42 | 83 | | int normalizedMaxCount = NormalizeMaxCount(maxCount); |
| | 42 | 84 | | DateTimeOffset normalizedUtcNow = utcNow.ToUniversalTime(); |
| | | 85 | | |
| | 42 | 86 | | IReadOnlyList<GovernanceOutboxEntry> matches = [.. entries.Values |
| | 42 | 87 | | .Where(entry => entry.IsRetryReady(normalizedUtcNow)) |
| | 8 | 88 | | .OrderBy(entry => entry.NextRetryUtc ?? entry.UpdatedUtc) |
| | 8 | 89 | | .ThenBy(entry => entry.OutboxEntryId) |
| | 42 | 90 | | .Take(normalizedMaxCount)]; |
| | | 91 | | |
| | 42 | 92 | | return ValueTask.FromResult(matches); |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | /// <inheritdoc /> |
| | | 96 | | public ValueTask<IReadOnlyList<GovernanceOutboxClaim>> ClaimPendingAsync( |
| | | 97 | | GovernanceOutboxClaimRequest request, |
| | | 98 | | CancellationToken cancellationToken = default) |
| | | 99 | | { |
| | 60 | 100 | | ArgumentNullException.ThrowIfNull(request); |
| | 60 | 101 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 102 | | |
| | 60 | 103 | | IReadOnlyList<GovernanceOutboxEntry> candidates = [.. entries.Values |
| | 58 | 104 | | .Where(entry => entry.Status is GovernanceEmissionStatus.Pending) |
| | 52 | 105 | | .Where(entry => entry.CanBeClaimed(request.UtcNow)) |
| | 48 | 106 | | .OrderBy(entry => entry.CreatedUtc) |
| | 108 | 107 | | .ThenBy(entry => entry.OutboxEntryId)]; |
| | | 108 | | |
| | 60 | 109 | | return ValueTask.FromResult<IReadOnlyList<GovernanceOutboxClaim>>(ClaimEntries(candidates, request, cancellation |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | /// <inheritdoc /> |
| | | 113 | | public ValueTask<IReadOnlyList<GovernanceOutboxClaim>> ClaimRetryReadyAsync( |
| | | 114 | | GovernanceOutboxClaimRequest request, |
| | | 115 | | CancellationToken cancellationToken = default) |
| | | 116 | | { |
| | 34 | 117 | | ArgumentNullException.ThrowIfNull(request); |
| | 34 | 118 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 119 | | |
| | 34 | 120 | | IReadOnlyList<GovernanceOutboxEntry> candidates = [.. entries.Values |
| | 30 | 121 | | .Where(entry => entry.IsRetryReady(request.UtcNow)) |
| | 6 | 122 | | .Where(entry => entry.CanBeClaimed(request.UtcNow)) |
| | 6 | 123 | | .OrderBy(entry => entry.NextRetryUtc ?? entry.UpdatedUtc) |
| | 40 | 124 | | .ThenBy(entry => entry.OutboxEntryId)]; |
| | | 125 | | |
| | 34 | 126 | | return ValueTask.FromResult<IReadOnlyList<GovernanceOutboxClaim>>(ClaimEntries(candidates, request, cancellation |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | /// <inheritdoc /> |
| | | 130 | | public ValueTask<GovernanceOutboxEntry> MarkDeliveredAsync( |
| | | 131 | | string outboxEntryId, |
| | | 132 | | GovernanceEmissionResult result, |
| | | 133 | | CancellationToken cancellationToken = default) |
| | | 134 | | { |
| | 14 | 135 | | ArgumentException.ThrowIfNullOrWhiteSpace(outboxEntryId); |
| | 14 | 136 | | ArgumentNullException.ThrowIfNull(result); |
| | 14 | 137 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 138 | | |
| | 14 | 139 | | GovernanceOutboxEntry updatedEntry = UpdateExistingEntry( |
| | 14 | 140 | | outboxEntryId, |
| | 14 | 141 | | entry => IsTerminal(entry) ? entry : entry.MarkDelivered(result), |
| | 14 | 142 | | cancellationToken); |
| | | 143 | | |
| | 14 | 144 | | return ValueTask.FromResult(updatedEntry); |
| | | 145 | | } |
| | | 146 | | |
| | | 147 | | /// <inheritdoc /> |
| | | 148 | | public ValueTask<GovernanceOutboxEntry> MarkClaimDeliveredAsync( |
| | | 149 | | GovernanceOutboxClaim claim, |
| | | 150 | | GovernanceEmissionResult result, |
| | | 151 | | CancellationToken cancellationToken = default) |
| | | 152 | | { |
| | 16 | 153 | | ArgumentNullException.ThrowIfNull(claim); |
| | 16 | 154 | | ArgumentNullException.ThrowIfNull(result); |
| | 16 | 155 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 156 | | |
| | 16 | 157 | | GovernanceOutboxEntry updatedEntry = UpdateClaimedEntry( |
| | 16 | 158 | | claim, |
| | 14 | 159 | | entry => IsTerminal(entry) ? entry : entry.MarkDelivered(result), |
| | 16 | 160 | | cancellationToken); |
| | | 161 | | |
| | 16 | 162 | | return ValueTask.FromResult(updatedEntry); |
| | | 163 | | } |
| | | 164 | | |
| | | 165 | | /// <inheritdoc /> |
| | | 166 | | public ValueTask<GovernanceOutboxEntry> MarkFailedAsync( |
| | | 167 | | string outboxEntryId, |
| | | 168 | | GovernanceEmissionError governanceEmissionError, |
| | | 169 | | DateTimeOffset? nextRetryUtc = null, |
| | | 170 | | CancellationToken cancellationToken = default) |
| | | 171 | | { |
| | 34 | 172 | | ArgumentException.ThrowIfNullOrWhiteSpace(outboxEntryId); |
| | 34 | 173 | | ArgumentNullException.ThrowIfNull(governanceEmissionError); |
| | 34 | 174 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 175 | | |
| | 34 | 176 | | GovernanceOutboxEntry updatedEntry = UpdateExistingEntry( |
| | 34 | 177 | | outboxEntryId, |
| | 34 | 178 | | entry => IsTerminal(entry) ? entry : entry.MarkFailed(governanceEmissionError, nextRetryUtc), |
| | 34 | 179 | | cancellationToken); |
| | | 180 | | |
| | 34 | 181 | | return ValueTask.FromResult(updatedEntry); |
| | | 182 | | } |
| | | 183 | | |
| | | 184 | | /// <inheritdoc /> |
| | | 185 | | public ValueTask<GovernanceOutboxEntry> MarkClaimFailedAsync( |
| | | 186 | | GovernanceOutboxClaim claim, |
| | | 187 | | GovernanceEmissionError governanceEmissionError, |
| | | 188 | | DateTimeOffset? nextRetryUtc = null, |
| | | 189 | | CancellationToken cancellationToken = default) |
| | | 190 | | { |
| | 10 | 191 | | ArgumentNullException.ThrowIfNull(claim); |
| | 10 | 192 | | ArgumentNullException.ThrowIfNull(governanceEmissionError); |
| | 10 | 193 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 194 | | |
| | 10 | 195 | | GovernanceOutboxEntry updatedEntry = UpdateClaimedEntry( |
| | 10 | 196 | | claim, |
| | 10 | 197 | | entry => IsTerminal(entry) ? entry : entry.MarkFailed(governanceEmissionError, nextRetryUtc), |
| | 10 | 198 | | cancellationToken); |
| | | 199 | | |
| | 10 | 200 | | return ValueTask.FromResult(updatedEntry); |
| | | 201 | | } |
| | | 202 | | |
| | | 203 | | /// <inheritdoc /> |
| | | 204 | | public ValueTask<GovernanceOutboxEntry> MarkDeadLetteredAsync( |
| | | 205 | | string outboxEntryId, |
| | | 206 | | GovernanceEmissionError governanceEmissionError, |
| | | 207 | | string? deadLetterReason = null, |
| | | 208 | | CancellationToken cancellationToken = default) |
| | | 209 | | { |
| | 6 | 210 | | ArgumentException.ThrowIfNullOrWhiteSpace(outboxEntryId); |
| | 6 | 211 | | ArgumentNullException.ThrowIfNull(governanceEmissionError); |
| | 6 | 212 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 213 | | |
| | 6 | 214 | | GovernanceOutboxEntry updatedEntry = UpdateExistingEntry( |
| | 6 | 215 | | outboxEntryId, |
| | 6 | 216 | | entry => IsTerminal(entry) ? entry : entry.MarkDeadLettered(governanceEmissionError, deadLetterReason), |
| | 6 | 217 | | cancellationToken); |
| | | 218 | | |
| | 6 | 219 | | return ValueTask.FromResult(updatedEntry); |
| | | 220 | | } |
| | | 221 | | |
| | | 222 | | /// <inheritdoc /> |
| | | 223 | | public ValueTask<GovernanceOutboxEntry> MarkClaimDeadLetteredAsync( |
| | | 224 | | GovernanceOutboxClaim claim, |
| | | 225 | | GovernanceEmissionError governanceEmissionError, |
| | | 226 | | string? deadLetterReason = null, |
| | | 227 | | CancellationToken cancellationToken = default) |
| | | 228 | | { |
| | 6 | 229 | | ArgumentNullException.ThrowIfNull(claim); |
| | 6 | 230 | | ArgumentNullException.ThrowIfNull(governanceEmissionError); |
| | 6 | 231 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 232 | | |
| | 6 | 233 | | GovernanceOutboxEntry updatedEntry = UpdateClaimedEntry( |
| | 6 | 234 | | claim, |
| | 6 | 235 | | entry => IsTerminal(entry) ? entry : entry.MarkDeadLettered(governanceEmissionError, deadLetterReason), |
| | 6 | 236 | | cancellationToken); |
| | | 237 | | |
| | 6 | 238 | | return ValueTask.FromResult(updatedEntry); |
| | | 239 | | } |
| | | 240 | | |
| | | 241 | | /// <inheritdoc /> |
| | | 242 | | public ValueTask<GovernanceOutboxEntry> SaveClaimAsync( |
| | | 243 | | GovernanceOutboxClaim claim, |
| | | 244 | | GovernanceOutboxEntry entry, |
| | | 245 | | CancellationToken cancellationToken = default) |
| | | 246 | | { |
| | 8 | 247 | | ArgumentNullException.ThrowIfNull(claim); |
| | 8 | 248 | | ArgumentNullException.ThrowIfNull(entry); |
| | 8 | 249 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 250 | | |
| | 8 | 251 | | if (!string.Equals(claim.OutboxEntryId, entry.OutboxEntryId, StringComparison.Ordinal)) |
| | | 252 | | { |
| | 0 | 253 | | throw new ArgumentException("Claim and entry must reference the same outbox entry ID.", nameof(entry)); |
| | | 254 | | } |
| | | 255 | | |
| | 8 | 256 | | GovernanceOutboxEntry updatedEntry = UpdateClaimedEntry( |
| | 8 | 257 | | claim, |
| | 8 | 258 | | currentEntry => IsTerminal(currentEntry) ? currentEntry : entry, |
| | 8 | 259 | | cancellationToken); |
| | | 260 | | |
| | 8 | 261 | | return ValueTask.FromResult(updatedEntry); |
| | | 262 | | } |
| | | 263 | | |
| | | 264 | | /// <inheritdoc /> |
| | | 265 | | public ValueTask<GovernanceOutboxEntry?> ReleaseClaimAsync( |
| | | 266 | | GovernanceOutboxClaim claim, |
| | | 267 | | string? reason = null, |
| | | 268 | | CancellationToken cancellationToken = default) |
| | | 269 | | { |
| | 20 | 270 | | ArgumentNullException.ThrowIfNull(claim); |
| | 18 | 271 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 272 | | |
| | 16 | 273 | | GovernanceOutboxEntry? releasedEntry = ReleaseClaim(claim, cancellationToken); |
| | | 274 | | |
| | 16 | 275 | | return ValueTask.FromResult(releasedEntry); |
| | | 276 | | } |
| | | 277 | | |
| | | 278 | | private List<GovernanceOutboxClaim> ClaimEntries( |
| | | 279 | | IReadOnlyList<GovernanceOutboxEntry> candidates, |
| | | 280 | | GovernanceOutboxClaimRequest request, |
| | | 281 | | CancellationToken cancellationToken) |
| | | 282 | | { |
| | 94 | 283 | | List<GovernanceOutboxClaim> claims = new(Math.Min(request.MaxCount, candidates.Count)); |
| | | 284 | | |
| | 294 | 285 | | foreach (GovernanceOutboxEntry candidate in candidates) |
| | | 286 | | { |
| | 54 | 287 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 288 | | |
| | 54 | 289 | | if (claims.Count >= request.MaxCount) |
| | | 290 | | { |
| | 2 | 291 | | break; |
| | | 292 | | } |
| | | 293 | | |
| | 52 | 294 | | GovernanceOutboxClaim? claim = TryClaimEntry(candidate.OutboxEntryId, request, cancellationToken); |
| | 52 | 295 | | if (claim is not null) |
| | | 296 | | { |
| | 52 | 297 | | claims.Add(claim); |
| | | 298 | | } |
| | | 299 | | } |
| | | 300 | | |
| | 94 | 301 | | return claims; |
| | | 302 | | } |
| | | 303 | | |
| | | 304 | | private GovernanceOutboxClaim? TryClaimEntry( |
| | | 305 | | string outboxEntryId, |
| | | 306 | | GovernanceOutboxClaimRequest request, |
| | | 307 | | CancellationToken cancellationToken) |
| | | 308 | | { |
| | | 309 | | while (true) |
| | | 310 | | { |
| | 52 | 311 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 312 | | |
| | 52 | 313 | | if (!entries.TryGetValue(outboxEntryId, out GovernanceOutboxEntry? currentEntry)) |
| | | 314 | | { |
| | 0 | 315 | | return null; |
| | | 316 | | } |
| | | 317 | | |
| | 52 | 318 | | if (!currentEntry.CanBeClaimed(request.UtcNow)) |
| | | 319 | | { |
| | 0 | 320 | | return null; |
| | | 321 | | } |
| | | 322 | | |
| | 52 | 323 | | GovernanceOutboxEntry claimedEntry = currentEntry.MarkClaimed( |
| | 52 | 324 | | request.WorkerId, |
| | 52 | 325 | | claimedUtc: request.UtcNow, |
| | 52 | 326 | | leaseDuration: request.LeaseDuration); |
| | | 327 | | |
| | 52 | 328 | | if (entries.TryUpdate(outboxEntryId, claimedEntry, currentEntry)) |
| | | 329 | | { |
| | 52 | 330 | | return CreateClaim(claimedEntry); |
| | | 331 | | } |
| | | 332 | | } |
| | | 333 | | } |
| | | 334 | | |
| | | 335 | | private GovernanceOutboxEntry SaveEntry( |
| | | 336 | | GovernanceOutboxEntry entry, |
| | | 337 | | CancellationToken cancellationToken) |
| | | 338 | | { |
| | | 339 | | while (true) |
| | | 340 | | { |
| | 12 | 341 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 342 | | |
| | 12 | 343 | | if (!entries.TryGetValue(entry.OutboxEntryId, out GovernanceOutboxEntry? currentEntry)) |
| | | 344 | | { |
| | 0 | 345 | | if (entries.TryAdd(entry.OutboxEntryId, entry)) |
| | | 346 | | { |
| | 0 | 347 | | return entry; |
| | | 348 | | } |
| | | 349 | | |
| | | 350 | | continue; |
| | | 351 | | } |
| | | 352 | | |
| | 12 | 353 | | if (IsTerminal(currentEntry)) |
| | | 354 | | { |
| | 2 | 355 | | return currentEntry; |
| | | 356 | | } |
| | | 357 | | |
| | 10 | 358 | | if (entries.TryUpdate(entry.OutboxEntryId, entry, currentEntry)) |
| | | 359 | | { |
| | 10 | 360 | | return entry; |
| | | 361 | | } |
| | | 362 | | } |
| | | 363 | | } |
| | | 364 | | |
| | | 365 | | private GovernanceOutboxEntry UpdateExistingEntry( |
| | | 366 | | string outboxEntryId, |
| | | 367 | | Func<GovernanceOutboxEntry, GovernanceOutboxEntry> updateEntry, |
| | | 368 | | CancellationToken cancellationToken) |
| | | 369 | | { |
| | 54 | 370 | | string normalizedOutboxEntryId = outboxEntryId.Trim(); |
| | | 371 | | |
| | | 372 | | while (true) |
| | | 373 | | { |
| | 54 | 374 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 375 | | |
| | 54 | 376 | | if (!entries.TryGetValue(normalizedOutboxEntryId, out GovernanceOutboxEntry? currentEntry)) |
| | | 377 | | { |
| | 0 | 378 | | throw new InvalidOperationException($"Outbox entry '{normalizedOutboxEntryId}' was not found."); |
| | | 379 | | } |
| | | 380 | | |
| | 54 | 381 | | GovernanceOutboxEntry updatedEntry = updateEntry(currentEntry); |
| | | 382 | | |
| | 54 | 383 | | if (ReferenceEquals(currentEntry, updatedEntry)) |
| | | 384 | | { |
| | 2 | 385 | | return currentEntry; |
| | | 386 | | } |
| | | 387 | | |
| | 52 | 388 | | if (entries.TryUpdate(normalizedOutboxEntryId, updatedEntry, currentEntry)) |
| | | 389 | | { |
| | 52 | 390 | | return updatedEntry; |
| | | 391 | | } |
| | | 392 | | } |
| | | 393 | | } |
| | | 394 | | |
| | | 395 | | private GovernanceOutboxEntry UpdateClaimedEntry( |
| | | 396 | | GovernanceOutboxClaim claim, |
| | | 397 | | Func<GovernanceOutboxEntry, GovernanceOutboxEntry> updateEntry, |
| | | 398 | | CancellationToken cancellationToken) |
| | | 399 | | { |
| | | 400 | | while (true) |
| | | 401 | | { |
| | 40 | 402 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 403 | | |
| | 40 | 404 | | if (!entries.TryGetValue(claim.OutboxEntryId, out GovernanceOutboxEntry? currentEntry)) |
| | | 405 | | { |
| | 0 | 406 | | throw new InvalidOperationException($"Outbox entry '{claim.OutboxEntryId}' was not found."); |
| | | 407 | | } |
| | | 408 | | |
| | 40 | 409 | | if (!currentEntry.IsClaimedBy(claim) || IsTerminal(currentEntry)) |
| | | 410 | | { |
| | 2 | 411 | | return currentEntry; |
| | | 412 | | } |
| | | 413 | | |
| | 38 | 414 | | GovernanceOutboxEntry updatedEntry = updateEntry(currentEntry); |
| | | 415 | | |
| | 38 | 416 | | if (ReferenceEquals(currentEntry, updatedEntry)) |
| | | 417 | | { |
| | 0 | 418 | | return currentEntry; |
| | | 419 | | } |
| | | 420 | | |
| | 38 | 421 | | if (entries.TryUpdate(claim.OutboxEntryId, updatedEntry, currentEntry)) |
| | | 422 | | { |
| | 38 | 423 | | return updatedEntry; |
| | | 424 | | } |
| | | 425 | | } |
| | | 426 | | } |
| | | 427 | | |
| | | 428 | | private GovernanceOutboxEntry? ReleaseClaim( |
| | | 429 | | GovernanceOutboxClaim claim, |
| | | 430 | | CancellationToken cancellationToken) |
| | | 431 | | { |
| | | 432 | | while (true) |
| | | 433 | | { |
| | 16 | 434 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 435 | | |
| | 16 | 436 | | if (!entries.TryGetValue(claim.OutboxEntryId, out GovernanceOutboxEntry? currentEntry)) |
| | | 437 | | { |
| | 2 | 438 | | return null; |
| | | 439 | | } |
| | | 440 | | |
| | 14 | 441 | | if (!currentEntry.IsClaimedBy(claim) || IsTerminal(currentEntry)) |
| | | 442 | | { |
| | 10 | 443 | | return currentEntry; |
| | | 444 | | } |
| | | 445 | | |
| | 4 | 446 | | GovernanceOutboxEntry releasedEntry = currentEntry.ReleaseClaim(); |
| | | 447 | | |
| | 4 | 448 | | if (entries.TryUpdate(claim.OutboxEntryId, releasedEntry, currentEntry)) |
| | | 449 | | { |
| | 4 | 450 | | return releasedEntry; |
| | | 451 | | } |
| | | 452 | | } |
| | | 453 | | } |
| | | 454 | | |
| | | 455 | | private static GovernanceOutboxClaim CreateClaim(GovernanceOutboxEntry entry) |
| | | 456 | | { |
| | 52 | 457 | | return GovernanceOutboxClaim.Create( |
| | 52 | 458 | | entry, |
| | 52 | 459 | | entry.ClaimOwner ?? throw new InvalidOperationException("Claimed entry is missing claim owner."), |
| | 52 | 460 | | entry.ClaimToken ?? throw new InvalidOperationException("Claimed entry is missing claim token."), |
| | 52 | 461 | | entry.ClaimedUtc ?? throw new InvalidOperationException("Claimed entry is missing claimed timestamp."), |
| | 52 | 462 | | entry.ClaimExpiresUtc ?? throw new InvalidOperationException("Claimed entry is missing claim expiration time |
| | | 463 | | } |
| | | 464 | | |
| | | 465 | | private static bool IsTerminal(GovernanceOutboxEntry entry) |
| | | 466 | | { |
| | 146 | 467 | | return entry.IsDelivered || entry.IsDeadLettered; |
| | | 468 | | } |
| | | 469 | | |
| | | 470 | | private static int NormalizeMaxCount(int maxCount) |
| | | 471 | | { |
| | 82 | 472 | | return maxCount <= 0 |
| | 82 | 473 | | ? throw new ArgumentOutOfRangeException(nameof(maxCount), maxCount, "Maximum count must be greater than zero |
| | 82 | 474 | | : maxCount; |
| | | 475 | | } |
| | | 476 | | } |