Skip to content

Commit

Permalink
perf: use low latency TCP options (#414)
Browse files Browse the repository at this point in the history
* perf: use low latency TCP options

PGAdapter will normally be used as a local proxy that will prefer a low
TCP latency over throughput. These settings reduce the latency
significantly for small requests that are typical for the PG wire
protocol.

* docs: add comments
  • Loading branch information
olavloite committed Oct 21, 2022
1 parent ca79476 commit 684506a
Showing 1 changed file with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ void runTcpServer(
this.localPort == 0 ? this.options.getProxyPort() : this.localPort,
this.options.getMaxBacklog(),
address);
// Optimize for latency (2), then bandwidth (1) and then connection time (0).
tcpSocket.setPerformancePreferences(0, 2, 1);
this.serverSockets.add(tcpSocket);
this.localPort = tcpSocket.getLocalPort();
tcpStartedLatch.countDown();
Expand All @@ -254,6 +256,8 @@ void runDomainSocketServer(CountDownLatch startupLatch, CountDownLatch stoppedLa
}
AFUNIXServerSocket domainSocket = AFUNIXServerSocket.newInstance();
domainSocket.bind(AFUNIXSocketAddress.of(tempDir), this.options.getMaxBacklog());
// Optimize for latency (2), then bandwidth (1) and then connection time (0).
domainSocket.setPerformancePreferences(0, 2, 1);
this.serverSockets.add(domainSocket);
runServer(domainSocket, startupLatch, stoppedLatch);
} catch (SocketException socketException) {
Expand Down Expand Up @@ -297,7 +301,11 @@ void runServer(
* @throws SpannerException if the {@link ConnectionHandler} is unable to connect to Cloud Spanner
* or if the dialect of the database is not PostgreSQL.
*/
void createConnectionHandler(Socket socket) {
void createConnectionHandler(Socket socket) throws SocketException {
// Optimize for latency (2), then bandwidth (1) and then connection time (0).
socket.setPerformancePreferences(0, 2, 1);
// Turn on TCP_NODELAY to optimize for chatty protocol that prefers low latency.
socket.setTcpNoDelay(true);
ConnectionHandler handler = new ConnectionHandler(this, socket);
register(handler);
handler.start();
Expand Down

0 comments on commit 684506a

Please sign in to comment.