Skip to content

Commit 2e982fc

Browse files
Expose tcp nodelay option on crt sockets (#1007)
1 parent 6a54aef commit 2e982fc

6 files changed

Lines changed: 76 additions & 6 deletions

File tree

crt/aws-c-s3

src/main/java/software/amazon/awssdk/crt/io/SocketOptions.java

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,34 @@ int getValue() {
6666
}
6767
}
6868

69+
/**
70+
* Controls the TCP_NODELAY option (Nagle's algorithm). TCP only; ignored for UDP and LOCAL sockets.
71+
*/
72+
public enum TcpNoDelay {
73+
/**
74+
* Leaves the OS default in place (Nagle's algorithm typically enabled).
75+
*/
76+
DEFAULT(0),
77+
/**
78+
* Disables Nagle's algorithm, sending small writes immediately.
79+
*/
80+
ON(1),
81+
/**
82+
* Explicitly enables Nagle's algorithm.
83+
*/
84+
OFF(2);
85+
86+
private int tcpNoDelay;
87+
88+
TcpNoDelay(int val) {
89+
tcpNoDelay = val;
90+
}
91+
92+
int getValue() {
93+
return tcpNoDelay;
94+
}
95+
}
96+
6997
/**
7098
* Sets the socket domain
7199
*/
@@ -100,6 +128,14 @@ int getValue() {
100128
*/
101129
public boolean keepAlive = false;
102130

131+
/**
132+
* Controls the TCP_NODELAY option (Nagle's algorithm).
133+
* Defaults to {@link TcpNoDelay#ON}.
134+
* Set to {@link TcpNoDelay#ON} to disable Nagle's algorithm (send small writes immediately),
135+
* or {@link TcpNoDelay#OFF} to explicitly enable it. TCP only; ignored for UDP and LOCAL sockets.
136+
*/
137+
public TcpNoDelay tcpNoDelay = TcpNoDelay.ON;
138+
103139
/**
104140
* Creates a new set of socket options
105141
*/
@@ -152,7 +188,8 @@ public long getNativeHandle() {
152188
keepAliveIntervalSecs,
153189
keepAliveTimeoutSecs,
154190
keepAliveMaxFailedProbes,
155-
keepAlive
191+
keepAlive,
192+
tcpNoDelay.getValue()
156193
));
157194
}
158195
return super.getNativeHandle();
@@ -179,7 +216,7 @@ protected void releaseNativeHandle() {
179216
* native methods
180217
******************************************************************************/
181218
private static native long socketOptionsNew(
182-
int domain, int type, int connectTimeoutMs, int keepAliveIntervalSecs, int keepAliveTimeoutSecs, int keepAliveMaxFailedProbes, boolean keepAlive);
219+
int domain, int type, int connectTimeoutMs, int keepAliveIntervalSecs, int keepAliveTimeoutSecs, int keepAliveMaxFailedProbes, boolean keepAlive, int tcpNoDelay);
183220

184221
private static native void socketOptionsDestroy(long elg);
185222
};

src/native/socket_options.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ jlong JNICALL Java_software_amazon_awssdk_crt_io_SocketOptions_socketOptionsNew(
3232
jint keep_alive_interval_secs,
3333
jint keep_alive_timeout_secs,
3434
jint keep_alive_max_failed_probes,
35-
jboolean keep_alive) {
35+
jboolean keep_alive,
36+
jint tcp_nodelay) {
3637
(void)jni_class;
3738
aws_cache_jni_ids(env);
3839

@@ -48,6 +49,7 @@ jlong JNICALL Java_software_amazon_awssdk_crt_io_SocketOptions_socketOptionsNew(
4849
options->keep_alive_timeout_sec = (uint16_t)keep_alive_timeout_secs;
4950
options->keep_alive_max_failed_probes = (uint16_t)keep_alive_max_failed_probes;
5051
options->keepalive = keep_alive;
52+
options->tcp_nodelay = (enum aws_socket_tcp_nodelay)tcp_nodelay;
5153

5254
return (jlong)options;
5355
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0.
4+
*/
5+
package software.amazon.awssdk.crt.test;
6+
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
10+
import software.amazon.awssdk.crt.io.SocketOptions;
11+
12+
public class SocketOptionsTest extends CrtTestFixture {
13+
14+
@Test
15+
public void testSocketOptionsDefaults() {
16+
try (SocketOptions options = new SocketOptions()) {
17+
Assert.assertEquals(SocketOptions.TcpNoDelay.ON, options.tcpNoDelay);
18+
Assert.assertNotEquals(0, options.getNativeHandle());
19+
}
20+
}
21+
22+
@Test
23+
public void testSocketOptionsTcpNoDelay() {
24+
for (SocketOptions.TcpNoDelay tcpNoDelay : SocketOptions.TcpNoDelay.values()) {
25+
try (SocketOptions options = new SocketOptions()) {
26+
options.tcpNoDelay = tcpNoDelay;
27+
Assert.assertNotEquals(0, options.getNativeHandle());
28+
}
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)