use of io.github.resilience4j.timelimiter.event.TimeLimiterEvent in project resilience4j by resilience4j.
the class TimeLimiterConfigurationTest method testTimeLimiterRegistry.
@Test
public void testTimeLimiterRegistry() {
// Given
io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigurationProperties.InstanceProperties instanceProperties1 = new io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigurationProperties.InstanceProperties();
instanceProperties1.setTimeoutDuration(Duration.ofSeconds(3));
io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigurationProperties.InstanceProperties instanceProperties2 = new io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigurationProperties.InstanceProperties();
instanceProperties2.setTimeoutDuration(Duration.ofSeconds(2));
TimeLimiterConfigurationProperties timeLimiterConfigurationProperties = new TimeLimiterConfigurationProperties();
timeLimiterConfigurationProperties.getInstances().put("backend1", instanceProperties1);
timeLimiterConfigurationProperties.getInstances().put("backend2", instanceProperties2);
timeLimiterConfigurationProperties.setTimeLimiterAspectOrder(200);
TimeLimiterConfiguration timeLimiterConfiguration = new TimeLimiterConfiguration();
DefaultEventConsumerRegistry<TimeLimiterEvent> eventConsumerRegistry = new DefaultEventConsumerRegistry<>();
// When
TimeLimiterRegistry timeLimiterRegistry = timeLimiterConfiguration.timeLimiterRegistry(timeLimiterConfigurationProperties, eventConsumerRegistry, new CompositeRegistryEventConsumer<>(emptyList()), compositeTimeLimiterCustomizerTestInstance());
// Then
assertThat(timeLimiterConfigurationProperties.getTimeLimiterAspectOrder()).isEqualTo(200);
assertThat(timeLimiterRegistry.getAllTimeLimiters().size()).isEqualTo(2);
TimeLimiter timeLimiter1 = timeLimiterRegistry.timeLimiter("backend1");
assertThat(timeLimiter1).isNotNull();
assertThat(timeLimiter1.getTimeLimiterConfig().getTimeoutDuration()).isEqualTo(Duration.ofSeconds(3));
TimeLimiter timeLimiter2 = timeLimiterRegistry.timeLimiter("backend2");
assertThat(timeLimiter2).isNotNull();
assertThat(timeLimiter2.getTimeLimiterConfig().getTimeoutDuration()).isEqualTo(Duration.ofSeconds(2));
assertThat(eventConsumerRegistry.getAllEventConsumer()).hasSize(2);
}
use of io.github.resilience4j.timelimiter.event.TimeLimiterEvent in project resilience4j by resilience4j.
the class TimeLimiterConfigurationTest method testCreateTimeLimiterRegistryWithSharedConfigs.
@Test
public void testCreateTimeLimiterRegistryWithSharedConfigs() {
// Given
io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigurationProperties.InstanceProperties defaultProperties = new io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigurationProperties.InstanceProperties();
defaultProperties.setTimeoutDuration(Duration.ofSeconds(3));
defaultProperties.setCancelRunningFuture(true);
io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigurationProperties.InstanceProperties sharedProperties = new io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigurationProperties.InstanceProperties();
sharedProperties.setTimeoutDuration(Duration.ofSeconds(2));
sharedProperties.setCancelRunningFuture(false);
io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigurationProperties.InstanceProperties backendWithDefaultConfig = new io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigurationProperties.InstanceProperties();
backendWithDefaultConfig.setBaseConfig("default");
backendWithDefaultConfig.setTimeoutDuration(Duration.ofSeconds(5));
io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigurationProperties.InstanceProperties backendWithSharedConfig = new io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigurationProperties.InstanceProperties();
backendWithSharedConfig.setBaseConfig("sharedConfig");
backendWithSharedConfig.setCancelRunningFuture(true);
TimeLimiterConfigurationProperties timeLimiterConfigurationProperties = new TimeLimiterConfigurationProperties();
timeLimiterConfigurationProperties.getConfigs().put("default", defaultProperties);
timeLimiterConfigurationProperties.getConfigs().put("sharedConfig", sharedProperties);
timeLimiterConfigurationProperties.getInstances().put("backendWithDefaultConfig", backendWithDefaultConfig);
timeLimiterConfigurationProperties.getInstances().put("backendWithSharedConfig", backendWithSharedConfig);
TimeLimiterConfiguration timeLimiterConfiguration = new TimeLimiterConfiguration();
DefaultEventConsumerRegistry<TimeLimiterEvent> eventConsumerRegistry = new DefaultEventConsumerRegistry<>();
// When
TimeLimiterRegistry timeLimiterRegistry = timeLimiterConfiguration.timeLimiterRegistry(timeLimiterConfigurationProperties, eventConsumerRegistry, new CompositeRegistryEventConsumer<>(emptyList()), compositeTimeLimiterCustomizerTestInstance());
// Then
assertThat(timeLimiterRegistry.getAllTimeLimiters().size()).isEqualTo(2);
// Should get default config and overwrite timeout duration
TimeLimiter timeLimiter1 = timeLimiterRegistry.timeLimiter("backendWithDefaultConfig");
assertThat(timeLimiter1).isNotNull();
assertThat(timeLimiter1.getTimeLimiterConfig().getTimeoutDuration()).isEqualTo(Duration.ofSeconds(5));
assertThat(timeLimiter1.getTimeLimiterConfig().shouldCancelRunningFuture()).isEqualTo(true);
// Should get shared config and overwrite cancelRunningFuture
TimeLimiter timeLimiter2 = timeLimiterRegistry.timeLimiter("backendWithSharedConfig");
assertThat(timeLimiter2).isNotNull();
assertThat(timeLimiter2.getTimeLimiterConfig().getTimeoutDuration()).isEqualTo(Duration.ofSeconds(2));
assertThat(timeLimiter2.getTimeLimiterConfig().shouldCancelRunningFuture()).isEqualTo(true);
// Unknown backend should get default config of Registry
TimeLimiter timeLimiter3 = timeLimiterRegistry.timeLimiter("unknownBackend");
assertThat(timeLimiter3).isNotNull();
assertThat(timeLimiter3.getTimeLimiterConfig().getTimeoutDuration()).isEqualTo(Duration.ofSeconds(3));
assertThat(eventConsumerRegistry.getAllEventConsumer()).hasSize(3);
}
use of io.github.resilience4j.timelimiter.event.TimeLimiterEvent in project resilience4j by resilience4j.
the class TimeLimiterChain method execute.
@Override
public void execute(Chain chain) throws Exception {
String prefix = chain.getRegistry().get(Resilience4jConfig.class).getEndpoints().getTimelimiter().getPath();
chain.prefix(prefix, chain1 -> {
chain1.get("events", ctx -> Promise.<TimeLimiterEventsEndpointResponse>async(d -> {
List<TimeLimiterEventDTO> eventsList = eventConsumerRegistry.getAllEventConsumer().stream().flatMap(CircularEventConsumer::getBufferedEventsStream).sorted(Comparator.comparing(TimeLimiterEvent::getCreationTime)).map(TimeLimiterEventDTO::createTimeLimiterEventDTO).collect(Collectors.toList());
d.success(new TimeLimiterEventsEndpointResponse(eventsList));
}).then(r -> ctx.render(Jackson.json(r))));
chain1.get("stream/events", ctx -> {
Flux<TimeLimiterEvent> eventStreams = Flux.fromIterable(timeLimiterRegistry.getAllTimeLimiters()).flatMap(timeLimiter -> ReactorAdapter.toFlux(timeLimiter.getEventPublisher()));
Function<TimeLimiterEvent, String> data = r -> Jackson.getObjectWriter(chain1.getRegistry()).writeValueAsString(TimeLimiterEventDTO.createTimeLimiterEventDTO(r));
ServerSentEvents events = ServerSentEvents.serverSentEvents(eventStreams, e -> e.id(TimeLimiterEvent::getTimeLimiterName).event(c -> c.getEventType().name()).data(data));
ctx.render(events);
});
chain1.get("events/:name", ctx -> {
String timeLimiterName = ctx.getPathTokens().get("name");
Promise.<TimeLimiterEventsEndpointResponse>async(d -> {
List<TimeLimiterEventDTO> eventsList = eventConsumerRegistry.getEventConsumer(timeLimiterName).getBufferedEventsStream().sorted(Comparator.comparing(TimeLimiterEvent::getCreationTime)).map(TimeLimiterEventDTO::createTimeLimiterEventDTO).collect(Collectors.toList());
d.success(new TimeLimiterEventsEndpointResponse(eventsList));
}).then(r -> ctx.render(Jackson.json(r)));
});
chain1.get("stream/events/:name", ctx -> {
String timeLimiterName = ctx.getPathTokens().get("name");
TimeLimiter timeLimiter = timeLimiterRegistry.getAllTimeLimiters().stream().filter(tL -> tL.getName().equals(timeLimiterName)).findAny().orElseThrow(() -> new IllegalArgumentException(String.format("time limiter with name %s not found", timeLimiterName)));
Function<TimeLimiterEvent, String> data = r -> Jackson.getObjectWriter(chain1.getRegistry()).writeValueAsString(TimeLimiterEventDTO.createTimeLimiterEventDTO(r));
ServerSentEvents events = ServerSentEvents.serverSentEvents(ReactorAdapter.toFlux(timeLimiter.getEventPublisher()), e -> e.id(TimeLimiterEvent::getTimeLimiterName).event(c -> c.getEventType().name()).data(data));
ctx.render(events);
});
chain1.get("events/:name/:type", ctx -> {
String timeLimiterName = ctx.getPathTokens().get("name");
String eventType = ctx.getPathTokens().get("type");
Promise.<TimeLimiterEventsEndpointResponse>async(d -> {
List<TimeLimiterEventDTO> eventsList = eventConsumerRegistry.getEventConsumer(timeLimiterName).getBufferedEventsStream().sorted(Comparator.comparing(TimeLimiterEvent::getCreationTime)).filter(event -> event.getEventType() == TimeLimiterEvent.Type.valueOf(eventType.toUpperCase())).map(TimeLimiterEventDTO::createTimeLimiterEventDTO).collect(Collectors.toList());
d.success(new TimeLimiterEventsEndpointResponse(eventsList));
}).then(r -> ctx.render(Jackson.json(r)));
});
chain1.get("stream/events/:name/:type", ctx -> {
String timeLimiterName = ctx.getPathTokens().get("name");
String eventType = ctx.getPathTokens().get("type");
TimeLimiter timeLimiter = timeLimiterRegistry.getAllTimeLimiters().stream().filter(rL -> rL.getName().equals(timeLimiterName)).findAny().orElseThrow(() -> new IllegalArgumentException(String.format("time limiter with name %s not found", timeLimiterName)));
Flux<TimeLimiterEvent> eventStream = ReactorAdapter.toFlux(timeLimiter.getEventPublisher()).filter(event -> event.getEventType() == TimeLimiterEvent.Type.valueOf(eventType.toUpperCase()));
Function<TimeLimiterEvent, String> data = r -> Jackson.getObjectWriter(chain1.getRegistry()).writeValueAsString(TimeLimiterEventDTO.createTimeLimiterEventDTO(r));
ServerSentEvents events = ServerSentEvents.serverSentEvents(eventStream, e -> e.id(TimeLimiterEvent::getTimeLimiterName).event(c -> c.getEventType().name()).data(data));
ctx.render(events);
});
});
}
Aggregations