use of io.reactivex.netty.protocol.http.client.HttpClientResponse in project ribbon by Netflix.
the class LoadBalancingHttpClient method requestToOperation.
/**
* Convert an HttpClientRequest to a ServerOperation
*
* @param server
* @param request
* @param rxClientConfig
* @return
*/
protected ServerOperation<HttpClientResponse<O>> requestToOperation(final HttpClientRequest<I> request, final ClientConfig rxClientConfig) {
Preconditions.checkNotNull(request);
return new ServerOperation<HttpClientResponse<O>>() {
final AtomicInteger count = new AtomicInteger(0);
@Override
public Observable<HttpClientResponse<O>> call(Server server) {
HttpClient<I, O> rxClient = getOrCreateRxClient(server);
setHostHeader(request, server.getHost());
Observable<HttpClientResponse<O>> o;
if (rxClientConfig != null) {
o = rxClient.submit(request, rxClientConfig);
} else {
o = rxClient.submit(request);
}
return o.concatMap(new Func1<HttpClientResponse<O>, Observable<HttpClientResponse<O>>>() {
@Override
public Observable<HttpClientResponse<O>> call(HttpClientResponse<O> t1) {
if (t1.getStatus().code() / 100 == 4 || t1.getStatus().code() / 100 == 5)
return responseToErrorPolicy.call(t1, backoffStrategy.call(count.getAndIncrement()));
else
return Observable.just(t1);
}
});
}
};
}
use of io.reactivex.netty.protocol.http.client.HttpClientResponse in project ribbon by Netflix.
the class NettyClientTest method testLoadBalancerThrottle.
@Test
public void testLoadBalancerThrottle() throws Exception {
HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/throttle");
IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().set(IClientConfigKey.Keys.MaxAutoRetriesNextServer, 1).set(IClientConfigKey.Keys.OkToRetryOnAllOperations, true);
BaseLoadBalancer lb = new BaseLoadBalancer(new DummyPing(), new AvailabilityFilteringRule());
LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config);
Server server = new Server(host, port);
lb.setServersList(Lists.newArrayList(server, server, server));
Observable<HttpClientResponse<ByteBuf>> response = lbObservables.submit(request);
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
response.subscribe(new Action1<HttpClientResponse<ByteBuf>>() {
@Override
public void call(HttpClientResponse<ByteBuf> t1) {
System.err.println("Get response: " + t1.getStatus().code());
latch.countDown();
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable t1) {
error.set(t1);
latch.countDown();
}
}, new Action0() {
@Override
public void call() {
Thread.dumpStack();
latch.countDown();
}
});
latch.await();
assertTrue(error.get() instanceof ClientException);
ClientException ce = (ClientException) error.get();
assertTrue(ce.toString(), ce.getErrorType() == ClientException.ErrorType.NUMBEROF_RETRIES_NEXTSERVER_EXCEEDED);
assertEquals(2, lbObservables.getServerStats(server).getSuccessiveConnectionFailureCount());
}
use of io.reactivex.netty.protocol.http.client.HttpClientResponse in project ribbon by Netflix.
the class NettyClientTest method testPoolReuse.
@Test
public void testPoolReuse() throws Exception {
HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet(SERVICE_URI + "testAsync/person");
LoadBalancingHttpClient<ByteBuf, ByteBuf> observableClient = RibbonTransport.newHttpClient(IClientConfig.Builder.newBuilder().withDefaultValues().withMaxAutoRetries(1).withMaxAutoRetriesNextServer(1).build());
Observable<HttpClientResponse<ByteBuf>> response = observableClient.submit(request);
Person person = getPersonObservable(response).toBlocking().single();
assertEquals(EmbeddedResources.defaultPerson, person);
response = observableClient.submit(request);
person = getPersonObservable(response).toBlocking().single();
assertEquals(EmbeddedResources.defaultPerson, person);
final HttpClientListener listener = observableClient.getListener();
assertEquals(2, listener.getPoolAcquires());
waitUntilTrueOrTimeout(1000, new Func0<Boolean>() {
@Override
public Boolean call() {
return listener.getPoolReleases() == 2;
}
});
assertEquals(1, listener.getConnectionCount());
assertEquals(1, listener.getPoolReuse());
}
use of io.reactivex.netty.protocol.http.client.HttpClientResponse in project ribbon by Netflix.
the class ListenerTest method testAbortedExecutionOnServer.
@Test
public void testAbortedExecutionOnServer() {
IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "100").withProperty(CommonClientConfigKey.MaxAutoRetries, 1).withProperty(CommonClientConfigKey.MaxAutoRetriesNextServer, 1);
HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/person");
Server badServer = new Server("localhost:12345");
Server badServer2 = new Server("localhost:34567");
List<Server> servers = Lists.newArrayList(badServer, badServer2);
BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder().withRule(new AvailabilityFilteringRule()).withPing(new DummyPing()).buildFixedServerListLoadBalancer(servers);
IClientConfig overrideConfig = DefaultClientConfigImpl.getEmptyConfig();
TestExecutionListener listener = new TestExecutionListener(request, overrideConfig) {
@Override
public void onStartWithServer(ExecutionContext context, ExecutionInfo info) {
throw new AbortExecutionException("exit now");
}
};
List<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>> listeners = Lists.<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>>newArrayList(listener);
LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(lb, config, new NettyHttpLoadBalancerErrorHandler(config), listeners);
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Throwable> ref = new AtomicReference<Throwable>();
client.submit(request, null, overrideConfig).subscribe(new Action1<HttpClientResponse<ByteBuf>>() {
@Override
public void call(HttpClientResponse<ByteBuf> byteBufHttpClientResponse) {
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
ref.set(throwable);
latch.countDown();
}
});
try {
latch.await(500, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
assertTrue(ref.get() instanceof AbortExecutionException);
}
use of io.reactivex.netty.protocol.http.client.HttpClientResponse in project ribbon by Netflix.
the class NettyClientTest method testPostWithByteBuf.
@Test
public void testPostWithByteBuf() throws Exception {
Person myPerson = new Person("netty", 5);
ObjectMapper mapper = new ObjectMapper();
byte[] raw = mapper.writeValueAsBytes(myPerson);
ByteBuf buffer = Unpooled.copiedBuffer(raw);
HttpClientRequest<ByteBuf> request = HttpClientRequest.createPost(SERVICE_URI + "testAsync/person").withHeader("Content-type", "application/json").withHeader("Content-length", String.valueOf(raw.length)).withContent(buffer);
LoadBalancingHttpClient<ByteBuf, ByteBuf> observableClient = RibbonTransport.newHttpClient(DefaultClientConfigImpl.getClientConfigWithDefaultValues().set(CommonClientConfigKey.ReadTimeout, 10000));
Observable<HttpClientResponse<ByteBuf>> response = observableClient.submit(request);
Person person = getPersonObservable(response).toBlocking().single();
assertEquals(myPerson, person);
}
Aggregations