Skip to content

NetworkTarget - Introduced option NoDelay to disable delayed ACK#5779

Merged
snakefoot merged 1 commit intoNLog:devfrom
snakefoot:NoDelay
Apr 12, 2025
Merged

NetworkTarget - Introduced option NoDelay to disable delayed ACK#5779
snakefoot merged 1 commit intoNLog:devfrom
snakefoot:NoDelay

Conversation

@snakefoot
Copy link
Contributor

Resolves #5778

@coderabbitai
Copy link

coderabbitai bot commented Apr 12, 2025

Walkthrough

This pull request introduces a new noDelay parameter to the network sender creation methods. The parameter is added to the interface and its implementations, allowing TCP NODELAY to be configured. An internal NoDelay property is now used in the TCP sender to adjust the socket’s behavior in the CreateSocket method. Additionally, the logging target now includes a configurable NoDelay setting (defaulting to true) and an updated default for the send timeout. Corresponding tests have been updated and a new test for configuration has been added.

Changes

File(s) Change Summary
src/NLog.Targets.Network/NetworkSenders/INetworkSenderFactory.cs
src/NLog.Targets.Network/NetworkSenders/NetworkSenderFactory.cs
tests/NLog.Targets.Network.Tests/NetworkSenders/HttpNetworkSenderTests.cs
tests/NLog.Targets.Network.Tests/NetworkTargetTests.cs
Updated the Create method signature to include a new NetworkTarget parameter; updated mocks and test calls accordingly.
src/NLog.Targets.Network/NetworkSenders/TcpNetworkSender.cs Added an internal NoDelay property and modified the CreateSocket method to set the socket’s NoDelay flag based on this property.
src/NLog.Targets.Network/Targets/NetworkTarget.cs Added a configurable NoDelay property (defaulting to true) and set a default value of 100 for SendTimeoutSeconds, updating its documentation.

Sequence Diagram(s)

sequenceDiagram
    participant NT as NetworkTarget
    participant NSF as NetworkSenderFactory
    participant TS as TcpNetworkSender
    participant Sock as UnderlyingSocket

    NT->>NSF: Create(url, ..., noDelay)
    NSF->>TS: Create(url, ..., noDelay)
    TS->>TS: Set NoDelay property
    TS->>Sock: CreateSocket() and apply NoDelay setting
Loading

Assessment against linked issues

