Skip to content

Commit

Permalink
working version
Browse files Browse the repository at this point in the history
  • Loading branch information
jrhee17 committed Nov 18, 2024
1 parent 8225f86 commit 756570c
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ public abstract class AbstractWebClientBuilder extends AbstractClientOptionsBuil
@Nullable
private final URI uri;
@Nullable
private final EndpointGroup endpointGroup;
@Nullable
private final ExecutionPreparation executionPreparation;
@Nullable
private final Scheme scheme;
Expand All @@ -49,7 +47,7 @@ public abstract class AbstractWebClientBuilder extends AbstractClientOptionsBuil
* Creates a new instance.
*/
protected AbstractWebClientBuilder() {
this(UNDEFINED_URI, null, null, null, null);
this(UNDEFINED_URI, null, null, null);
}

/**
Expand All @@ -59,7 +57,7 @@ protected AbstractWebClientBuilder() {
* in {@link SessionProtocol}
*/
protected AbstractWebClientBuilder(URI uri) {
this(validateUri(uri), null, null, null, null);
this(validateUri(uri), null, null, null);
}

/**
Expand All @@ -71,7 +69,7 @@ protected AbstractWebClientBuilder(URI uri) {
protected AbstractWebClientBuilder(SessionProtocol sessionProtocol, EndpointGroup endpointGroup,
@Nullable String path) {
this(null, validateSessionProtocol(sessionProtocol),
requireNonNull(endpointGroup, "endpointGroup"), null, path);
requireNonNull(endpointGroup, "endpointGroup"), path);
}

/**
Expand All @@ -83,22 +81,19 @@ protected AbstractWebClientBuilder(SessionProtocol sessionProtocol, EndpointGrou
protected AbstractWebClientBuilder(SerializationFormat serializationFormat,
ExecutionPreparation executionPreparation, @Nullable String path) {
this(null, Scheme.of(serializationFormat, SessionProtocol.UNDETERMINED),
null, requireNonNull(executionPreparation, "executionPreparation"), path);
requireNonNull(executionPreparation, "executionPreparation"), path);
}

/**
* Creates a new instance.
*/
protected AbstractWebClientBuilder(@Nullable URI uri, @Nullable Scheme scheme,
@Nullable EndpointGroup endpointGroup,
@Nullable ExecutionPreparation executionPreparation,
@Nullable String path) {
assert uri != null || (scheme != null && endpointGroup != null) ||
(executionPreparation != null && scheme != null);
assert uri != null || (scheme != null && executionPreparation != null);
assert path == null || uri == null;
this.uri = uri;
this.scheme = scheme;
this.endpointGroup = endpointGroup;
this.executionPreparation = executionPreparation;
this.path = validatePath(path);
}
Expand Down Expand Up @@ -174,13 +169,9 @@ protected final ClientBuilderParams clientBuilderParams(ClientOptions options) {
return ClientBuilderParams.of(uri, WebClient.class, options);
}
assert scheme != null;
assert executionPreparation != null;

if (executionPreparation != null) {
return ClientBuilderParams.of(scheme, executionPreparation, path, WebClient.class, options);
}

