Skip to content
This repository has been archived by the owner on Sep 17, 2023. It is now read-only.

Commit

Permalink
Splits Publisher by introducing AsyncPublisher (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielwertheim authored Mar 10, 2021
1 parent 7f5f470 commit 2f17e22
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 26 deletions.
11 changes: 11 additions & 0 deletions src/main/MyNatsClient/IAsyncPublisher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Threading.Tasks;

namespace MyNatsClient
{
public interface IAsyncPublisher
{
Task PubAsync(string subject, string body, string replyTo = null);
Task PubAsync(string subject, ReadOnlyMemory<byte> body, string replyTo = null);
}
}
2 changes: 1 addition & 1 deletion src/main/MyNatsClient/INatsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public interface INatsClient
/// is done.
/// </summary>
/// <param name="p"></param>
Task PubManyAsync(Func<IPublisher, Task> p);
Task PubManyAsync(Func<IAsyncPublisher, Task> p);

/// <summary>
/// Async request response.
Expand Down
6 changes: 1 addition & 5 deletions src/main/MyNatsClient/IPublisher.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
using System;
using System.Threading.Tasks;

namespace MyNatsClient
{
public interface IPublisher
{
void Pub(string subject, string body, string replyTo = null);
void Pub(string subject, ReadOnlyMemory<byte> body, string replyTo = null);

Task PubAsync(string subject, string body, string replyTo = null);
Task PubAsync(string subject, ReadOnlyMemory<byte> body, string replyTo = null);
}
}
}
35 changes: 35 additions & 0 deletions src/main/MyNatsClient/Internals/AsyncPublisher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Threading.Tasks;
using MyNatsClient.Internals.Commands;

namespace MyNatsClient.Internals
{
internal class AsyncPublisher : IAsyncPublisher
{
private readonly INatsStreamWriter _writer;
private readonly int _maxPayload;

internal AsyncPublisher(INatsStreamWriter writer, int maxPayload)
{
_writer = writer;
_maxPayload = maxPayload;
}

public Task PubAsync(string subject, string body, string replyTo = null)
{
var payload = NatsEncoder.GetBytes(body);
if (payload.Length > _maxPayload)
throw NatsException.ExceededMaxPayload(_maxPayload, payload.Length);

return PubCmd.WriteAsync(_writer, subject.AsMemory(), replyTo.AsMemory(), payload);
}

public Task PubAsync(string subject, ReadOnlyMemory<byte> body, string replyTo = null)
{
if (body.Length > _maxPayload)
throw NatsException.ExceededMaxPayload(_maxPayload, body.Length);

return PubCmd.WriteAsync(_writer, subject.AsMemory(), replyTo.AsMemory(), body);
}
}
}
19 changes: 1 addition & 18 deletions src/main/MyNatsClient/Internals/Publisher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,5 @@ public void Pub(string subject, ReadOnlyMemory<byte> body, string replyTo = null

PubCmd.Write(_writer, subject, replyTo, body);
}

public Task PubAsync(string subject, string body, string replyTo = null)
{
var payload = NatsEncoder.GetBytes(body);
if (payload.Length > _maxPayload)
throw NatsException.ExceededMaxPayload(_maxPayload, payload.Length);

return PubCmd.WriteAsync(_writer, subject.AsMemory(), replyTo.AsMemory(), payload);
}

public Task PubAsync(string subject, ReadOnlyMemory<byte> body, string replyTo = null)
{
if (body.Length > _maxPayload)
throw NatsException.ExceededMaxPayload(_maxPayload, body.Length);

return PubCmd.WriteAsync(_writer, subject.AsMemory(), replyTo.AsMemory(), body);
}
}
}
}
4 changes: 2 additions & 2 deletions src/main/MyNatsClient/NatsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -467,15 +467,15 @@ public void PubMany(Action<IPublisher> p)
});
}

public async Task PubManyAsync(Func<IPublisher, Task> p)
public async Task PubManyAsync(Func<IAsyncPublisher, Task> p)
{
ThrowIfDisposed();

ThrowIfNotConnected();

await _connection.WithWriteLockAsync(async writer =>
{
await p(new Publisher(writer, _connection.ServerInfo.MaxPayload)).ConfigureAwait(false);
await p(new AsyncPublisher(writer, _connection.ServerInfo.MaxPayload)).ConfigureAwait(false);
if (ShouldAutoFlush)
await writer.FlushAsync().ConfigureAwait(false);
}).ConfigureAwait(false);
Expand Down

0 comments on commit 2f17e22

Please sign in to comment.