use of com.nike.fastbreak.CircuitBreakerDelegate in project riposte by Nike-Inc.
the class AsyncHttpClientHelper method getCircuitBreaker.
protected Optional<CircuitBreaker<Response>> getCircuitBreaker(RequestBuilderWrapper requestBuilderWrapper) {
if (requestBuilderWrapper.disableCircuitBreaker)
return Optional.empty();
// custom one is not specified.
if (requestBuilderWrapper.customCircuitBreaker.isPresent())
return requestBuilderWrapper.customCircuitBreaker;
// No custom circuit breaker. Use the default for the given request's host.
Uri uri = Uri.create(requestBuilderWrapper.url);
String host = uri.getHost();
EventLoop nettyEventLoop = requestBuilderWrapper.getCtx() == null ? null : requestBuilderWrapper.getCtx().channel().eventLoop();
CircuitBreaker<Integer> defaultStatusCodeCircuitBreaker = getDefaultHttpStatusCodeCircuitBreakerForKey(host, Optional.ofNullable(nettyEventLoop), Optional.ofNullable(nettyEventLoop));
return Optional.of(new CircuitBreakerDelegate<>(defaultStatusCodeCircuitBreaker, response -> (response == null ? null : response.getStatusCode())));
}
use of com.nike.fastbreak.CircuitBreakerDelegate in project riposte by Nike-Inc.
the class ProxyRouterEndpointExecutionHandler method getCircuitBreaker.
protected Optional<CircuitBreaker<HttpResponse>> getCircuitBreaker(DownstreamRequestFirstChunkInfo downstreamReqFirstChunkInfo, ChannelHandlerContext ctx) {
if (downstreamReqFirstChunkInfo == null || downstreamReqFirstChunkInfo.disableCircuitBreaker)
return Optional.empty();
// custom one is not specified.
if (downstreamReqFirstChunkInfo.customCircuitBreaker.isPresent())
return downstreamReqFirstChunkInfo.customCircuitBreaker;
// No custom circuit breaker. Use the default for the given request's host.
EventLoop nettyEventLoop = ctx.channel().eventLoop();
CircuitBreaker<Integer> defaultStatusCodeCircuitBreaker = getDefaultHttpStatusCodeCircuitBreakerForKey(downstreamReqFirstChunkInfo.host, Optional.ofNullable(nettyEventLoop), Optional.ofNullable(nettyEventLoop));
return Optional.of(new CircuitBreakerDelegate<>(defaultStatusCodeCircuitBreaker, httpResponse -> (httpResponse == null ? null : httpResponse.getStatus().code())));
}
use of com.nike.fastbreak.CircuitBreakerDelegate in project riposte by Nike-Inc.
the class AsyncHttpClientHelperTest method getCircuitBreaker_returns_CircuitBreakerDelegate_wrapping_default_CircuitBreakerForHttpStatusCode_using_host_as_the_key.
@DataProvider(value = { "true", "false" }, splitBy = "\\|")
@Test
public void getCircuitBreaker_returns_CircuitBreakerDelegate_wrapping_default_CircuitBreakerForHttpStatusCode_using_host_as_the_key(boolean useNettyEventLoop) {
// given
String host = UUID.randomUUID().toString();
String url = "http://" + host + "/some/path";
String method = "GET";
AsyncHttpClient.BoundRequestBuilder reqMock = mock(AsyncHttpClient.BoundRequestBuilder.class);
Optional<CircuitBreaker<Response>> customCb = Optional.empty();
RequestBuilderWrapper rbw = new RequestBuilderWrapper(url, method, reqMock, customCb, false);
if (useNettyEventLoop)
rbw.setCtx(ctxMock);
// when
Optional<CircuitBreaker<Response>> result = helperSpy.getCircuitBreaker(rbw);
// then
assertThat(result).isPresent();
assertThat(result.get()).isInstanceOf(CircuitBreakerDelegate.class);
CircuitBreakerDelegate<Response, Integer> wrapper = (CircuitBreakerDelegate) result.get();
CircuitBreaker<Integer> delegate = (CircuitBreaker<Integer>) Whitebox.getInternalState(wrapper, "delegate");
Function<Response, Integer> eventConverter = (Function<Response, Integer>) Whitebox.getInternalState(wrapper, "eventConverter");
assertThat(delegate).isSameAs(CircuitBreakerForHttpStatusCode.getDefaultHttpStatusCodeCircuitBreakerForKey(host));
Response responseMock = mock(Response.class);
doReturn(42).when(responseMock).getStatusCode();
assertThat(eventConverter.apply(responseMock)).isEqualTo(42);
assertThat(eventConverter.apply(null)).isNull();
if (useNettyEventLoop) {
assertThat(Whitebox.getInternalState(delegate, "scheduler")).isEqualTo(eventLoopMock);
assertThat(Whitebox.getInternalState(delegate, "stateChangeNotificationExecutor")).isEqualTo(eventLoopMock);
} else {
assertThat(Whitebox.getInternalState(delegate, "scheduler")).isNotEqualTo(eventLoopMock);
assertThat(Whitebox.getInternalState(delegate, "stateChangeNotificationExecutor")).isNotEqualTo(eventLoopMock);
}
}
Aggregations