Skip to content

Commit

Permalink
add TCP No Delay option fixes Issue #2324 (#2341)
Browse files Browse the repository at this point in the history
  • Loading branch information
davecramer committed Nov 16, 2021
1 parent b83f0b3 commit 2a8ecbe
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -119,6 +119,7 @@ In addition to the standard connection parameters the driver supports a number o
| connectTimeout | Integer | 10 | The timeout value used for socket connect operations. |
| socketTimeout | Integer | 0 | The timeout value used for socket read operations. |
| tcpKeepAlive | Boolean | false | Enable or disable TCP keep-alive. |
| tcpNoDelay | Boolean | false | Enable or disable TCP no delay. |
| ApplicationName | String | PostgreSQL JDBC Driver | The application name (require server version >= 9.0). If assumeMinServerVersion is set to >= 9.0 this will be sent in the startup packets, otherwise after the connection is made |
| readOnly | Boolean | false | Puts this connection in read-only mode |
| disableColumnSanitiser | Boolean | false | Enable optimization that disables column name sanitiser |
Expand Down
4 changes: 4 additions & 0 deletions docs/documentation/head/connect.md
Expand Up @@ -342,6 +342,10 @@ Connection conn = DriverManager.getConnection(url);

Enable or disable TCP keep-alive probe. The default is `false`.

* **tcpNoDelay** = boolean

Enable or disable TCP nodelay. The default is `false`.

* **unknownLength** = int

Certain postgresql types such as `TEXT` do not have a well defined length.
Expand Down
5 changes: 5 additions & 0 deletions pgjdbc/src/main/java/org/postgresql/PGProperty.java
Expand Up @@ -698,6 +698,11 @@ public enum PGProperty {
"false",
"Enable or disable TCP keep-alive. The default is {@code false}."),

TCP_NO_DELAY(
"tcpNoDelay",
"false",
"Enable or disable TCP no delay. The default is (@code false}."
),
/**
* Specifies the length to return for types of unknown length.
*/
Expand Down
3 changes: 3 additions & 0 deletions pgjdbc/src/main/java/org/postgresql/core/PGStream.java
Expand Up @@ -113,6 +113,7 @@ public PGStream(PGStream pgStream, int timeout ) throws IOException {
int receiveBufferSize = 1024;
int soTimeout = 0;
boolean keepAlive = false;
boolean tcpNoDelay = false;

/*
Get the existing values before closing the stream
Expand All @@ -122,6 +123,7 @@ public PGStream(PGStream pgStream, int timeout ) throws IOException {
receiveBufferSize = pgStream.getSocket().getReceiveBufferSize();
soTimeout = pgStream.getSocket().getSoTimeout();
keepAlive = pgStream.getSocket().getKeepAlive();
tcpNoDelay = pgStream.getSocket().getTcpNoDelay();

} catch ( SocketException ex ) {
// ignore it
Expand All @@ -140,6 +142,7 @@ public PGStream(PGStream pgStream, int timeout ) throws IOException {
socket.setSendBufferSize(sendBufferSize);
setNetworkTimeout(soTimeout);
socket.setKeepAlive(keepAlive);
socket.setTcpNoDelay(tcpNoDelay);

int2Buf = new byte[2];
int4Buf = new byte[4];
Expand Down
Expand Up @@ -114,6 +114,10 @@ private PGStream tryConnect(String user, String database,
boolean requireTCPKeepAlive = PGProperty.TCP_KEEP_ALIVE.getBoolean(info);
newStream.getSocket().setKeepAlive(requireTCPKeepAlive);

// Enable TCP no delay if required
boolean requireTCPNoDelay = PGProperty.TCP_NO_DELAY.getBoolean(info);
newStream.getSocket().setTcpNoDelay(requireTCPNoDelay);

// Try to set SO_SNDBUF and SO_RECVBUF socket options, if requested.
// If receiveBufferSize and send_buffer_size are set to a value greater
// than 0, adjust. -1 means use the system default, 0 is ignored since not
Expand Down
16 changes: 16 additions & 0 deletions pgjdbc/src/main/java/org/postgresql/ds/common/BaseDataSource.java
Expand Up @@ -815,6 +815,22 @@ public boolean getTcpKeepAlive() {
return PGProperty.TCP_KEEP_ALIVE.getBoolean(properties);
}

/**
* @param enabled if TCP no delay should be enabled
* @see PGProperty#TCP_NO_DELAY
*/
public void setTcpNoDelay( boolean enabled ) {
PGProperty.TCP_NO_DELAY.set(properties,enabled);
}

/**
* @return true if TCP no delay is enabled
* @see PGProperty#TCP_NO_DELAY
*/
public boolean getTcpNoDelay() {
return PGProperty.TCP_NO_DELAY.getBoolean( properties );
}

/**
* @param enabled if binary transfer should be enabled
* @see PGProperty#BINARY_TRANSFER
Expand Down

0 comments on commit 2a8ecbe

Please sign in to comment.