Skip to content

Commit f515a73

Browse files
committed
Polishing
Reformat code.
1 parent 934f042 commit f515a73

12 files changed

+104
-88
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ Mono<Connection> connectionMono = Mono.from(connectionFactory.create());
7979
| `autodetectExtensions` | Whether to auto-detect and register `Extension`s from the class path. Defaults to `true`. _(Optional)_
8080
| `fetchSize` | The default number of rows to return when fetching results. Defaults to `0` for unlimited. _(Optional)_
8181
| `forceBinary` | Whether to force binary transfer. Defaults to `false`. _(Optional)_
82-
| `preparedStatementCacheQueries` | Determine the number of queries that are cached in each connection. The default is `-1`, meaning there's no limit. The value of `-1` disables the cache. Any other value specifies the cache size.
82+
| `loopResources` | TCP/Socket LoopResources (depends on the endpoint connection type). _(Optional)_
83+
| `preparedStatementCacheQueries` | Determine the number of queries that are cached in each connection. The default is `-1`, meaning there's no limit. The value of `0` disables the cache. Any other value specifies the cache size.
8384
| `options` | A `Map<String, String>` of connection parameters. These are applied to each database connection created by the `ConnectionFactory`. Useful for setting generic [PostgreSQL connection parameters][psql-runtime-config]. _(Optional)_
8485
| `schema` | The search path to set. _(Optional)_
8586
| `sslMode` | SSL mode to use, see `SSLMode` enum. Supported values: `DISABLE`, `ALLOW`, `PREFER`, `REQUIRE`, `VERIFY_CA`, `VERIFY_FULL`, `TUNNEL`. _(Optional)_
@@ -90,7 +91,6 @@ Mono<Connection> connectionMono = Mono.from(connectionFactory.create());
9091
| `sslHostnameVerifier` | `javax.net.ssl.HostnameVerifier` implementation. _(Optional)_
9192
| `tcpNoDelay` | Enabled/disable TCP NoDelay. Disabled by default. _(Optional)_
9293
| `tcpKeepAlive` | Enabled/disable TCP KeepAlive. Disabled by default. _(Optional)_
93-
| `tcpLoopResources`| TCP LoopResources. _(Optional)_
9494