Objective Addressed Explanation
Implement configurable Socket.NoDelay parameter to disable TCP_NODELAY [#5778]

Poem

In a burrow under code so deep,
I hopped along with changes to keep,
A new noDelay flag to make things fast,
TCP_NODELAY set, delays a thing of the past!
Carrots and sockets, a bunny's delight,
Hoppy coding days and debug nights!
🥕🐰

Tip

⚡💬 Agentic Chat (Pro Plan, General Availability)
  • We're introducing multi-step agentic chat in review comments and issue comments, within and outside of PR's. This feature enhances review and issue discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments and add commits to existing pull requests.

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 533aa20 and fa0fa7e.

📒 Files selected for processing (7)
  • src/NLog.Targets.Network/NetworkSenders/INetworkSenderFactory.cs (1 hunks)
  • src/NLog.Targets.Network/NetworkSenders/NetworkSenderFactory.cs (5 hunks)
  • src/NLog.Targets.Network/NetworkSenders/TcpNetworkSender.cs (2 hunks)
  • src/NLog.Targets.Network/Targets/NetworkTarget.cs (2 hunks)
  • tests/NLog.Targets.Network.Tests/NetworkSenders/HttpNetworkSenderTests.cs (3 hunks)
  • tests/NLog.Targets.Network.Tests/NetworkSenders/TcpNetworkSenderTests.cs (2 hunks)
  • tests/NLog.Targets.Network.Tests/NetworkTargetTests.cs (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • tests/NLog.Targets.Network.Tests/NetworkSenders/HttpNetworkSenderTests.cs
  • src/NLog.Targets.Network/NetworkSenders/TcpNetworkSender.cs
  • tests/NLog.Targets.Network.Tests/NetworkSenders/TcpNetworkSenderTests.cs
🧰 Additional context used
🧬 Code Graph Analysis (3)
src/NLog.Targets.Network/NetworkSenders/NetworkSenderFactory.cs (7)
src/NLog.Targets.Network/NetworkSenders/INetworkSenderFactory.cs (1)
  • QueuedNetworkSender (54-54)
tests/NLog.Targets.Network.Tests/NetworkTargetTests.cs (1)
  • QueuedNetworkSender (1374-1379)
src/NLog.Targets.Network/NetworkSenders/QueuedNetworkSender.cs (2)
  • QueuedNetworkSender (44-328)
  • QueuedNetworkSender (73-76)
src/NLog.Targets.Network/Targets/NetworkTarget.cs (3)
  • X509Certificate2Collection (690-737)
  • NetworkTarget (93-96)
  • NetworkTarget (105-108)
src/NLog.Targets.Network/NetworkSenders/TcpNetworkSender.cs (2)
  • TcpNetworkSender (46-347)
  • TcpNetworkSender (58-63)
src/NLog.Targets.Network/NetworkSenders/UdpNetworkSender.cs (2)
  • UdpNetworkSender (45-218)
  • UdpNetworkSender (57-62)
src/NLog.Targets.Network/NetworkSenders/HttpNetworkSender.cs (2)
  • HttpNetworkSender (44-164)
  • HttpNetworkSender (58-62)
tests/NLog.Targets.Network.Tests/NetworkTargetTests.cs (4)
src/NLog.Targets.Network/Targets/NetworkTarget.cs (3)
  • NetworkTarget (93-96)
  • NetworkTarget (105-108)
  • X509Certificate2Collection (690-737)
src/NLog.Targets.Network/NetworkSenders/INetworkSenderFactory.cs (1)
  • QueuedNetworkSender (54-54)
src/NLog.Targets.Network/NetworkSenders/NetworkSenderFactory.cs (1)
  • QueuedNetworkSender (49-146)
src/NLog.Targets.Network/NetworkSenders/QueuedNetworkSender.cs (2)
  • QueuedNetworkSender (44-328)
  • QueuedNetworkSender (73-76)
src/NLog.Targets.Network/NetworkSenders/INetworkSenderFactory.cs (1)
src/NLog.Targets.Network/Targets/NetworkTarget.cs (3)
  • X509Certificate2Collection (690-737)
  • NetworkTarget (93-96)
  • NetworkTarget (105-108)
🔇 Additional comments (16)
src/NLog.Targets.Network/Targets/NetworkTarget.cs (3)

254-257: Good improvement to documentation and default timeout.

Adding a default timeout value of 100 seconds and clarifying the behavior when set to 0 makes the API more user-friendly and prevents potential issues with indefinite blocking.


259-262: Good addition of NoDelay property to optimize network performance.

The new NoDelay property allows disabling the delayed ACK timer (Nagle's algorithm) which can reduce latency for small packets. Setting it to true by default is a good choice for most logging scenarios where minimizing latency is typically preferred over bandwidth optimization.


652-652: Good refactoring to simplify method call.

The method now passes the entire NetworkTarget object instead of individual parameters, which makes the code more maintainable and less prone to parameter-related errors when new properties are added.

src/NLog.Targets.Network/NetworkSenders/NetworkSenderFactory.cs (9)

49-50: Good API simplification.

Changing the method signature to accept a NetworkTarget instead of multiple parameters makes the API more maintainable and extensible.


55-60: Properly propagating the NoDelay property to TcpNetworkSender.

The NoDelay property is now correctly passed from the NetworkTarget to the TcpNetworkSender, along with other configuration properties.


69-74: Consistent property propagation for tcp4 implementation.

The changes maintain consistency across different TCP implementations by applying the same properties.


83-88: Consistent property propagation for tcp6 implementation.

The changes ensure the tcp6 implementation gets the same property values as tcp4 and standard tcp.


97-99: Appropriate property propagation for UDP implementation.

The UDP implementation correctly receives only the relevant properties from the NetworkTarget.


107-109: Consistent property propagation for udp4 implementation.

The changes maintain consistency with other UDP implementations.


117-119: Consistent property propagation for udp6 implementation.

The changes ensure the udp6 implementation gets the same property values as udp4 and standard udp.


127-129: Appropriate property propagation for HTTP implementation.

The HTTP implementation correctly receives only the relevant properties from the NetworkTarget.


138-140: Consistent property propagation for HTTPS implementation.

The changes maintain consistency with the HTTP implementation.

src/NLog.Targets.Network/NetworkSenders/INetworkSenderFactory.cs (2)

50-51: Updated parameter documentation to reflect API changes.

Documentation correctly describes the new networkTarget parameter.


54-54: Good API simplification.

The interface method signature is now simplified, making it more maintainable when adding new network target properties.

tests/NLog.Targets.Network.Tests/NetworkTargetTests.cs (2)

1281-1298: Good test coverage for the new NoDelay property.

The test correctly validates that the NoDelay property can be configured through XML configuration with both true and false values.


1374-1379: Updated test factory implementation to match interface changes.

The MyQueudSenderFactory implementation now properly matches the updated INetworkSenderFactory interface, allowing tests to continue functioning correctly with the new API.


🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Caution

Inline review comments failed to post. This is likely due to GitHub's limits when posting large numbers of comments. If you are seeing this consistently it is likely a permissions issue. Please check "Moderation" -> "Code review limits" under your organization settings.

Actionable comments posted: 3

🧹 Nitpick comments (1)
src/NLog.Targets.Network/NetworkSenders/INetworkSenderFactory.cs (1)

56-56: Consider enhancing the parameter documentation.

While the documentation includes the parameter, it only mentions "TCP_NODELAY" without explaining its purpose. Consider adding more context about what this socket option does (disables Nagle's algorithm) and its effect (reduces latency by sending packets immediately).

-/// <param name="noDelay">TCP_NODELAY</param>
+/// <param name="noDelay">When true, disables the Nagle algorithm by setting TCP_NODELAY, which reduces latency by sending packets immediately rather than waiting to build a larger packet.</param>
🛑 Comments failed to post (3)
src/NLog.Targets.Network/NetworkSenders/NetworkSenderFactory.cs (1)

49-49: ⚠️ Potential issue

Updated Create method signature with noDelay parameter

The method signature has been updated to include the noDelay parameter, matching the interface. However, the implementation doesn't set the NoDelay property for the TcpNetworkSender instances it creates.

The code should be updated to pass the noDelay value to the TcpNetworkSenders created in the method. Here's the fix for the first case (other cases need similar changes):

return new TcpNetworkSender(url, AddressFamily.Unspecified)
{
    MaxQueueSize = maxQueueSize,
    OnQueueOverflow = onQueueOverflow,
    SslProtocols = sslProtocols,
    KeepAliveTime = keepAliveTime,
    SendTimeout = sendTimeout,
    SslCertificateOverride = sslCertificateOverride,
+   NoDelay = noDelay,
};
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

public QueuedNetworkSender Create(string url, int maxQueueSize, 
                                  NetworkTargetQueueOverflowAction onQueueOverflow, 
                                  int maxQueueSize, 
                                  int maxMessageSize, 
                                  System.Security.Authentication.SslProtocols sslProtocols, 
                                  X509Certificate2Collection sslCertificateOverride, 
                                  TimeSpan keepAliveTime, 
                                  TimeSpan sendTimeout, 
                                  bool noDelay)
{
    return new TcpNetworkSender(url, AddressFamily.Unspecified)
    {
        MaxQueueSize = maxQueueSize,
        OnQueueOverflow = onQueueOverflow,
        SslProtocols = sslProtocols,
        KeepAliveTime = keepAliveTime,
        SendTimeout = sendTimeout,
        SslCertificateOverride = sslCertificateOverride,
        NoDelay = noDelay,
    };
}
tests/NLog.Targets.Network.Tests/NetworkSenders/HttpNetworkSenderTests.cs (2)

138-138: 🛠️ Refactor suggestion

Updated second test assertion with noDelay parameter

Similar to the previous comment, the noDelay value should be true for consistency with the default in NetworkTarget.

-networkSenderFactoryMock.Received(1).Create("http://test.with.mock", 1234, NetworkTargetQueueOverflowAction.Block, 0, SslProtocols.None, null, TimeSpan.Zero, TimeSpan.Zero, false); // Only created one HttpNetworkSender
+networkSenderFactoryMock.Received(1).Create("http://test.with.mock", 1234, NetworkTargetQueueOverflowAction.Block, 0, SslProtocols.None, null, TimeSpan.Zero, TimeSpan.Zero, true); // Only created one HttpNetworkSender
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

            networkSenderFactoryMock.Received(1).Create("http://test.with.mock", 1234, NetworkTargetQueueOverflowAction.Block, 0, SslProtocols.None, null, TimeSpan.Zero, TimeSpan.Zero, true); // Only created one HttpNetworkSender

92-92: 🛠️ Refactor suggestion

Updated test assertion with the new noDelay parameter

The test assertion has been updated to include the noDelay parameter, but it's using false instead of the default value true set in the NetworkTarget class. This should be updated for consistency unless there's a specific reason to test with NoDelay disabled.

-networkSenderFactoryMock.Received(1).Create("http://test.with.mock", 1234, NetworkTargetQueueOverflowAction.Block, 0, SslProtocols.None, null, TimeSpan.Zero, TimeSpan.Zero, false);
+networkSenderFactoryMock.Received(1).Create("http://test.with.mock", 1234, NetworkTargetQueueOverflowAction.Block, 0, SslProtocols.None, null, TimeSpan.Zero, TimeSpan.Zero, true);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

            networkSenderFactoryMock.Received(1).Create("http://test.with.mock", 1234, NetworkTargetQueueOverflowAction.Block, 0, SslProtocols.None, null, TimeSpan.Zero, TimeSpan.Zero, true);

@snakefoot snakefoot enabled auto-merge (squash) April 12, 2025 13:28
@sonarqubecloud
Copy link

@snakefoot snakefoot added new default (breaking) Kind of Breaking behavior change breaking behavior change Same API, different result labels Apr 12, 2025
@snakefoot snakefoot merged commit f92ae05 into NLog:dev Apr 12, 2025
6 checks passed
@snakefoot
Copy link
Contributor Author

@snakefoot snakefoot added the documentation done all docs done (wiki, api docs, lists on nlog-project.org, xmldocs) label Jun 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking behavior change Same API, different result documentation done all docs done (wiki, api docs, lists on nlog-project.org, xmldocs) feature needs documentation on wiki network/NLogViewer-target new default (breaking) Kind of Breaking behavior change size/L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NetworkTarget with Socket.NoDelay to disable TCP_NODELAY

1 participant