use of io.servicetalk.transport.api.HostAndPort in project servicetalk by apple.
the class DefaultSingleAddressHttpClientBuilder method setFallbackHostAndPort.
private void setFallbackHostAndPort(HttpClientConfig config, U address) {
if (address instanceof HostAndPort) {
HostAndPort hostAndPort = (HostAndPort) address;
config.fallbackPeerHost(hostAndPort.hostName());
config.fallbackPeerPort(hostAndPort.port());
} else if (address instanceof InetSocketAddress) {
InetSocketAddress inetSocketAddress = (InetSocketAddress) address;
config.fallbackPeerHost(inetSocketAddress.getHostString());
config.fallbackPeerPort(inetSocketAddress.getPort());
} else {
CharSequence cs = hostToCharSequenceFunction.apply(address);
if (cs == null) {
config.fallbackPeerHost(null);
config.fallbackPeerPort(-1);
} else {
int colon = CharSequences.indexOf(cs, ':', 0);
if (colon < 0) {
config.fallbackPeerHost(cs.toString());
config.fallbackPeerPort(-1);
} else if (cs.charAt(0) == '[') {
colon = CharSequences.indexOf(cs, ']', 1);
if (colon < 0) {
throw new IllegalArgumentException("unable to find end ']' of IPv6 address: " + cs);
}
config.fallbackPeerHost(cs.subSequence(1, colon).toString());
++colon;
if (colon >= cs.length()) {
config.fallbackPeerPort(-1);
} else if (cs.charAt(colon) != ':') {
throw new IllegalArgumentException("':' expected after ']' for IPv6 address: " + cs);
} else {
config.fallbackPeerPort(parseInt(cs.subSequence(colon + 1, cs.length()).toString()));
}
} else {
config.fallbackPeerHost(cs.subSequence(0, colon).toString());
config.fallbackPeerPort(parseInt(cs.subSequence(colon + 1, cs.length()).toString()));
}
}
}
}
use of io.servicetalk.transport.api.HostAndPort in project servicetalk by apple.
the class BasicAuthStrategyInfluencerTest method setup.
private BlockingHttpClient setup(boolean noOffloadsInfluence) throws Exception {
ioExecutor = NettyIoExecutors.createIoExecutor(IO_EXECUTOR_NAME_PREFIX);
HttpServerBuilder serverBuilder = HttpServers.forAddress(localAddress(0));
when(credentialsVerifier.apply(anyString(), anyString())).thenReturn(succeeded("success"));
when(credentialsVerifier.closeAsync()).thenReturn(completed());
when(credentialsVerifier.closeAsyncGracefully()).thenReturn(completed());
when(credentialsVerifier.requiredOffloads()).thenCallRealMethod();
CredentialsVerifier<String> verifier = credentialsVerifier;
if (noOffloadsInfluence) {
verifier = new InfluencingVerifier(verifier, offloadNone());
serverBuilder.executionStrategy(offloadNone());
}
serverBuilder.appendServiceFilter(new BasicAuthHttpServiceFilter.Builder<>(verifier, "dummy").buildServer());
serverBuilder.ioExecutor(ioExecutor);
service = new OffloadCheckingService();
serverContext = serverBuilder.listenStreamingAndAwait(service);
SingleAddressHttpClientBuilder<HostAndPort, InetSocketAddress> clientBuilder = HttpClients.forSingleAddress(serverHostAndPort(serverContext));
this.client = clientBuilder.buildBlocking();
return this.client;
}
use of io.servicetalk.transport.api.HostAndPort in project servicetalk by apple.
the class KeepAliveTest method setUp.
private void setUp(final boolean keepAlivesFromClient, final Duration keepAliveIdleFor, final Duration idleTimeout) throws Exception {
this.idleTimeoutMillis = idleTimeout.toMillis();
GrpcServerBuilder serverBuilder = forAddress(localAddress(0)).initializeHttp(builder -> {
builder.executor(SERVER_CTX.executor()).ioExecutor(SERVER_CTX.ioExecutor()).executionStrategy(defaultStrategy());
if (!keepAlivesFromClient) {
builder.protocols(h2Config(keepAliveIdleFor));
} else {
builder.socketOption(IDLE_TIMEOUT, idleTimeoutMillis).protocols(h2Config(null));
}
});
ctx = serverBuilder.listenAndAwait(new ServiceFactory(new InfiniteStreamsService()));
GrpcClientBuilder<HostAndPort, InetSocketAddress> clientBuilder = GrpcClients.forAddress(serverHostAndPort(ctx)).initializeHttp(builder -> builder.executor(CLIENT_CTX.executor()).ioExecutor(CLIENT_CTX.ioExecutor()).executionStrategy(defaultStrategy()));
if (keepAlivesFromClient) {
clientBuilder.initializeHttp(builder -> builder.protocols(h2Config(keepAliveIdleFor)));
} else {
clientBuilder.initializeHttp(builder -> builder.socketOption(IDLE_TIMEOUT, idleTimeoutMillis).protocols(h2Config(null)));
}
client = clientBuilder.build(new ClientFactory());
}
use of io.servicetalk.transport.api.HostAndPort in project servicetalk by apple.
the class GrpcProvidersTest method testGrpcClientBuilderProvider.
@Test
void testGrpcClientBuilderProvider() throws Exception {
try (ServerContext serverContext = GrpcServers.forAddress(localAddress(0)).listenAndAwait(BlockingGreeterServiceImpl.INSTANCE)) {
HostAndPort serverAddress = serverHostAndPort(serverContext);
TestGrpcClientBuilderProvider.MODIFY_FOR_ADDRESS.set(serverAddress);
try (Greeter.BlockingGreeterClient client = GrpcClients.forAddress(serverAddress).buildBlocking(new ClientFactory())) {
assertThat(TestGrpcClientBuilderProvider.BUILD_COUNTER.get(), is(1));
HelloReply reply = client.sayHello(HelloRequest.newBuilder().setName("foo").build());
assertThat(reply.getMessage(), is(equalTo("reply to foo")));
assertThat(TestGrpcClientBuilderProvider.CONNECTION_COUNTER.get(), is(1));
}
}
}
use of io.servicetalk.transport.api.HostAndPort in project servicetalk by apple.
the class H2ConcurrencyControllerTest method setUp.
@BeforeEach
void setUp() throws Exception {
serverEventLoopGroup = createIoExecutor(1, "server-io").eventLoopGroup();
for (int i = 0; i < N_ITERATIONS; i++) {
latches[i] = new CountDownLatch(1);
}
AtomicBoolean secondAndMore = new AtomicBoolean();
serverAcceptorChannel = bindH2Server(serverEventLoopGroup, new ChannelInitializer<Http2StreamChannel>() {
@Override
protected void initChannel(Http2StreamChannel ch) {
// Respond only for the first request which is used to propagate MAX_CONCURRENT_STREAMS_VALUE
if (secondAndMore.compareAndSet(false, true)) {
ch.pipeline().addLast(new EchoHttp2Handler());
} else {
// Do not respond to any subsequent requests, only release the associated latch to notify the client
// that server received the request.
ch.pipeline().addLast(new SimpleChannelInboundHandler<Http2HeadersFrame>() {
@Override
protected void channelRead0(final ChannelHandlerContext ctx, final Http2HeadersFrame msg) {
String path = msg.headers().path().toString();
int i = parseInt(path.substring(1));
latches[i].countDown();
}
});
}
}
}, parentPipeline -> {
}, h2Builder -> {
h2Builder.initialSettings().maxConcurrentStreams(MAX_CONCURRENT_STREAMS_VALUE);
return h2Builder;
});
final HostAndPort serverAddress = of((InetSocketAddress) serverAcceptorChannel.localAddress());
client = forResolvedAddress(serverAddress).ioExecutor(CTX.ioExecutor()).executor(CTX.executor()).executionStrategy(defaultStrategy()).appendClientFilter(// All exceptions should be propagated
disableAutoRetries()).appendConnectionFilter(MulticastTransportEventsStreamingHttpConnectionFilter::new).appendConnectionFilter(connection -> new StreamingHttpConnectionFilter(connection) {
@Override
public Single<StreamingHttpResponse> request(StreamingHttpRequest request) {
return delegate().request(request).liftSync(subscriber -> new SingleSource.Subscriber<StreamingHttpResponse>() {
@Override
public void onSubscribe(final Cancellable cancellable) {
// Defer the cancel() signal to let the test thread start a new request
subscriber.onSubscribe(() -> CTX.executor().schedule(cancellable::cancel, ofMillis(100)));
}
@Override
public void onSuccess(@Nullable final StreamingHttpResponse result) {
subscriber.onSuccess(result);
}
@Override
public void onError(final Throwable t) {
subscriber.onError(t);
}
});
}
}).protocols(h2().enableFrameLogging("servicetalk-tests-h2-frame-logger", TRACE, () -> true).build()).build();
}
Aggregations