Skip to content
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

open Header.Writer class and create GetBytesLength method #674

Merged
merged 4 commits into from
Nov 13, 2024

Conversation

SiuTung08
Copy link
Contributor

@SiuTung08 SiuTung08 commented Nov 9, 2024

Open Header.Writer class and create GetBytesLength method for calculating NatsHeaders size (length in bytes) up front to call Publish for large Nats message which may exceeds payload size against server's maximum payload size.

@mtmk
Copy link
Collaborator

mtmk commented Nov 11, 2024

@SiuTung08 should we provide a convenience method on the header class so you can call it like headers.GetBytesLength()?

@mtmk mtmk added the hackathon label Nov 11, 2024
@SiuTung08
Copy link
Contributor Author

@SiuTung08 should we provide a convenience method on the header class so you can call it like headers.GetBytesLength()?

Make sense! Just pushed the commit for this.

@SiuTung08
Copy link
Contributor Author

@mtmk looks like the test jobs for Linux (latest) failed. Looking at the actual results though it seems that all tests have passed. Can we retrigger it?

/// </summary>
/// <param name="headerWriter">an instance of headerWriter</param>
/// <returns>bytes length of th header</returns>
public long GetBytesLength(HeaderWriter headerWriter)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just thinking about it from the application developer standpoint: is there a way we can make this taking an optional encoding rather than the writer? maybe in that case we won't even need to expose a writer? e.g. have an internal static GrtBytesLength(Encoding) method on the writer where we can reuse here?

public long GetBytesLength(Encoding? encoding = null)
{
    // e.g. if null set to utf-8

what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mtmk I agree, I will make some changes.

@mtmk mtmk self-assigned this Nov 12, 2024
Copy link
Collaborator

@mtmk mtmk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks @SiuTung08

@mtmk mtmk merged commit 26ffb37 into nats-io:main Nov 13, 2024
10 checks passed
throw new NatsException(
$"Invalid header key '{kv.Key}': contains colon, space, or other non-printable ASCII characters");
}
throw new NatsException($"Invalid header key '{kv.Key}': contains colon, space, or other non-printable ASCII characters");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. @mtmk correct me if I'm off here, do we prefer including braces?
  2. If possible, this would be a good opportunity to wrap this throw into a helper method, e.x.
if (!ValidateKey(keySpan.Slice(0, keyLength)))
{
    NatsException.Throw($"Invalid header key '{kv.Key}': contains colon, space, or other non-printable ASCII characters")
}

public class NatsException : Exception
{
   //etc
   [MethodImpl(MethodImplOptions.NoInlining)]
   public static void Throw(string message)
   {
      throw new NatsException(message);
   }
   //etc
}

(There may be a throwhelper already floating around, if so prefer that)

Main advantage of such is we can get some better inlining/opts and avoid certain callstack checks on happy path....

Copy link
Collaborator

@mtmk mtmk Nov 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ouch just merged it. I shall open a new PR

EDIT

mtmk added a commit that referenced this pull request Nov 13, 2024
* Add OnSocketAvailableAsync Hook (#647)
* Refactor exception handling in NatsException (#677)
* Fixed an exception which happens when PutAsync is used more than once and activity logging is enabled in main project (#675)
* open Header.Writer class and create GetBytesLength method (#674)
* Fix date time serialization to use ISO8601 (#664)
@mtmk mtmk mentioned this pull request Nov 13, 2024
mtmk added a commit that referenced this pull request Nov 13, 2024
* Add OnSocketAvailableAsync Hook (#647)
* Refactor exception handling in NatsException (#677)
* Fixed an exception which happens when PutAsync is used more than once and activity logging is enabled in main project (#675)
* open Header.Writer class and create GetBytesLength method (#674)
* Fix date time serialization to use ISO8601 (#664)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose a public API for getting NatsHeaders in the byte array (serialization)
3 participants