Skip to content

Commit 5cb9344

Browse files
Added Option to set reactor.netty.resources.ConnectionProvider
1 parent 3449c84 commit 5cb9344

File tree

4 files changed

+48
-7
lines changed

4 files changed

+48
-7
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ Mono<Connection> connectionMono = Mono.from(connectionFactory.create());
8888
| `trustStoreType` | Type of the TrustStore. Defaults to `KeyStore.getDefaultType()`. _(Optional)_
8989
| `trustStore` | Path to the certificate TrustStore file. _(Optional)_
9090
| `trustStorePassword` | Password used to check the integrity of the TrustStore data. _(Optional)_
91+
| `connectionProvider` | Set the `reactor.netty.resources.ConnectionProvider` to be used when creating the connection. Defaults to `ConnectionProvider.newConnection()`. _(Optional)_ |
9192

9293

9394
**Programmatic Configuration**

src/main/java/io/r2dbc/mssql/MssqlConnectionConfiguration.java

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,16 @@ public final class MssqlConnectionConfiguration {
119119

120120
private final String username;
121121

122+
@Nullable
123+
private final ConnectionProvider connectionProvider;
124+
122125
private MssqlConnectionConfiguration(@Nullable String applicationName, @Nullable UUID connectionId, Duration connectTimeout, @Nullable String database, String host, String hostNameInCertificate,
123126
@Nullable Duration lockWaitTimeout, CharSequence password, Predicate<String> preferCursoredExecution, int port, boolean sendStringParametersAsUnicode,
124127
boolean ssl,
125128
Function<SslContextBuilder, SslContextBuilder> sslContextBuilderCustomizer,
126129
@Nullable Function<SslContextBuilder, SslContextBuilder> sslTunnelSslContextBuilderCustomizer, boolean tcpKeepAlive, boolean tcpNoDelay,
127130
boolean trustServerCertificate, @Nullable File trustStore, @Nullable String trustStoreType,
128-
@Nullable char[] trustStorePassword, String username) {
131+
@Nullable char[] trustStorePassword, String username, @Nullable ConnectionProvider connectionProvider) {
129132

130133
this.applicationName = applicationName;
131134
this.connectionId = connectionId;
@@ -148,6 +151,7 @@ private MssqlConnectionConfiguration(@Nullable String applicationName, @Nullable
148151
this.trustStoreType = trustStoreType;
149152
this.trustStorePassword = trustStorePassword;
150153
this.username = Assert.requireNonNull(username, "username must not be null");
154+
this.connectionProvider = connectionProvider;
151155
}
152156

153157
/**
@@ -185,12 +189,14 @@ MssqlConnectionConfiguration withRedirect(Redirect redirect) {
185189
return new MssqlConnectionConfiguration(this.applicationName, this.connectionId, this.connectTimeout, this.database, redirectServerName, hostNameInCertificate, this.lockWaitTimeout,
186190
this.password,
187191
this.preferCursoredExecution, redirect.getPort(), this.sendStringParametersAsUnicode, this.ssl, this.sslContextBuilderCustomizer,
188-
this.sslTunnelSslContextBuilderCustomizer, this.tcpKeepAlive, this.tcpNoDelay, this.trustServerCertificate, this.trustStore, this.trustStoreType, this.trustStorePassword, this.username);
192+
this.sslTunnelSslContextBuilderCustomizer, this.tcpKeepAlive, this.tcpNoDelay, this.trustServerCertificate, this.trustStore, this.trustStoreType, this.trustStorePassword, this.username,
193+
this.connectionProvider);
189194
}
190195

191196
ClientConfiguration toClientConfiguration() {
192197
return new DefaultClientConfiguration(this.connectTimeout, this.host, this.hostNameInCertificate, this.port, this.ssl, this.sslContextBuilderCustomizer,
193-
this.sslTunnelSslContextBuilderCustomizer, this.tcpKeepAlive, this.tcpNoDelay, this.trustServerCertificate, this.trustStore, this.trustStoreType, this.trustStorePassword);
198+
this.sslTunnelSslContextBuilderCustomizer, this.tcpKeepAlive, this.tcpNoDelay, this.trustServerCertificate, this.trustStore, this.trustStoreType, this.trustStorePassword,
199+
this.connectionProvider);
194200
}
195201

196202
ConnectionOptions toConnectionOptions() {
@@ -387,6 +393,9 @@ public static final class Builder {
387393
@Nullable
388394
private char[] trustStorePassword;
389395

396+
@Nullable
397+
private ConnectionProvider connectionProvider;
398+
390399
private Builder() {
391400
}
392401

@@ -703,6 +712,18 @@ public Builder username(String username) {
703712
return this;
704713
}
705714

715+
/**
716+
* Configure the {@link ConnectionProvider} to be used with Netty
717+
*
718+
* @param connectionProvider the connection provider
719+
* @return this {@link Builder}
720+
* @since 1.1.0
721+
*/
722+
public Builder connectionProvider(ConnectionProvider connectionProvider) {
723+
this.connectionProvider = connectionProvider;
724+
return this;
725+
}
726+
706727
/**
707728
* Returns a configured {@link MssqlConnectionConfiguration}.
708729
*
@@ -719,7 +740,7 @@ public MssqlConnectionConfiguration build() {
719740
this.preferCursoredExecution, this.port, this.sendStringParametersAsUnicode, this.ssl, this.sslContextBuilderCustomizer,
720741
this.sslTunnelSslContextBuilderCustomizer, this.tcpKeepAlive,
721742
this.tcpNoDelay, this.trustServerCertificate, this.trustStore,
722-
this.trustStoreType, this.trustStorePassword, this.username);
743+
this.trustStoreType, this.trustStorePassword, this.username, this.connectionProvider);
723744
}
724745

725746
}
@@ -756,10 +777,14 @@ static class DefaultClientConfiguration implements ClientConfiguration {
756777
@Nullable
757778
private final char[] trustStorePassword;
758779

780+
@Nullable
781+
private final ConnectionProvider connectionProvider;
782+
759783
DefaultClientConfiguration(Duration connectTimeout, String host, String hostNameInCertificate, int port, boolean ssl,
760784
Function<SslContextBuilder, SslContextBuilder> sslContextBuilderCustomizer,
761785
@Nullable Function<SslContextBuilder, SslContextBuilder> sslTunnelSslContextBuilderCustomizer, boolean tcpKeepAlive, boolean tcpNoDelay,
762-
boolean trustServerCertificate, @Nullable File trustStore, @Nullable String trustStoreType, @Nullable char[] trustStorePassword) {
786+
boolean trustServerCertificate, @Nullable File trustStore, @Nullable String trustStoreType, @Nullable char[] trustStorePassword,
787+
ConnectionProvider connectionProvider) {
763788

764789
this.connectTimeout = connectTimeout;
765790
this.host = host;
@@ -774,6 +799,7 @@ static class DefaultClientConfiguration implements ClientConfiguration {
774799
this.trustStore = trustStore;
775800
this.trustStoreType = trustStoreType;
776801
this.trustStorePassword = trustStorePassword;
802+
this.connectionProvider = connectionProvider;
777803
}
778804

779805
@Override
@@ -803,7 +829,8 @@ public boolean isTcpNoDelay() {
803829

804830
@Override
805831
public ConnectionProvider getConnectionProvider() {
806-
return ConnectionProvider.newConnection();
832+
return Optional.ofNullable(connectionProvider)
833+
.orElseGet(ConnectionProvider::newConnection);
807834
}
808835

809836
@Override

src/main/java/io/r2dbc/mssql/MssqlConnectionFactoryProvider.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io.r2dbc.spi.ConnectionFactoryOptions;
2222
import io.r2dbc.spi.ConnectionFactoryProvider;
2323
import io.r2dbc.spi.Option;
24+
import reactor.netty.resources.ConnectionProvider;
2425
import reactor.util.Logger;
2526
import reactor.util.Loggers;
2627

@@ -134,6 +135,13 @@ public final class MssqlConnectionFactoryProvider implements ConnectionFactoryPr
134135
*/
135136
public static final Option<char[]> TRUST_STORE_PASSWORD = Option.valueOf("trustStorePassword");
136137

138+
/**
139+
* Optional {@link reactor.netty.resources.ConnectionProvider} to control Netty configuration directly
140+
*
141+
* @since 1.1.0
142+
*/
143+
public static final Option<ConnectionProvider> CONNECTION_PROVIDER = Option.valueOf("connectionProvider");
144+
137145
/**
138146
* Driver option value.
139147
*/
@@ -194,6 +202,7 @@ public MssqlConnectionFactory create(ConnectionFactoryOptions connectionFactoryO
194202
mapper.from(TRUST_STORE).map(OptionMapper::toFile).to(builder::trustStore);
195203
mapper.fromTyped(TRUST_STORE_TYPE).to(builder::trustStoreType);
196204
mapper.from(TRUST_STORE_PASSWORD).map(it -> it instanceof String ? ((String) it).toCharArray() : (char[]) it).to(builder::trustStorePassword);
205+
mapper.fromTyped(CONNECTION_PROVIDER).to(builder::connectionProvider);
197206

198207
builder.host(connectionFactoryOptions.getRequiredValue(HOST).toString());
199208
builder.password((CharSequence) connectionFactoryOptions.getRequiredValue(PASSWORD));

src/test/java/io/r2dbc/mssql/MssqlConnectionConfigurationUnitTests.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.testcontainers.shaded.org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
2929
import org.testcontainers.shaded.org.bouncycastle.operator.ContentSigner;
3030
import org.testcontainers.shaded.org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
31+
import reactor.netty.resources.ConnectionProvider;
3132

3233
import java.io.File;
3334
import java.io.FileOutputStream;
@@ -86,6 +87,7 @@ void builderNoUsername() {
8687
void configuration() {
8788
UUID connectionId = UUID.randomUUID();
8889
Predicate<String> TRUE = s -> true;
90+
ConnectionProvider connectionProvider = ConnectionProvider.create("test");
8991
MssqlConnectionConfiguration configuration = MssqlConnectionConfiguration.builder()
9092
.connectionId(connectionId)
9193
.database("test-database")
@@ -95,6 +97,7 @@ void configuration() {
9597
.port(100)
9698
.username("test-username")
9799
.sendStringParametersAsUnicode(false)
100+
.connectionProvider(connectionProvider)
98101
.build();
99102

100103
assertThat(configuration)
@@ -105,7 +108,8 @@ void configuration() {
105108
.hasFieldOrPropertyWithValue("preferCursoredExecution", TRUE)
106109
.hasFieldOrPropertyWithValue("port", 100)
107110
.hasFieldOrPropertyWithValue("username", "test-username")
108-
.hasFieldOrPropertyWithValue("sendStringParametersAsUnicode", false);
111+
.hasFieldOrPropertyWithValue("sendStringParametersAsUnicode", false)
112+
.hasFieldOrPropertyWithValue("connectionProvider", connectionProvider);
109113
}
110114

111115
@Test

0 commit comments

Comments
 (0)