9595
**Programmatic Configuration**
9696

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

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ public final class PostgresqlConnectionConfiguration {
7272

7373
private final String host;
7474

75+
@Nullable
76+
private final LoopResources loopResources;
77+
7578
private final Map<String, String> options;
7679

7780
private final CharSequence password;
@@ -82,20 +85,18 @@ public final class PostgresqlConnectionConfiguration {
8285

8386
private final String socket;
8487

85-
private final String username;
86-
8788
private final SSLConfig sslConfig;
8889

8990
private final boolean tcpKeepAlive;
9091

9192
private final boolean tcpNoDelay;
9293

93-
private final LoopResources tcpLoopResources;
94+
private final String username;
9495

9596
private PostgresqlConnectionConfiguration(String applicationName, boolean autodetectExtensions, @Nullable Duration connectTimeout, @Nullable String database, List<Extension> extensions,
96-
ToIntFunction<String> fetchSize, boolean forceBinary, @Nullable String host, @Nullable Map<String, String> options, @Nullable CharSequence password,
97-
int port, int preparedStatementCacheQueries, @Nullable String schema, @Nullable String socket, boolean tcpKeepAlive, boolean tcpNoDelay,
98-
String username, SSLConfig sslConfig, LoopResources tcpLoopResources) {
97+
ToIntFunction<String> fetchSize, boolean forceBinary, @Nullable String host, @Nullable LoopResources loopResources,
98+
@Nullable Map<String, String> options, @Nullable CharSequence password, int port, int preparedStatementCacheQueries, @Nullable String schema,
99+
@Nullable String socket, SSLConfig sslConfig, boolean tcpKeepAlive, boolean tcpNoDelay, String username) {
99100
this.applicationName = Assert.requireNonNull(applicationName, "applicationName must not be null");
100101
this.autodetectExtensions = autodetectExtensions;
101102
this.connectTimeout = connectTimeout;
@@ -104,6 +105,7 @@ private PostgresqlConnectionConfiguration(String applicationName, boolean autode
104105
this.fetchSize = fetchSize;
105106
this.forceBinary = forceBinary;
106107
this.host = host;
108+
this.loopResources = loopResources;
107109
this.options = options == null ? new LinkedHashMap<>() : new LinkedHashMap<>(options);
108110

109111
if (schema != null && !schema.isEmpty()) {
@@ -114,11 +116,10 @@ private PostgresqlConnectionConfiguration(String applicationName, boolean autode
114116
this.port = port;
115117
this.preparedStatementCacheQueries = preparedStatementCacheQueries;
116118
this.socket = socket;
117-
this.username = Assert.requireNonNull(username, "username must not be null");
118119
this.sslConfig = sslConfig;
119120
this.tcpKeepAlive = tcpKeepAlive;
120121
this.tcpNoDelay = tcpNoDelay;
121-
this.tcpLoopResources = tcpLoopResources;
122+
this.username = Assert.requireNonNull(username, "username must not be null");
122123
}
123124

124125
/**
@@ -141,9 +142,11 @@ public String toString() {
141142
", fetchSize=" + this.fetchSize +
142143
", forceBinary='" + this.forceBinary + '\'' +
143144
", host='" + this.host + '\'' +
145+
", loopResources='" + this.loopResources + '\'' +
144146
", options='" + this.options + '\'' +
145147
", password='" + obfuscate(this.password != null ? this.password.length() : 0) + '\'' +
146148
", port=" + this.port +
149+
", socket=" + this.socket +
147150
", tcpKeepAlive=" + this.tcpKeepAlive +
148151
", tcpNoDelay=" + this.tcpNoDelay +
149152
", username='" + this.username + '\'' +
@@ -192,6 +195,11 @@ String getRequiredHost() {
192195
return host;
193196
}
194197

198+
@Nullable
199+
LoopResources getLoopResources() {
200+
return this.loopResources;
201+
}
202+
195203
Map<String, String> getOptions() {
196204
return Collections.unmodifiableMap(this.options);
197205
}
@@ -253,10 +261,6 @@ SSLConfig getSslConfig() {
253261
return this.sslConfig;
254262
}
255263

256-
LoopResources getTcpLoopResources() {
257-
return this.tcpLoopResources;
258-
}
259-
260264
private static String obfuscate(int length) {
261265

262266
StringBuilder builder = new StringBuilder();
@@ -285,7 +289,7 @@ public static final class Builder {
285289
@Nullable
286290
private String database;
287291

288-
private List<Extension> extensions = new ArrayList<>();
292+
private final List<Extension> extensions = new ArrayList<>();
289293

290294
private ToIntFunction<String> fetchSize = sql -> NO_LIMIT;
291295

@@ -332,7 +336,7 @@ public static final class Builder {
332336
private boolean tcpNoDelay = false;
333337

334338
@Nullable
335-
private LoopResources tcpLoopResources = null;
339+
private LoopResources loopResources = null;
336340

337341
@Nullable
338342
private String username;
@@ -383,9 +387,8 @@ public PostgresqlConnectionConfiguration build() {
383387
}
384388

385389
return new PostgresqlConnectionConfiguration(this.applicationName, this.autodetectExtensions, this.connectTimeout, this.database, this.extensions, this.fetchSize, this.forceBinary,
386-
this.host, this.options, this.password, this.port, this.preparedStatementCacheQueries, this.schema, this.socket, this.tcpKeepAlive, this.tcpNoDelay, this.username,
387-
this.createSslConfig()
388-
, this.tcpLoopResources);
390+
this.host, this.loopResources, this.options, this.password, this.port, this.preparedStatementCacheQueries, this.schema, this.socket, this.createSslConfig(), this.tcpKeepAlive,
391+
this.tcpNoDelay, this.username);
389392
}
390393

391394
/**
@@ -492,6 +495,18 @@ public Builder host(String host) {
492495
return this;
493496
}
494497

498+
/**
499+
* Configure {@link LoopResources}.
500+
*
501+
* @param loopResources the {@link LoopResources}
502+
* @return this {@link Builder}
503+
* @since 0.8.5
504+
*/
505+
public Builder loopResources(LoopResources loopResources) {
506+
this.loopResources = Assert.requireNonNull(loopResources, "loopResources must not be null");
507+
return this;
508+
}
509+
495510
/**
496511
* Configure connection initialization parameters.
497512
* <p>
@@ -692,18 +707,6 @@ public Builder username(String username) {
692707
return this;
693708
}
694709

695-
/**
696-
* Configure TCP {@link LoopResources}.
697-
*
698-
* @param loopResources the {@link LoopResources}
699-
* @return this {@link Builder}
700-
* @since 1.0.0
701-
*/
702-
public Builder tcpLoopResources(LoopResources loopResources) {
703-
this.tcpLoopResources = Assert.requireNonNull(loopResources, "tcpLoopResources must not be null");
704-
return this;
705-
}
706-
707710
@Override
708711
public String toString() {
709712
return "Builder{" +
@@ -715,6 +718,7 @@ public String toString() {
715718
", fetchSize='" + this.fetchSize + '\'' +
716719
", forceBinary='" + this.forceBinary + '\'' +
717720
", host='" + this.host + '\'' +
721+
", loopResources='" + this.loopResources + '\'' +
718722
", parameters='" + this.options + '\'' +
719723
", password='" + obfuscate(this.password != null ? this.password.length() : 0) + '\'' +
720724
", port=" + this.port +

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
import io.r2dbc.postgresql.authentication.AuthenticationHandler;
2222
import io.r2dbc.postgresql.authentication.PasswordAuthenticationHandler;
2323
import io.r2dbc.postgresql.authentication.SASLAuthenticationHandler;
24-
import io.r2dbc.postgresql.client.ConnectionSettings;
2524
import io.r2dbc.postgresql.client.Client;
25+
import io.r2dbc.postgresql.client.ConnectionSettings;
2626
import io.r2dbc.postgresql.client.ReactorNettyClient;
2727
import io.r2dbc.postgresql.client.SSLConfig;
2828
import io.r2dbc.postgresql.client.SSLMode;
@@ -79,7 +79,7 @@ public PostgresqlConnectionFactory(PostgresqlConnectionConfiguration configurati
7979
this.configuration = Assert.requireNonNull(configuration, "configuration must not be null");
8080
this.endpoint = createSocketAddress(configuration);
8181

82-
ConnectionSettings options = new ConnectionSettings(configuration.getConnectTimeout(), configuration.isTcpKeepAlive(), configuration.isTcpNoDelay());
82+
ConnectionSettings options = new ConnectionSettings(configuration.getConnectTimeout(), configuration.isTcpKeepAlive(), configuration.isTcpNoDelay(), configuration.getLoopResources());
8383
this.clientFactory = sslConfig -> ReactorNettyClient.connect(ConnectionProvider.newConnection(), this.endpoint, options, sslConfig).cast(Client.class);
8484
this.extensions = getExtensions(configuration);
8585
}

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ public final class PostgresqlConnectionFactoryProvider implements ConnectionFact
6464
*/
6565
public static final Option<Boolean> FORCE_BINARY = Option.valueOf("forceBinary");
6666

67+
/**
68+
* Event {@link LoopResources}.
69+
*
70+
* @since 0.8.5
71+
*/
72+
public static final Option<LoopResources> LOOP_RESOURCES = Option.valueOf("loopResources");
73+
74+
/**
75+
* Connection options which are applied once after the connection has been created.
76+
*/
77+
public static final Option<Map<String, String>> OPTIONS = Option.valueOf("options");
78+
6779
/**
6880
* Driver option value.
6981
*/
@@ -74,6 +86,12 @@ public final class PostgresqlConnectionFactoryProvider implements ConnectionFact
7486
*/
7587
public static final String LEGACY_POSTGRESQL_DRIVER = "postgres";
7688

89+
/**
90+
* Determine the number of queries that are cached in each connection.
91+
* The default is {@code -1}, meaning there's no limit. The value of {@code 0} disables the cache. Any other value specifies the cache size.
92+
*/
93+
public static final Option<Integer> PREPARED_STATEMENT_CACHE_QUERIES = Option.valueOf("preparedStatementCacheQueries");
94+
7795
/**
7896
* Schema search path (alias for "currentSchema").
7997
*/
@@ -140,24 +158,6 @@ public final class PostgresqlConnectionFactoryProvider implements ConnectionFact
140158
*/
141159
public static final Option<Boolean> TCP_NODELAY = Option.valueOf("tcpNoDelay");
142160

143-
/**
144-
* TCP {@link LoopResources}.
145-
*
146-
* @since 1.0.0
147-
*/
148-
public static final Option<LoopResources> TCP_LOOP_RESOURCES = Option.valueOf("tcpLoopResources");
149-
150-
/**
151-
* Determine the number of queries that are cached in each connection.
152-
* The default is {@code -1}, meaning there's no limit. The value of {@code 0} disables the cache. Any other value specifies the cache size.
153-
*/
154-
public static final Option<Integer> PREPARED_STATEMENT_CACHE_QUERIES = Option.valueOf("preparedStatementCacheQueries");
155-
156-
/**
157-
* Connection options which are applied once after the connection has been created.
158-
*/
159-
public static final Option<Map<String, String>> OPTIONS = Option.valueOf("options");
160-
161161
/**
162162
* Returns a new {@link PostgresqlConnectionConfiguration.Builder} configured with the given {@link ConnectionFactoryOptions}.
163163
*
@@ -209,6 +209,7 @@ private static PostgresqlConnectionConfiguration.Builder fromConnectionFactoryOp
209209
mapper.from(DATABASE).to(builder::database);
210210
mapper.from(FETCH_SIZE).map(OptionMapper::toInteger).to(builder::fetchSize);
211211
mapper.from(FORCE_BINARY).map(OptionMapper::toBoolean).to(builder::forceBinary);
212+
mapper.from(LOOP_RESOURCES).to(builder::loopResources);
212213
mapper.from(OPTIONS).map(PostgresqlConnectionFactoryProvider::convertToMap).to(builder::options);
213214
mapper.from(PASSWORD).to(builder::password);
214215
mapper.from(PORT).map(OptionMapper::toInteger).to(builder::port);
@@ -219,7 +220,6 @@ private static PostgresqlConnectionConfiguration.Builder fromConnectionFactoryOp
219220
});
220221
mapper.from(TCP_KEEPALIVE).map(OptionMapper::toBoolean).to(builder::tcpKeepAlive);
221222
mapper.from(TCP_NODELAY).map(OptionMapper::toBoolean).to(builder::tcpNoDelay);
222-
mapper.from(TCP_LOOP_RESOURCES).to(builder::tcpLoopResources);
223223
builder.username(options.getRequiredValue(USER));
224224

225225
return builder;

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ public final class ConnectionSettings {
3636
private final boolean tcpNoDelay;
3737

3838
@Nullable
39-
private final LoopResources tcpLoopResources;
39+
private final LoopResources loopResources;
4040

41-
public ConnectionSettings(@Nullable Duration connectTimeout, boolean tcpKeepAlive, boolean tcpNoDelay, @Nullable LoopResources tcpLoopResources) {
41+
public ConnectionSettings(@Nullable Duration connectTimeout, boolean tcpKeepAlive, boolean tcpNoDelay, @Nullable LoopResources loopResources) {
4242
this.tcpKeepAlive = tcpKeepAlive;
4343
this.tcpNoDelay = tcpNoDelay;
4444
this.connectTimeout = connectTimeout;
45-
this.tcpLoopResources = tcpLoopResources;
45+
this.loopResources = loopResources;
4646
}
4747

4848
@Nullable
@@ -58,12 +58,17 @@ boolean isTcpNoDelay() {
5858
return this.tcpNoDelay;
5959
}
6060

61-
public boolean hasTcpLoopResources() {
62-
return this.tcpLoopResources != null;
61+
boolean hasLoopResources() {
62+
return this.loopResources != null;
6363
}
64-
65-
LoopResources getTcpLoopResources() {
66-
return this.tcpLoopResources;
64+
65+
LoopResources getRequiredLoopResources() {
66+
67+
if (!hasLoopResources()) {
68+
throw new IllegalStateException("No LoopResources configured");
69+
}
70+
71+
return this.loopResources;
6772
}
6873

6974
}

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -346,17 +346,18 @@ public static Mono<ReactorNettyClient> connect(String host, int port, @Nullable
346346
/**
347347
* Create a new frame processor connected to a given host.
348348
*
349-
* @param host the host to connect to
350-
* @param port the port to connect to
351-
* @param connectTimeout connect timeout
352-
* @param sslConfig SSL configuration
353-
* @param tcpLoopResources tcp loop resources
349+
* @param host the host to connect to
350+
* @param port the port to connect to
351+
* @param connectTimeout connect timeout
352+
* @param sslConfig SSL configuration
353+
* @param loopResources tcp loop resources
354354
* @throws IllegalArgumentException if {@code host} is {@code null}
355+
* @since 0.8.5
355356
*/
356-
public static Mono<ReactorNettyClient> connect(String host, int port, @Nullable Duration connectTimeout, @Nullable SSLConfig sslConfig, @Nullable LoopResources tcpLoopResources) {
357+
public static Mono<ReactorNettyClient> connect(String host, int port, @Nullable Duration connectTimeout, @Nullable SSLConfig sslConfig, @Nullable LoopResources loopResources) {
357358
Assert.requireNonNull(host, "host must not be null");
358359

359-
ConnectionSettings settings = new ConnectionSettings(connectTimeout, false, false, tcpLoopResources);
360+
ConnectionSettings settings = new ConnectionSettings(connectTimeout, false, false, loopResources);
360361
return connect(ConnectionProvider.newConnection(), InetSocketAddress.createUnresolved(host, port), settings, sslConfig);
361362
}
362363

@@ -377,11 +378,13 @@ public static Mono<ReactorNettyClient> connect(ConnectionProvider connectionProv
377378
TcpClient tcpClient = TcpClient.create(connectionProvider).remoteAddress(() -> socketAddress);
378379

379380
if (!(socketAddress instanceof InetSocketAddress)) {
380-
tcpClient = tcpClient.runOn(new SocketLoopResources(), true);
381+
tcpClient = tcpClient.runOn(new SocketLoopResources(connectionSettings.hasLoopResources() ? connectionSettings.getRequiredLoopResources() : TcpResources.get()), true);
381382
} else {
382-
if (connectionSettings.hasTcpLoopResources()) {
383-
tcpClient = tcpClient.runOn(connectionSettings.getTcpLoopResources());
383+
384+
if (connectionSettings.hasLoopResources()) {
385+
tcpClient = tcpClient.runOn(connectionSettings.getRequiredLoopResources());
384386
}
387+
385388
tcpClient = tcpClient.option(ChannelOption.SO_KEEPALIVE, connectionSettings.isTcpKeepAlive());
386389
tcpClient = tcpClient.option(ChannelOption.TCP_NODELAY, connectionSettings.isTcpNoDelay());
387390
}
@@ -597,7 +600,11 @@ static class SocketLoopResources implements LoopResources {
597600
epoll = epollCheck;
598601
}
599602

600-
private final LoopResources delegate = TcpResources.get();
603+
private final LoopResources delegate;
604+
605+
public SocketLoopResources(LoopResources delegate) {
606+
this.delegate = delegate;
607+
}
601608

602609
@SuppressWarnings("unchecked")
603610
private static Class<? extends Channel> findClass(String className) {

0 commit comments

Comments
 (0)