-
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 INatsConnectionPool interface (#109)
* Extract INatsConnectionPool interface * Make request/reply extension methods instance methods * Move NewInbox to INatsConnection * Fix formatting * Simplify INatsConnection registration * Remove explicit interface implementation * Fix NatsConnection factory delegate
- Loading branch information
Showing
7 changed files
with
241 additions
and
207 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,8 @@ | ||
namespace NATS.Client.Core; | ||
|
||
public interface INatsConnectionPool : IAsyncDisposable | ||
{ | ||
INatsConnection GetConnection(); | ||
|
||
IEnumerable<INatsConnection> GetConnections(); | ||
} |
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,120 @@ | ||
using System.Buffers; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace NATS.Client.Core; | ||
|
||
public partial class NatsConnection | ||
{ | ||
/// <inheritdoc /> | ||
public string NewInbox() => $"{InboxPrefix}{Guid.NewGuid():n}"; | ||
|
||
/// <inheritdoc /> | ||
public async ValueTask<NatsMsg<TReply?>?> RequestAsync<TRequest, TReply>( | ||
string subject, | ||
TRequest? data, | ||
NatsPubOpts? requestOpts = default, | ||
NatsSubOpts? replyOpts = default, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
var opts = SetReplyOptsDefaults(replyOpts); | ||
|
||
await using var sub = await RequestSubAsync<TRequest, TReply>(subject, data, requestOpts, opts, cancellationToken) | ||
.ConfigureAwait(false); | ||
|
||
if (await sub.Msgs.WaitToReadAsync(cancellationToken).ConfigureAwait(false)) | ||
{ | ||
if (sub.Msgs.TryRead(out var msg)) | ||
{ | ||
return msg; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async ValueTask<NatsMsg?> RequestAsync( | ||
string subject, | ||
ReadOnlySequence<byte> payload = default, | ||
NatsPubOpts? requestOpts = default, | ||
NatsSubOpts? replyOpts = default, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
var opts = SetReplyOptsDefaults(replyOpts); | ||
|
||
await using var sub = await RequestSubAsync(subject, payload, requestOpts, opts, cancellationToken).ConfigureAwait(false); | ||
|
||
if (await sub.Msgs.WaitToReadAsync(cancellationToken).ConfigureAwait(false)) | ||
{ | ||
if (sub.Msgs.TryRead(out var msg)) | ||
{ | ||
return msg; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async IAsyncEnumerable<NatsMsg<TReply?>> RequestManyAsync<TRequest, TReply>( | ||
string subject, | ||
TRequest? data, | ||
NatsPubOpts? requestOpts = default, | ||
NatsSubOpts? replyOpts = default, | ||
[EnumeratorCancellation] CancellationToken cancellationToken = default) | ||
{ | ||
await using var sub = await RequestSubAsync<TRequest, TReply>(subject, data, requestOpts, replyOpts, cancellationToken) | ||
.ConfigureAwait(false); | ||
|
||
while (await sub.Msgs.WaitToReadAsync(cancellationToken).ConfigureAwait(false)) | ||
{ | ||
while (sub.Msgs.TryRead(out var msg)) | ||
{ | ||
// Received end of stream sentinel | ||
if (msg.Data is null) | ||
{ | ||
yield break; | ||
} | ||
|
||
yield return msg; | ||
} | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async IAsyncEnumerable<NatsMsg> RequestManyAsync( | ||
string subject, | ||
ReadOnlySequence<byte> payload = default, | ||
NatsPubOpts? requestOpts = default, | ||
NatsSubOpts? replyOpts = default, | ||
[EnumeratorCancellation] CancellationToken cancellationToken = default) | ||
{ | ||
await using var sub = await RequestSubAsync(subject, payload, requestOpts, replyOpts, cancellationToken).ConfigureAwait(false); | ||
|
||
while (await sub.Msgs.WaitToReadAsync(cancellationToken).ConfigureAwait(false)) | ||
{ | ||
while (sub.Msgs.TryRead(out var msg)) | ||
{ | ||
// Received end of stream sentinel | ||
if (msg.Data.Length == 0) | ||
{ | ||
yield break; | ||
} | ||
|
||
yield return msg; | ||
} | ||
} | ||
} | ||
|
||
private NatsSubOpts SetReplyOptsDefaults(in NatsSubOpts? replyOpts) | ||
{ | ||
var opts = (replyOpts ?? default) with { MaxMsgs = 1, }; | ||
|
||
if ((opts.Timeout ?? default) == default) | ||
{ | ||
opts = opts with { Timeout = Options.RequestTimeout }; | ||
} | ||
|
||
return opts; | ||
} | ||
} |
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.