Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions jetcd-core/src/main/java/io/etcd/jetcd/ClientBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public final class ClientBuilder implements Cloneable {
private Duration connectTimeout;
private boolean waitForReady = true;
private Vertx vertx;
private boolean useVertx = false;

ClientBuilder() {
}
Expand Down Expand Up @@ -709,6 +710,25 @@ public ClientBuilder waitForReady(boolean waitForReady) {
return this;
}

/**
* Returns whether to use Vertx for gRPC channel creation.
*
* @return true if using Vertx for gRPC channel creation.
*/
public boolean isUsingVertx() {
return useVertx;
}

/**
* Use Vertx for gRPC channel creation instead of default grpc-netty.
*
* @return this builder
*/
public ClientBuilder useVertx() {
useVertx = true;
return this;
}

/**
* Gets the Vertx instance.
*
Expand All @@ -729,6 +749,7 @@ public ClientBuilder vertx(Vertx vertx) {
Preconditions.checkArgument(vertx != null, "vertx can't be null");

this.vertx = vertx;
this.useVertx = true;

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.grpc.*;
import io.grpc.ForwardingClientCall.SimpleForwardingClientCall;
import io.grpc.netty.NegotiationType;
import io.grpc.netty.NettyChannelBuilder;
import io.grpc.stub.AbstractStub;
import io.netty.channel.ChannelOption;
import io.vertx.core.Vertx;
Expand Down Expand Up @@ -159,7 +160,15 @@ ManagedChannelBuilder<?> defaultChannelBuilder(String target) {
throw new IllegalArgumentException("At least one endpoint should be provided");
}

final VertxChannelBuilder channelBuilder = VertxChannelBuilder.forTarget(vertx(), target);
final ManagedChannelBuilder channelBuilder;
final NettyChannelBuilder nettyChannelBuilder;
if (builder.isUsingVertx()) {
channelBuilder = VertxChannelBuilder.forTarget(vertx(), target);
nettyChannelBuilder = ((VertxChannelBuilder) channelBuilder).nettyBuilder();
} else {
channelBuilder = NettyChannelBuilder.forTarget(target);
nettyChannelBuilder = (NettyChannelBuilder) channelBuilder;
}

if (builder.authority() != null) {
channelBuilder.overrideAuthority(builder.authority());
Expand All @@ -168,10 +177,10 @@ ManagedChannelBuilder<?> defaultChannelBuilder(String target) {
channelBuilder.maxInboundMessageSize(builder.maxInboundMessageSize());
}
if (builder.sslContext() != null) {
channelBuilder.nettyBuilder().negotiationType(NegotiationType.TLS);
channelBuilder.nettyBuilder().sslContext(builder.sslContext());
nettyChannelBuilder.negotiationType(NegotiationType.TLS);
nettyChannelBuilder.sslContext(builder.sslContext());
} else {
channelBuilder.nettyBuilder().negotiationType(NegotiationType.PLAINTEXT);
nettyChannelBuilder.negotiationType(NegotiationType.PLAINTEXT);
}

if (builder.keepaliveTime() != null) {
Expand All @@ -184,7 +193,7 @@ ManagedChannelBuilder<?> defaultChannelBuilder(String target) {
channelBuilder.keepAliveWithoutCalls(builder.keepaliveWithoutCalls());
}
if (builder.connectTimeout() != null) {
channelBuilder.nettyBuilder().withOption(ChannelOption.CONNECT_TIMEOUT_MILLIS,
nettyChannelBuilder.withOption(ChannelOption.CONNECT_TIMEOUT_MILLIS,
(int) builder.connectTimeout().toMillis());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import io.etcd.jetcd.ByteSequence;
import io.etcd.jetcd.Client;
import io.etcd.jetcd.ClientBuilder;
import io.vertx.grpc.VertxChannelBuilder;
import io.grpc.netty.NettyChannelBuilder;

import static io.etcd.jetcd.impl.TestUtil.bytesOf;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -75,10 +75,10 @@ public void testBuild_WithoutEndpoints() {
public void testMaxInboundMessageSize() throws URISyntaxException {
final int value = 1024 + new Random().nextInt(10);
final ClientBuilder builder = Client.builder().endpoints(new URI("http://127.0.0.1:2379")).maxInboundMessageSize(value);
final VertxChannelBuilder channelBuilder = (VertxChannelBuilder) new ClientConnectionManager(builder)
final NettyChannelBuilder channelBuilder = (NettyChannelBuilder) new ClientConnectionManager(builder)
.defaultChannelBuilder();

assertThat(channelBuilder.nettyBuilder()).hasFieldOrPropertyWithValue("maxInboundMessageSize", value);
assertThat(channelBuilder).hasFieldOrPropertyWithValue("maxInboundMessageSize", value);
}

@Test
Expand Down