34
34
35
35
import javax .net .ssl .HostnameVerifier ;
36
36
import java .io .File ;
37
+ import java .net .Socket ;
37
38
import java .time .Duration ;
38
39
import java .util .ArrayList ;
39
40
import java .util .Collections ;
@@ -90,15 +91,19 @@ public final class PostgresqlConnectionConfiguration {
90
91
91
92
private final SSLConfig sslConfig ;
92
93
94
+ private final boolean tcpKeepAlive ;
95
+
96
+ private final boolean tcpNoDelay ;
97
+
93
98
private final int preparedStatementCacheQueries ;
94
99
95
- private PostgresqlConnectionConfiguration (String applicationName , boolean autodetectExtensions ,
96
- @ Nullable Duration connectTimeout , @ Nullable String database , LogLevel errorResponseLogLevel , List <Extension > extensions ,
100
+ private PostgresqlConnectionConfiguration (String applicationName , boolean autodetectExtensions , @ Nullable Duration connectTimeout , @ Nullable String database , LogLevel errorResponseLogLevel ,
101
+ List <Extension > extensions ,
97
102
ToIntFunction <String > fetchSize , boolean forceBinary ,
98
103
LogLevel noticeLogLevel , @ Nullable String host ,
99
104
@ Nullable Map <String , String > options , @ Nullable CharSequence password , int port , @ Nullable String schema ,
100
- @ Nullable String socket , String username ,
101
- SSLConfig sslConfig , int preparedStatementCacheQueries ) {
105
+ @ Nullable String socket , boolean tcpKeepAlive , boolean tcpNoDelay , String username , SSLConfig sslConfig ,
106
+ int preparedStatementCacheQueries ) {
102
107
this .applicationName = Assert .requireNonNull (applicationName , "applicationName must not be null" );
103
108
this .autodetectExtensions = autodetectExtensions ;
104
109
this .connectTimeout = connectTimeout ;
@@ -120,6 +125,8 @@ private PostgresqlConnectionConfiguration(String applicationName, boolean autode
120
125
this .socket = socket ;
121
126
this .username = Assert .requireNonNull (username , "username must not be null" );
122
127
this .sslConfig = sslConfig ;
128
+ this .tcpKeepAlive = tcpKeepAlive ;
129
+ this .tcpNoDelay = tcpNoDelay ;
123
130
this .preparedStatementCacheQueries = preparedStatementCacheQueries ;
124
131
}
125
132
@@ -148,6 +155,8 @@ public String toString() {
148
155
", options='" + this .options + '\'' +
149
156
", password='" + obfuscate (this .password != null ? this .password .length () : 0 ) + '\'' +
150
157
", port=" + this .port +
158
+ ", tcpKeepAlive=" + this .tcpKeepAlive +
159
+ ", tcpNoDelay=" + this .tcpNoDelay +
151
160
", username='" + this .username + '\'' +
152
161
'}' ;
153
162
}
@@ -235,6 +244,14 @@ boolean isForceBinary() {
235
244
return this .forceBinary ;
236
245
}
237
246
247
+ boolean isTcpKeepAlive () {
248
+ return this .tcpKeepAlive ;
249
+ }
250
+
251
+ boolean isTcpNoDelay () {
252
+ return this .tcpNoDelay ;
253
+ }
254
+
238
255
boolean isUseSocket () {
239
256
return getSocket () != null ;
240
257
}
@@ -249,6 +266,8 @@ ConnectionSettings getConnectionSettings() {
249
266
.errorResponseLogLevel (this .errorResponseLogLevel )
250
267
.noticeLogLevel (this .noticeLogLevel )
251
268
.sslConfig (getSslConfig ())
269
+ .tcpKeepAlive (isTcpKeepAlive ())
270
+ .tcpNoDelay (isTcpNoDelay ())
252
271
.build ();
253
272
}
254
273
@@ -328,6 +347,10 @@ public static final class Builder {
328
347
329
348
private Function <SslContextBuilder , SslContextBuilder > sslContextBuilderCustomizer = Function .identity ();
330
349
350
+ private boolean tcpKeepAlive ;
351
+
352
+ private boolean tcpNoDelay ;
353
+
331
354
@ Nullable
332
355
private String username ;
333
356
@@ -379,7 +402,8 @@ public PostgresqlConnectionConfiguration build() {
379
402
}
380
403
381
404
return new PostgresqlConnectionConfiguration (this .applicationName , this .autodetectExtensions , this .connectTimeout , this .database , this .errorResponseLogLevel , this .extensions ,
382
- this .fetchSize , this .forceBinary , this .noticeLogLevel , this .host , this .options , this .password , this .port , this .schema , this .socket , this .username , this .createSslConfig (),
405
+ this .fetchSize , this .forceBinary , this .noticeLogLevel , this .host , this .options , this .password , this .port , this .schema , this .socket , this .tcpKeepAlive , this .tcpNoDelay , this .username
406
+ , this .createSslConfig (),
383
407
this .preparedStatementCacheQueries );
384
408
}
385
409
@@ -660,6 +684,32 @@ public Builder sslRootCert(String sslRootCert) {
660
684
return this ;
661
685
}
662
686
687
+ /**
688
+ * Configure TCP KeepAlive.
689
+ *
690
+ * @param enabled whether to enable TCP KeepAlive
691
+ * @return this {@link Builder}
692
+ * @see Socket#setKeepAlive(boolean)
693
+ * @since 0.8.4
694
+ */
695
+ public Builder tcpKeepAlive (boolean enabled ) {
696
+ this .tcpKeepAlive = enabled ;
697
+ return this ;
698
+ }
699
+
700
+ /**
701
+ * Configure TCP NoDelay.
702
+ *
703
+ * @param enabled whether to enable TCP NoDelay
704
+ * @return this {@link Builder}
705
+ * @see Socket#setTcpNoDelay(boolean)
706
+ * @since 0.8.4
707
+ */
708
+ public Builder tcpNoDelay (boolean enabled ) {
709
+ this .tcpNoDelay = enabled ;
710
+ return this ;
711
+ }
712
+
663
713
/**
664
714
* Configure the username.
665
715
*
@@ -710,6 +760,8 @@ public String toString() {
710
760
", sslCert='" + this .sslCert + '\'' +
711
761
", sslKey='" + this .sslKey + '\'' +
712
762
", sslHostnameVerifier='" + this .sslHostnameVerifier + '\'' +
763
+ ", tcpKeepAlive='" + this .tcpKeepAlive + '\'' +
764
+ ", tcpNoDelay='" + this .tcpNoDelay + '\'' +
713
765
", preparedStatementCacheQueries='" + this .preparedStatementCacheQueries + '\'' +
714
766
'}' ;
715
767
}
0 commit comments