assert endpointGroup != null;
return ClientBuilderParams.of(scheme, endpointGroup, path, WebClient.class, options);
return ClientBuilderParams.of(scheme, executionPreparation, path, WebClient.class, options);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,15 @@ protected DecoratingClientFactory(ClientFactory delegate) {
* {@link ClientOptions} with the specified {@link ClientBuilderParams}. Note that {@code path} and
* {@link SerializationFormat} are always {@code "/"} and {@link SerializationFormat#NONE}.
*/
protected final HttpClient newHttpClient(ClientBuilderParams params, boolean autoInitializeEndpoint) {
protected final HttpClient newHttpClient(ClientBuilderParams params) {
final URI uri = params.uri();
final ClientBuilderParams newParams;
final ClientOptionValue<Boolean> value =
ClientOptions.AUTO_INITIALIZE_ENDPOINT.newValue(autoInitializeEndpoint);
final ClientOptions newOptions = params.options().toBuilder()
.factory(unwrap()).options(value)
.build();
if (Clients.isUndefinedUri(uri)) {
newParams = ClientBuilderParams.of(uri, HttpClient.class, newOptions);
newParams = ClientBuilderParams.of(uri, HttpClient.class, params.options());
} else {
final Scheme newScheme = Scheme.of(SerializationFormat.NONE, params.scheme().sessionProtocol());
newParams = ClientBuilderParams.of(newScheme, params.endpointGroup(),
null, HttpClient.class, newOptions);
newParams = ClientBuilderParams.of(newScheme, params.executionPreparation(),
null, HttpClient.class, params.options());
}

return (HttpClient) unwrap().newClient(newParams);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,18 @@ public final class WebSocketClientBuilder extends AbstractWebClientBuilder {
private boolean aggregateContinuation;

WebSocketClientBuilder(URI uri) {
super(validateUri(requireNonNull(uri, "uri")), null, null, null, null);
super(validateUri(requireNonNull(uri, "uri")), null, null, null);
setWebSocketDefaultOption();
}

WebSocketClientBuilder(Scheme scheme, EndpointGroup endpointGroup, @Nullable String path) {
super(null, validateScheme(requireNonNull(scheme, "scheme")), endpointGroup, null, path);
super(null, validateScheme(requireNonNull(scheme, "scheme")), endpointGroup, path);
setWebSocketDefaultOption();
}

WebSocketClientBuilder(ExecutionPreparation executionPreparation) {
super(null, Scheme.of(SerializationFormat.WS, SessionProtocol.UNDETERMINED),
null, requireNonNull(executionPreparation, "executionPreparation"), null);
requireNonNull(executionPreparation, "executionPreparation"), null);
setWebSocketDefaultOption();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public Object newClient(ClientBuilderParams params) {
final Scheme scheme = params.scheme();
final SerializationFormat serializationFormat = scheme.serializationFormat();
final ClientBuilderParams newParams = ClientBuilderParams.of(
Scheme.of(SerializationFormat.NONE, scheme.sessionProtocol()), params.endpointGroup(),
Scheme.of(SerializationFormat.NONE, scheme.sessionProtocol()), params.executionPreparation(),
params.absolutePathRef(), WebClient.class, params.options());
final WebClient webClient = (WebClient) unwrap().newClient(newParams);
return new UnaryGrpcClient(webClient, serializationFormat);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ public final class GrpcClientBuilder extends AbstractClientOptionsBuilder {

private final ImmutableList.Builder<ClientInterceptor> interceptors = ImmutableList.builder();
@Nullable
private final EndpointGroup endpointGroup;
@Nullable
private final ExecutionPreparation executionPreparation;

@Nullable
Expand All @@ -112,29 +110,17 @@ public final class GrpcClientBuilder extends AbstractClientOptionsBuilder {
GrpcClientBuilder(URI uri) {
requireNonNull(uri, "uri");
checkArgument(uri.getScheme() != null, "uri must have scheme: %s", uri);
endpointGroup = null;
executionPreparation = null;
this.uri = uri;
scheme = Scheme.parse(uri.getScheme());
validateOrSetSerializationFormat();
}

GrpcClientBuilder(Scheme scheme, EndpointGroup endpointGroup) {
requireNonNull(scheme, "scheme");
requireNonNull(endpointGroup, "endpointGroup");
uri = null;
this.scheme = scheme;
validateOrSetSerializationFormat();
this.endpointGroup = endpointGroup;
executionPreparation = endpointGroup;
}

GrpcClientBuilder(Scheme scheme, ExecutionPreparation executionPreparation) {
requireNonNull(scheme, "scheme");
requireNonNull(executionPreparation, "executionPreparation");
uri = null;
this.scheme = scheme;
endpointGroup = null;
this.executionPreparation = executionPreparation;
validateOrSetSerializationFormat();
}
Expand Down Expand Up @@ -446,9 +432,6 @@ public <T> T build(Class<T> clientType) {
uri = uri.resolve(prefix);
}
client = factory.newClient(ClientBuilderParams.of(uri, clientType, options));
} else if (endpointGroup != null) {
client = factory.newClient(ClientBuilderParams.of(scheme, endpointGroup,
prefix, clientType, options));
} else {
assert executionPreparation != null;
client = factory.newClient(ClientBuilderParams.of(scheme, executionPreparation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public Object newClient(ClientBuilderParams params) {

final ClientBuilderParams newParams =
addTrailersExtractor(params, options, serializationFormat);
final HttpClient httpClient = newHttpClient(newParams, false);
final HttpClient httpClient = newHttpClient(newParams);

final GrpcJsonMarshaller jsonMarshaller;
if (GrpcSerializationFormats.isJson(serializationFormat)) {
Expand Down
1 change: 1 addition & 0 deletions it/xds-clients/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ dependencies {
testImplementation project(':xds')
testImplementation project(':xds-testing-internal')
testImplementation project(':grpc')
testImplementation project(':grpc-protocol')
testImplementation project(':thrift0.13')
testImplementation project(':scala_2.13')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,17 @@
import com.google.common.collect.ImmutableList;

import com.linecorp.armeria.client.ClientOptions;
import com.linecorp.armeria.client.ClientRequestContext;
import com.linecorp.armeria.client.ClientRequestContextCaptor;
import com.linecorp.armeria.client.Clients;
import com.linecorp.armeria.client.grpc.GrpcClients;
import com.linecorp.armeria.client.grpc.protocol.UnaryGrpcClient;
import com.linecorp.armeria.common.HttpHeaders;
import com.linecorp.armeria.common.Scheme;
import com.linecorp.armeria.common.SessionProtocol;
import com.linecorp.armeria.common.grpc.GrpcSerializationFormats;
import com.linecorp.armeria.common.grpc.protocol.GrpcHeaderNames;
import com.linecorp.armeria.common.grpc.protocol.GrpcWebTrailers;
import com.linecorp.armeria.server.ServerBuilder;
import com.linecorp.armeria.server.grpc.GrpcService;
import com.linecorp.armeria.testing.junit5.server.ServerExtension;
Expand Down Expand Up @@ -120,4 +129,34 @@ void simpleClient() {
assertThat(stub.hello(HelloRequest.getDefaultInstance()).getMessage()).isEqualTo("Hello");
}
}

@Test
void unary() throws Exception {
final ConfigSource configSource = XdsTestResources.basicConfigSource(BOOTSTRAP_CLUSTER_NAME);
final URI uri = controlPlaneServer.httpUri();
final ClusterLoadAssignment loadAssignment =
XdsTestResources.loadAssignment(BOOTSTRAP_CLUSTER_NAME,
uri.getHost(), uri.getPort());
final Cluster bootstrapCluster =
XdsTestResources.createStaticCluster(BOOTSTRAP_CLUSTER_NAME, loadAssignment);
final Bootstrap bootstrap = XdsTestResources.bootstrap(configSource, bootstrapCluster);
try (XdsBootstrap xdsBootstrap = XdsBootstrap.of(bootstrap);
XdsExecutionPreparation preparation = XdsExecutionPreparation.of("listener", xdsBootstrap)) {
final UnaryGrpcClient client = Clients.newClient(Scheme.of(GrpcSerializationFormats.PROTO,
SessionProtocol.UNDETERMINED),
preparation, UnaryGrpcClient.class);
final HelloRequest request = HelloRequest.getDefaultInstance();

try (ClientRequestContextCaptor captor = Clients.newContextCaptor()) {
final byte[] responseBytes =
client.execute("/testing.grpc.TestService/Hello", request.toByteArray()).join();
final ClientRequestContext ctx = captor.get();
final HttpHeaders trailers = GrpcWebTrailers.get(ctx);
assertThat(trailers).isNotNull();
assertThat(trailers.getInt(GrpcHeaderNames.GRPC_STATUS)).isZero();
final HelloReply response = HelloReply.parseFrom(responseBytes);
assertThat(response.getMessage()).isEqualTo("Hello");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@

import com.linecorp.armeria.client.ClientOptions;
import com.linecorp.armeria.client.Clients;
import com.linecorp.armeria.client.logging.LoggingClient;
import com.linecorp.armeria.client.thrift.THttpClient;
import com.linecorp.armeria.client.thrift.ThriftClients;
import com.linecorp.armeria.common.RpcResponse;
import com.linecorp.armeria.common.thrift.ThriftSerializationFormats;
import com.linecorp.armeria.server.ServerBuilder;
import com.linecorp.armeria.server.grpc.GrpcService;
Expand Down Expand Up @@ -109,11 +112,38 @@ void basicCase() throws Exception {
XdsExecutionPreparation preparation = XdsExecutionPreparation.of("listener", xdsBootstrap)) {
Iface iface = ThriftClients.builder(ThriftSerializationFormats.BINARY, preparation)
.path("/thrift")
.decorator(LoggingClient.newDecorator())
.build(Iface.class);
assertThat(iface.sayHello("Hello, ")).isEqualTo("World");

iface = Clients.newDerivedClient(iface, ClientOptions.RESPONSE_TIMEOUT_MILLIS.newValue(10L));
assertThat(iface.sayHello("Hello, ")).isEqualTo("World");
}
}

@Test
void tHttpClient() throws Exception {
final ConfigSource configSource = XdsTestResources.basicConfigSource(BOOTSTRAP_CLUSTER_NAME);
final URI uri = controlPlaneServer.httpUri();
final ClusterLoadAssignment loadAssignment =
XdsTestResources.loadAssignment(BOOTSTRAP_CLUSTER_NAME,
uri.getHost(), uri.getPort());
final Cluster bootstrapCluster =
XdsTestResources.createStaticCluster(BOOTSTRAP_CLUSTER_NAME, loadAssignment);
final Bootstrap bootstrap = XdsTestResources.bootstrap(configSource, bootstrapCluster);
try (XdsBootstrap xdsBootstrap = XdsBootstrap.of(bootstrap);
XdsExecutionPreparation preparation = XdsExecutionPreparation.of("listener", xdsBootstrap)) {
THttpClient tHttpClient = ThriftClients.builder(ThriftSerializationFormats.BINARY, preparation)
.decorator(LoggingClient.newDecorator())
.path("/thrift")
.build(THttpClient.class);
RpcResponse res = tHttpClient.execute("", TestService.Iface.class, "sayHello", "World");
assertThat(res.get()).isEqualTo("World");

tHttpClient = Clients.newDerivedClient(tHttpClient,
ClientOptions.RESPONSE_TIMEOUT_MILLIS.newValue(10L));
res = tHttpClient.execute("", TestService.Iface.class, "sayHello", "World");
assertThat(res.get()).isEqualTo("World");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public Object newClient(ClientBuilderParams params) {
final Class<?> clientType = params.clientType();
final ClientOptions options = params.options();
final RpcClient delegate = options.decoration().rpcDecorate(
new THttpClientDelegate(newHttpClient(params, false),
new THttpClientDelegate(newHttpClient(params),
options, params.scheme().serializationFormat()));

if (clientType == THttpClient.class) {
Expand Down

0 comments on commit 756570c

Please sign in to comment.