This repository has been archived by the owner on Sep 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Splits Publisher by introducing AsyncPublisher (#73)
- Loading branch information
1 parent
7f5f470
commit 2f17e22
Showing
6 changed files
with
51 additions
and
26 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
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); | ||
} | ||
} |
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 |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
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,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); | ||
} | ||
} | ||
} |
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