-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose a public API for getting NatsHeaders in the byte array (serialization) #638
Comments
we can open HeaderWriter that's what we're using internally
example usage: nats.net/tests/NATS.Client.CoreUnit.Tests/NatsHeaderTest.cs Lines 40 to 43 in c84f216
|
I want to publish a message with data (payload) size close to the But the publish fails with The headers size is calculated with: nats.net/tests/NATS.Client.Core.Tests/NatsHeaderTest.cs Lines 40 to 44 in a163445
I based my assumption on these lines: nats.net/src/NATS.Client.Core/Commands/CommandWriter.cs Lines 288 to 292 in a163445
This is the test:
And this is implementation of the NatsHeadersSizeInBytes method:
Stack trace of the failure
I just curious, how such much exceeding number of bytes (1398091-1048576 = 349515) is computed? Note: I made available accessibility of HeaderWriter class for this test. This is dev environment:
|
thanks for the code. I wonder in your example This is what I was able to try, it worked as expected for me: using System.Text;
using NATS.Client.Core;
using NATS.Client.Core.Internal;
var nc = new NatsConnection();
await nc.ConnectAsync();
var max = nc.ServerInfo!.MaxPayload;
var headers = new NatsHeaders { { "key1", "value1" }, { "key2", "value2" } };
var buffer = new NatsBufferWriter<byte>();
var headersLength = new HeaderWriter(Encoding.ASCII).Write(buffer, headers);
var payloadLength = max - headersLength;
Console.WriteLine($"headers length: {headersLength}");
Console.WriteLine($"payload length: {payloadLength}");
Console.WriteLine($" server max: {max}");
await nc.PublishAsync("foo", new byte[payloadLength], headers: headers);
Console.WriteLine("OK");
try
{
await nc.PublishAsync("foo", new byte[payloadLength + 1], headers: headers);
Console.WriteLine("OK");
}
catch (NatsException ex)
{
Console.WriteLine(ex.Message);
}
// Output:
// headers length: 40
// payload length: 1048536
// server max: 1048576
// OK
// Payload size 1048577 exceeds server's maximum payload size 1048576 running against server with no config:
|
The generated maxAllowedPayload is actually small to the headersSizeInBytes amount. It's assured here:
Using the same encoding for the payload and the headers (ASCII) resulted to the same error:
|
what is the type of |
The much exceeding number of bytes in payload issue was in serialization put in connection NatsOpts
I was mislead with such behavior. My other tests which have mix of publishing objects and small texts were fine. But when I wanted to examine maxPayload - headers overhead I faced with this issue. So, below code reproduces the issue.
Output: P.S: we still need that |
Currently working on it - we should be able to make the HeaderWrite class public and create a method for getting the header size under it. |
Proposed change
Such kind of methods :
public byte[] NatsHeaders.SerializeToArray()
orpublic byte[] NatsHeaders.GetSerialized()
- Returns the byte array in a serialized version (ASCII or any other encoding).public int SerializedLength()
- Returns the number of bytes in a serialized version (ASCII or any other encoding).Use case
Need to calculate/take into account NatsHeaders size (length in bytes) up front to call
Publish
for large Nats message which exceeds payload size against server's maximum payload size.Contribution
No response
The text was updated successfully, but these errors were encountered: