Skip to content

Commit 4bbdbf1

Browse files
author
Viktors Baltauss
committed
Duration PR comment fixed, polishing
1 parent 90ff1de commit 4bbdbf1

File tree

5 files changed

+33
-31
lines changed

5 files changed

+33
-31
lines changed

src/main/java/io/r2dbc/postgresql/MultipleHostsClientFactory.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import javax.annotation.Nullable;
1212
import java.net.InetSocketAddress;
1313
import java.net.SocketAddress;
14+
import java.time.Duration;
1415
import java.util.ArrayList;
1516
import java.util.Collections;
1617
import java.util.List;
@@ -115,7 +116,9 @@ private Flux<SocketAddress> getCandidates(TargetServerType targetServerType) {
115116
int counter = 0;
116117
for (SocketAddress address : addresses) {
117118
HostSpecStatus currentStatus = this.statusMap.get(address);
118-
if (currentStatus == null || now > currentStatus.updated + this.configuration.getHostRecheckTime()) {
119+
Duration hostRecheckDuration = this.configuration.getHostRecheckTime();
120+
boolean recheck = currentStatus == null || hostRecheckDuration.plusMillis(currentStatus.updated).toMillis() < now;
121+
if (recheck) {
119122
sink.next(address);
120123
counter++;
121124
} else if (targetServerType.allowStatus(currentStatus.hostStatus)) {

src/main/java/io/r2dbc/postgresql/PostgresqlConnectionConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -598,12 +598,12 @@ public Builder targetServerType(TargetServerType targetServerType) {
598598
}
599599

600600
/**
601-
* Controls how long in seconds the knowledge about a host state is cached connection factory. The default value is 10000 milliseconds.
601+
* Controls how long in milliseconds the knowledge about a host state is cached connection factory. The default value is 10000 milliseconds.
602602
*
603603
* @param hostRecheckTime host recheck time in milliseconds
604604
* @return this {@link Builder}
605605
*/
606-
public Builder hostRecheckTime(int hostRecheckTime) {
606+
public Builder hostRecheckTime(@Nullable Duration hostRecheckTime) {
607607
if (this.multipleHostsConfiguration == null) {
608608
this.multipleHostsConfiguration = MultipleHostsConfiguration.builder();
609609
}

src/main/java/io/r2dbc/postgresql/PostgresqlConnectionFactoryProvider.java

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import io.r2dbc.spi.Option;
2626

2727
import javax.net.ssl.HostnameVerifier;
28+
import java.time.Duration;
2829
import java.util.Map;
2930
import java.util.function.Function;
3031

@@ -232,6 +233,7 @@ private static PostgresqlConnectionConfiguration.Builder fromConnectionFactoryOp
232233

233234
if (isUsingTcp(connectionFactoryOptions)) {
234235
setupSsl(builder, connectionFactoryOptions);
236+
setupFailover(builder, connectionFactoryOptions);
235237
} else {
236238
builder.socket(connectionFactoryOptions.getRequiredValue(SOCKET));
237239
}
@@ -263,27 +265,11 @@ private static void setSslHostnameVerifier(PostgresqlConnectionConfiguration.Bui
263265
}
264266
}
265267

266-
private static void setupSsl(PostgresqlConnectionConfiguration.Builder builder, ConnectionFactoryOptions connectionFactoryOptions) {
267-
Boolean ssl = connectionFactoryOptions.getValue(SSL);
268-
if (ssl != null && ssl) {
269-
builder.enableSsl();
270-
}
271-
272-
Object sslMode = connectionFactoryOptions.getValue(SSL_MODE);
273-
if (sslMode != null) {
274-
if (sslMode instanceof String) {
275-
builder.sslMode(SSLMode.fromValue(sslMode.toString()));
276-
} else {
277-
builder.sslMode((SSLMode) sslMode);
278-
}
279-
}
280-
281-
builder.connectTimeout(connectionFactoryOptions.getValue(CONNECT_TIMEOUT));
282-
builder.database(connectionFactoryOptions.getValue(DATABASE));
283-
268+
private static void setupFailover(PostgresqlConnectionConfiguration.Builder builder, ConnectionFactoryOptions connectionFactoryOptions) {
284269
if (FAILOVER_PROTOCOL.equals(connectionFactoryOptions.getValue(PROTOCOL))) {
285270
if (connectionFactoryOptions.hasOption(HOST_RECHECK_TIME)) {
286-
builder.hostRecheckTime(connectionFactoryOptions.getRequiredValue(HOST_RECHECK_TIME));
271+
Duration hostRecheckTime = Duration.ofMillis(connectionFactoryOptions.getRequiredValue(HOST_RECHECK_TIME));
272+
builder.hostRecheckTime(hostRecheckTime);
287273
}
288274
if (connectionFactoryOptions.hasOption(LOAD_BALANCE_HOSTS)) {
289275
Object loadBalanceHosts = connectionFactoryOptions.getRequiredValue(LOAD_BALANCE_HOSTS);
@@ -323,11 +309,22 @@ private static void setupSsl(PostgresqlConnectionConfiguration.Builder builder,
323309
builder.port(port);
324310
}
325311
}
312+
}
326313

314+
private static void setupSsl(PostgresqlConnectionConfiguration.Builder builder, ConnectionFactoryOptions connectionFactoryOptions) {
315+
Boolean ssl = connectionFactoryOptions.getValue(SSL);
316+
if (ssl != null && ssl) {
317+
builder.enableSsl();
318+
}
327319

328-
builder.password(connectionFactoryOptions.getValue(PASSWORD));
329-
builder.schema(connectionFactoryOptions.getValue(SCHEMA));
330-
builder.username(connectionFactoryOptions.getRequiredValue(USER));
320+
Object sslMode = connectionFactoryOptions.getValue(SSL_MODE);
321+
if (sslMode != null) {
322+
if (sslMode instanceof String) {
323+
builder.sslMode(SSLMode.fromValue(sslMode.toString()));
324+
} else {
325+
builder.sslMode((SSLMode) sslMode);
326+
}
327+
}
331328

332329
String sslRootCert = connectionFactoryOptions.getValue(SSL_ROOT_CERT);
333330
if (sslRootCert != null) {

src/main/java/io/r2dbc/postgresql/client/MultipleHostsConfiguration.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.r2dbc.postgresql.TargetServerType;
44
import io.r2dbc.postgresql.util.Assert;
55

6+
import java.time.Duration;
67
import java.util.ArrayList;
78
import java.util.List;
89

@@ -12,20 +13,20 @@ public class MultipleHostsConfiguration {
1213

1314
private final List<ServerHost> hosts;
1415

15-
private final int hostRecheckTime;
16+
private final Duration hostRecheckTime;
1617

1718
private final boolean loadBalanceHosts;
1819

1920
private final TargetServerType targetServerType;
2021

21-
public MultipleHostsConfiguration(List<ServerHost> hosts, int hostRecheckTime, boolean loadBalanceHosts, TargetServerType targetServerType) {
22+
public MultipleHostsConfiguration(List<ServerHost> hosts, Duration hostRecheckTime, boolean loadBalanceHosts, TargetServerType targetServerType) {
2223
this.hosts = hosts;
2324
this.hostRecheckTime = hostRecheckTime;
2425
this.loadBalanceHosts = loadBalanceHosts;
2526
this.targetServerType = targetServerType;
2627
}
2728

28-
public int getHostRecheckTime() {
29+
public Duration getHostRecheckTime() {
2930
return hostRecheckTime;
3031
}
3132

@@ -95,7 +96,7 @@ public static Builder builder() {
9596
*/
9697
public static class Builder {
9798

98-
private int hostRecheckTime = 10000;
99+
private Duration hostRecheckTime = Duration.ofMillis(10000);
99100

100101
private List<ServerHost> hosts = new ArrayList<>();
101102

@@ -124,7 +125,7 @@ public Builder targetServerType(TargetServerType targetServerType) {
124125
* @param hostRecheckTime host recheck time in milliseconds
125126
* @return this {@link Builder}
126127
*/
127-
public Builder hostRecheckTime(int hostRecheckTime) {
128+
public Builder hostRecheckTime(Duration hostRecheckTime) {
128129
this.hostRecheckTime = hostRecheckTime;
129130
return this;
130131
}

src/test/java/io/r2dbc/postgresql/PostgresqlConnectionFactoryProviderTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.r2dbc.spi.Option;
2424
import org.junit.jupiter.api.Test;
2525

26+
import java.time.Duration;
2627
import java.util.HashMap;
2728
import java.util.List;
2829
import java.util.Map;
@@ -297,7 +298,7 @@ void testFailoverConfiguration() {
297298

298299
assertThat(factory.getConfiguration().getSingleHostConfiguration()).isNull();
299300
assertThat(factory.getConfiguration().getMultipleHostsConfiguration().isLoadBalanceHosts()).isEqualTo(true);
300-
assertThat(factory.getConfiguration().getMultipleHostsConfiguration().getHostRecheckTime()).isEqualTo(20000);
301+
assertThat(factory.getConfiguration().getMultipleHostsConfiguration().getHostRecheckTime()).isEqualTo(Duration.ofMillis(20000));
301302
assertThat(factory.getConfiguration().getMultipleHostsConfiguration().getTargetServerType()).isEqualTo(TargetServerType.SECONDARY);
302303
List<MultipleHostsConfiguration.ServerHost> hosts = factory.getConfiguration().getMultipleHostsConfiguration().getHosts();
303304
assertThat(hosts).hasSize(3);

0 commit comments

Comments
 (0)