-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add flags to NatsMsg and JetStream Request (#652)
* Add flags to NatsMsg * Code comments for msg size * Format * Fix warnings * Keep 503 header * Format * JetStream TryJSRequestAsync * Format * Fix tests * Refactor JetStream method results to use NatsResult * Refactor NatsMsg size and flags handling in tests and class * Refactor JSON document deserialization * Refactor NatsJSContext error handling logic. Reordered error checks in `NatsJSContext.cs` to prioritize handling `HasNoResponders` and `null` data conditions early. Modified flag checks in `NatsMsg.cs` for `IsEmpty` and `HasNoResponders` by directly using bitwise operations instead of enum comparisons. * Fix memory leak by disposing JsonDocument properly * Skip slow tests for unique NUID generation
- Loading branch information
Showing
10 changed files
with
374 additions
and
89 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
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,38 @@ | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace NATS.Client.Core; | ||
|
||
public readonly struct NatsResult<T> | ||
{ | ||
private readonly T? _value; | ||
private readonly Exception? _error; | ||
|
||
public NatsResult(T value) | ||
{ | ||
_value = value; | ||
_error = null; | ||
} | ||
|
||
public NatsResult(Exception error) | ||
{ | ||
_value = default; | ||
_error = error; | ||
} | ||
|
||
public T Value => _value ?? ThrowValueIsNotSetException(); | ||
|
||
public Exception Error => _error ?? ThrowErrorIsNotSetException(); | ||
|
||
public bool Success => _error == null; | ||
|
||
public static implicit operator NatsResult<T>(T value) => new(value); | ||
|
||
public static implicit operator NatsResult<T>(Exception error) => new(error); | ||
|
||
private static T ThrowValueIsNotSetException() => throw CreateInvalidOperationException("Result value is not set"); | ||
|
||
private static Exception ThrowErrorIsNotSetException() => throw CreateInvalidOperationException("Result error is not set"); | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private static Exception CreateInvalidOperationException(string message) => new InvalidOperationException(message); | ||
} |
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
41 changes: 0 additions & 41 deletions
41
src/NATS.Client.JetStream/Internal/NatsJSErrorAwareJsonSerializer.cs
This file was deleted.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
src/NATS.Client.JetStream/Internal/NatsJSJsonDocumentSerializer.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,12 @@ | ||
using System.Buffers; | ||
using System.Text.Json; | ||
using NATS.Client.Core; | ||
|
||
namespace NATS.Client.JetStream.Internal; | ||
|
||
internal sealed class NatsJSJsonDocumentSerializer : INatsDeserialize<JsonDocument> | ||
{ | ||
public static readonly NatsJSJsonDocumentSerializer Default = new(); | ||
|
||
public JsonDocument? Deserialize(in ReadOnlySequence<byte> buffer) => buffer.Length == 0 ? default : JsonDocument.Parse(buffer); | ||
} |
Oops, something went wrong.