use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ClientInterceptor in project jetcd by coreos.
the class ClientConnectionManager method defaultChannelBuilder.
@SuppressWarnings("rawtypes")
ManagedChannelBuilder<?> defaultChannelBuilder(String target) {
if (target == null) {
throw new IllegalArgumentException("At least one endpoint should be provided");
}
final Vertx vertx = Vertx.vertx();
final VertxChannelBuilder channelBuilder = VertxChannelBuilder.forTarget(vertx, target);
if (builder.authority() != null) {
channelBuilder.overrideAuthority(builder.authority());
}
if (builder.maxInboundMessageSize() != null) {
channelBuilder.maxInboundMessageSize(builder.maxInboundMessageSize());
}
if (builder.sslContext() != null) {
channelBuilder.nettyBuilder().negotiationType(NegotiationType.TLS);
channelBuilder.nettyBuilder().sslContext(builder.sslContext());
} else {
channelBuilder.nettyBuilder().negotiationType(NegotiationType.PLAINTEXT);
}
if (builder.keepaliveTime() != null) {
channelBuilder.keepAliveTime(builder.keepaliveTime().toMillis(), TimeUnit.MILLISECONDS);
}
if (builder.keepaliveTimeout() != null) {
channelBuilder.keepAliveTimeout(builder.keepaliveTimeout().toMillis(), TimeUnit.MILLISECONDS);
}
if (builder.keepaliveWithoutCalls() != null) {
channelBuilder.keepAliveWithoutCalls(builder.keepaliveWithoutCalls());
}
if (builder.connectTimeout() != null) {
channelBuilder.nettyBuilder().withOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, (int) builder.connectTimeout().toMillis());
}
if (builder.loadBalancerPolicy() != null) {
channelBuilder.defaultLoadBalancingPolicy(builder.loadBalancerPolicy());
} else {
channelBuilder.defaultLoadBalancingPolicy("pick_first");
}
if (builder.headers() != null) {
channelBuilder.intercept(new ClientInterceptor() {
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
@Override
public void start(Listener<RespT> responseListener, Metadata headers) {
builder.headers().forEach((BiConsumer<Metadata.Key, Object>) headers::put);
super.start(responseListener, headers);
}
};
}
});
}
if (builder.interceptors() != null) {
channelBuilder.intercept(builder.interceptors());
}
return channelBuilder;
}
use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ClientInterceptor in project grpc-java by grpc.
the class ManagedChannelImplTest method interceptor.
@Test
public void interceptor() throws Exception {
final AtomicLong atomic = new AtomicLong();
ClientInterceptor interceptor = new ClientInterceptor() {
@Override
public <RequestT, ResponseT> ClientCall<RequestT, ResponseT> interceptCall(MethodDescriptor<RequestT, ResponseT> method, CallOptions callOptions, Channel next) {
atomic.set(1);
return next.newCall(method, callOptions);
}
};
createChannel(interceptor);
assertNotNull(channel.newCall(method, CallOptions.DEFAULT));
assertEquals(1, atomic.get());
}
use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ClientInterceptor in project grpc-java by grpc.
the class ManagedChannelImplIdlenessTest method setUp.
@Before
// For NameResolver.Listener
@SuppressWarnings("deprecation")
public void setUp() {
LoadBalancerRegistry.getDefaultRegistry().register(mockLoadBalancerProvider);
when(mockNameResolver.getServiceAuthority()).thenReturn(AUTHORITY);
when(mockNameResolverFactory.newNameResolver(any(URI.class), any(NameResolver.Args.class))).thenReturn(mockNameResolver);
when(mockTransportFactory.getScheduledExecutorService()).thenReturn(timer.getScheduledExecutorService());
ManagedChannelImplBuilder builder = new ManagedChannelImplBuilder("fake://target", new UnsupportedClientTransportFactoryBuilder(), null);
builder.nameResolverFactory(mockNameResolverFactory).defaultLoadBalancingPolicy(MOCK_POLICY_NAME).idleTimeout(IDLE_TIMEOUT_SECONDS, TimeUnit.SECONDS).userAgent(USER_AGENT);
builder.executorPool = executorPool;
channel = new ManagedChannelImpl(builder, mockTransportFactory, new FakeBackoffPolicyProvider(), oobExecutorPool, timer.getStopwatchSupplier(), Collections.<ClientInterceptor>emptyList(), TimeProvider.SYSTEM_TIME_PROVIDER);
newTransports = TestUtils.captureTransports(mockTransportFactory);
for (int i = 0; i < 2; i++) {
ArrayList<SocketAddress> addrs = Lists.newArrayList();
for (int j = 0; j < 2; j++) {
addrs.add(new FakeSocketAddress("servergroup" + i + "server" + j));
}
servers.add(new EquivalentAddressGroup(addrs));
}
verify(mockNameResolverFactory).newNameResolver(any(URI.class), any(NameResolver.Args.class));
// Verify the initial idleness
verify(mockLoadBalancerProvider, never()).newLoadBalancer(any(Helper.class));
verify(mockTransportFactory, never()).newClientTransport(any(SocketAddress.class), any(ClientTransportFactory.ClientTransportOptions.class), any(ChannelLogger.class));
verify(mockNameResolver, never()).start(any(NameResolver.Listener.class));
verify(mockNameResolver, never()).start(any(NameResolver.Listener2.class));
}
use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ClientInterceptor in project grpc-java by grpc.
the class ConfigSelectingClientCallTest method configSelectorInterceptsCall.
@Test
public void configSelectorInterceptsCall() {
Map<String, ?> rawMethodConfig = ImmutableMap.of("retryPolicy", ImmutableMap.of("maxAttempts", 3.0D, "initialBackoff", "1s", "maxBackoff", "10s", "backoffMultiplier", 1.5D, "retryableStatusCodes", ImmutableList.of("UNAVAILABLE")));
final MethodInfo methodInfo = new MethodInfo(rawMethodConfig, true, 4, 4);
final Metadata.Key<String> metadataKey = Metadata.Key.of("test", Metadata.ASCII_STRING_MARSHALLER);
final CallOptions.Key<String> callOptionsKey = CallOptions.Key.create("test");
InternalConfigSelector configSelector = new InternalConfigSelector() {
@Override
public Result selectConfig(final PickSubchannelArgs args) {
ManagedChannelServiceConfig config = new ManagedChannelServiceConfig(methodInfo, ImmutableMap.<String, MethodInfo>of(), ImmutableMap.<String, MethodInfo>of(), null, null, null);
return Result.newBuilder().setConfig(config).setInterceptor(// An interceptor that mutates CallOptions based on headers value.
new ClientInterceptor() {
String value = args.getHeaders().get(metadataKey);
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
callOptions = callOptions.withOption(callOptionsKey, value);
return next.newCall(method, callOptions);
}
}).build();
}
};
ClientCall<Void, Void> configSelectingClientCall = new ConfigSelectingClientCall<>(configSelector, channel, MoreExecutors.directExecutor(), method, CallOptions.DEFAULT.withAuthority("bar.authority"));
Metadata metadata = new Metadata();
metadata.put(metadataKey, "fooValue");
configSelectingClientCall.start(callListener, metadata);
assertThat(call.callOptions.getAuthority()).isEqualTo("bar.authority");
assertThat(call.callOptions.getOption(MethodInfo.KEY)).isEqualTo(methodInfo);
assertThat(call.callOptions.getOption(callOptionsKey)).isEqualTo("fooValue");
}
use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ClientInterceptor in project grpc-java by grpc.
the class TransportCompressionTest method createChannelBuilder.
@Override
protected NettyChannelBuilder createChannelBuilder() {
NettyChannelBuilder builder = NettyChannelBuilder.forAddress(getListenAddress()).maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE).decompressorRegistry(decompressors).compressorRegistry(compressors).intercept(new ClientInterceptor() {
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
final ClientCall<ReqT, RespT> call = next.newCall(method, callOptions);
return new ForwardingClientCall<ReqT, RespT>() {
@Override
protected ClientCall<ReqT, RespT> delegate() {
return call;
}
@Override
public void start(final ClientCall.Listener<RespT> responseListener, Metadata headers) {
ClientCall.Listener<RespT> listener = new ForwardingClientCallListener<RespT>() {
@Override
protected io.grpc.ClientCall.Listener<RespT> delegate() {
return responseListener;
}
@Override
public void onHeaders(Metadata headers) {
super.onHeaders(headers);
if (expectFzip) {
String encoding = headers.get(GrpcUtil.MESSAGE_ENCODING_KEY);
assertEquals(encoding, FZIPPER.getMessageEncoding());
}
}
};
super.start(listener, headers);
setMessageCompression(true);
}
};
}
}).usePlaintext();
// Disable the default census stats interceptor, use testing interceptor instead.
InternalNettyChannelBuilder.setStatsEnabled(builder, false);
return builder.intercept(createCensusStatsClientInterceptor());
}
Aggregations