30
30
31
31
import javax .net .ssl .HostnameVerifier ;
32
32
import java .io .File ;
33
+ import java .net .Socket ;
33
34
import java .time .Duration ;
34
35
import java .util .ArrayList ;
35
36
import java .util .Collections ;
@@ -82,14 +83,16 @@ public final class PostgresqlConnectionConfiguration {
82
83
83
84
private final SSLConfig sslConfig ;
84
85
86
+ private final boolean tcpKeepAlive ;
87
+
88
+ private final boolean tcpNoDelay ;
89
+
85
90
private final int preparedStatementCacheQueries ;
86
91
87
- private PostgresqlConnectionConfiguration (String applicationName , boolean autodetectExtensions ,
88
- @ Nullable Duration connectTimeout , @ Nullable String database , List <Extension > extensions ,
89
- ToIntFunction <String > fetchSize , boolean forceBinary ,
90
- @ Nullable String host ,
91
- @ Nullable Map <String , String > options , @ Nullable CharSequence password , int port , @ Nullable String schema , @ Nullable String socket , String username ,
92
- SSLConfig sslConfig , int preparedStatementCacheQueries ) {
92
+ private PostgresqlConnectionConfiguration (String applicationName , boolean autodetectExtensions , @ Nullable Duration connectTimeout , @ Nullable String database , List <Extension > extensions ,
93
+ ToIntFunction <String > fetchSize , boolean forceBinary , @ Nullable String host , @ Nullable Map <String , String > options , @ Nullable CharSequence password ,
94
+ int port , @ Nullable String schema , @ Nullable String socket , boolean tcpKeepAlive , boolean tcpNoDelay , String username , SSLConfig sslConfig ,
95
+ int preparedStatementCacheQueries ) {
93
96
this .applicationName = Assert .requireNonNull (applicationName , "applicationName must not be null" );
94
97
this .autodetectExtensions = autodetectExtensions ;
95
98
this .connectTimeout = connectTimeout ;
@@ -109,6 +112,8 @@ private PostgresqlConnectionConfiguration(String applicationName, boolean autode
109
112
this .socket = socket ;
110
113
this .username = Assert .requireNonNull (username , "username must not be null" );
111
114
this .sslConfig = sslConfig ;
115
+ this .tcpKeepAlive = tcpKeepAlive ;
116
+ this .tcpNoDelay = tcpNoDelay ;
112
117
this .preparedStatementCacheQueries = preparedStatementCacheQueries ;
113
118
}
114
119
@@ -135,6 +140,8 @@ public String toString() {
135
140
", options='" + this .options + '\'' +
136
141
", password='" + obfuscate (this .password != null ? this .password .length () : 0 ) + '\'' +
137
142
", port=" + this .port +
143
+ ", tcpKeepAlive=" + this .tcpKeepAlive +
144
+ ", tcpNoDelay=" + this .tcpNoDelay +
138
145
", username='" + this .username + '\'' +
139
146
'}' ;
140
147
}
@@ -222,6 +229,14 @@ boolean isForceBinary() {
222
229
return this .forceBinary ;
223
230
}
224
231
232
+ boolean isTcpKeepAlive () {
233
+ return this .tcpKeepAlive ;
234
+ }
235
+
236
+ boolean isTcpNoDelay () {
237
+ return this .tcpNoDelay ;
238
+ }
239
+
225
240
boolean isUseSocket () {
226
241
return getSocket () != null ;
227
242
}
@@ -302,6 +317,10 @@ public static final class Builder {
302
317
303
318
private Function <SslContextBuilder , SslContextBuilder > sslContextBuilderCustomizer = Function .identity ();
304
319
320
+ private boolean tcpKeepAlive ;
321
+
322
+ private boolean tcpNoDelay ;
323
+
305
324
@ Nullable
306
325
private String username ;
307
326
@@ -353,8 +372,8 @@ public PostgresqlConnectionConfiguration build() {
353
372
}
354
373
355
374
return new PostgresqlConnectionConfiguration (this .applicationName , this .autodetectExtensions , this .connectTimeout , this .database , this .extensions , this .fetchSize , this .forceBinary ,
356
- this .host ,
357
- this .options , this . password , this . port , this . schema , this . socket , this . username , this . createSslConfig (), this . preparedStatementCacheQueries );
375
+ this .host , this . options , this . password , this . port , this . schema , this . socket , this . tcpKeepAlive , this . tcpNoDelay , this . username , this . createSslConfig (),
376
+ this .preparedStatementCacheQueries );
358
377
}
359
378
360
379
/**
@@ -610,6 +629,32 @@ public Builder sslRootCert(String sslRootCert) {
610
629
return this ;
611
630
}
612
631
632
+ /**
633
+ * Configure TCP KeepAlive.
634
+ *
635
+ * @param enabled whether to enable TCP KeepAlive
636
+ * @return this {@link Builder}
637
+ * @see Socket#setKeepAlive(boolean)
638
+ * @since 0.8.4
639
+ */
640
+ public Builder tcpKeepAlive (boolean enabled ) {
641
+ this .tcpKeepAlive = enabled ;
642
+ return this ;
643
+ }
644
+
645
+ /**
646
+ * Configure TCP NoDelay.
647
+ *
648
+ * @param enabled whether to enable TCP NoDelay
649
+ * @return this {@link Builder}
650
+ * @see Socket#setTcpNoDelay(boolean)
651
+ * @since 0.8.4
652
+ */
653
+ public Builder tcpNoDelay (boolean enabled ) {
654
+ this .tcpNoDelay = enabled ;
655
+ return this ;
656
+ }
657
+
613
658
/**
614
659
* Configure the username.
615
660
*
@@ -658,6 +703,8 @@ public String toString() {
658
703
", sslCert='" + this .sslCert + '\'' +
659
704
", sslKey='" + this .sslKey + '\'' +
660
705
", sslHostnameVerifier='" + this .sslHostnameVerifier + '\'' +
706
+ ", tcpKeepAlive='" + this .tcpKeepAlive + '\'' +
707
+ ", tcpNoDelay='" + this .tcpNoDelay + '\'' +
661
708
", preparedStatementCacheQueries='" + this .preparedStatementCacheQueries + '\'' +
662
709
'}' ;
663
710
}
0 commit comments