use of io.github.resilience4j.ratelimiter.event.RateLimiterEvent in project resilience4j by resilience4j.
the class RateLimiterChain method execute.
@Override
public void execute(Chain chain) throws Exception {
String prefix = chain.getRegistry().get(Resilience4jConfig.class).getEndpoints().getRateLimiters().getPath();
chain.prefix(prefix, chain1 -> {
chain1.get("events", ctx -> Promise.<RateLimiterEventsEndpointResponse>async(d -> {
List<RateLimiterEventDTO> eventsList = eventConsumerRegistry.getAllEventConsumer().flatMap(CircularEventConsumer::getBufferedEvents).sorted(Comparator.comparing(RateLimiterEvent::getCreationTime)).map(RateLimiterEventDTO::createRateLimiterEventDTO).toJavaList();
d.success(new RateLimiterEventsEndpointResponse(eventsList));
}).then(r -> ctx.render(Jackson.json(r))));
chain1.get("stream/events", ctx -> {
Seq<Flowable<RateLimiterEvent>> eventStreams = rateLimiterRegistry.getAllRateLimiters().map(rateLimiter -> RxJava2Adapter.toFlowable(rateLimiter.getEventPublisher()));
Function<RateLimiterEvent, String> data = r -> Jackson.getObjectWriter(chain1.getRegistry()).writeValueAsString(RateLimiterEventDTO.createRateLimiterEventDTO(r));
ServerSentEvents events = ServerSentEvents.serverSentEvents(Flowable.merge(eventStreams), e -> e.id(RateLimiterEvent::getRateLimiterName).event(c -> c.getEventType().name()).data(data));
ctx.render(events);
});
chain1.get("events/:name", ctx -> {
String rateLimiterName = ctx.getPathTokens().get("name");
Promise.<RateLimiterEventsEndpointResponse>async(d -> {
List<RateLimiterEventDTO> eventsList = eventConsumerRegistry.getEventConsumer(rateLimiterName).getBufferedEvents().sorted(Comparator.comparing(RateLimiterEvent::getCreationTime)).map(RateLimiterEventDTO::createRateLimiterEventDTO).toJavaList();
d.success(new RateLimiterEventsEndpointResponse(eventsList));
}).then(r -> ctx.render(Jackson.json(r)));
});
chain1.get("stream/events/:name", ctx -> {
String rateLimiterName = ctx.getPathTokens().get("name");
RateLimiter rateLimiter = rateLimiterRegistry.getAllRateLimiters().find(rL -> rL.getName().equals(rateLimiterName)).getOrElseThrow(() -> new IllegalArgumentException(String.format("rate limiter with name %s not found", rateLimiterName)));
Function<RateLimiterEvent, String> data = r -> Jackson.getObjectWriter(chain1.getRegistry()).writeValueAsString(RateLimiterEventDTO.createRateLimiterEventDTO(r));
ServerSentEvents events = ServerSentEvents.serverSentEvents(RxJava2Adapter.toFlowable(rateLimiter.getEventPublisher()), e -> e.id(RateLimiterEvent::getRateLimiterName).event(c -> c.getEventType().name()).data(data));
ctx.render(events);
});
chain1.get("events/:name/:type", ctx -> {
String rateLimiterName = ctx.getPathTokens().get("name");
String eventType = ctx.getPathTokens().get("type");
Promise.<RateLimiterEventsEndpointResponse>async(d -> {
List<RateLimiterEventDTO> eventsList = eventConsumerRegistry.getEventConsumer(rateLimiterName).getBufferedEvents().sorted(Comparator.comparing(RateLimiterEvent::getCreationTime)).filter(event -> event.getEventType() == RateLimiterEvent.Type.valueOf(eventType.toUpperCase())).map(RateLimiterEventDTO::createRateLimiterEventDTO).toJavaList();
d.success(new RateLimiterEventsEndpointResponse(eventsList));
}).then(r -> ctx.render(Jackson.json(r)));
});
chain1.get("stream/events/:name/:type", ctx -> {
String rateLimiterName = ctx.getPathTokens().get("name");
String eventType = ctx.getPathTokens().get("type");
RateLimiter rateLimiter = rateLimiterRegistry.getAllRateLimiters().find(rL -> rL.getName().equals(rateLimiterName)).getOrElseThrow(() -> new IllegalArgumentException(String.format("rate limiter with name %s not found", rateLimiterName)));
Flowable<RateLimiterEvent> eventStream = RxJava2Adapter.toFlowable(rateLimiter.getEventPublisher()).filter(event -> event.getEventType() == RateLimiterEvent.Type.valueOf(eventType.toUpperCase()));
Function<RateLimiterEvent, String> data = r -> Jackson.getObjectWriter(chain1.getRegistry()).writeValueAsString(RateLimiterEventDTO.createRateLimiterEventDTO(r));
ServerSentEvents events = ServerSentEvents.serverSentEvents(eventStream, e -> e.id(RateLimiterEvent::getRateLimiterName).event(c -> c.getEventType().name()).data(data));
ctx.render(events);
});
});
}
use of io.github.resilience4j.ratelimiter.event.RateLimiterEvent in project resilience4j by resilience4j.
the class RateLimiterEventsEndpoint method getEventsStreamFilteredByRateLimiterNameAndEventType.
@RequestMapping(value = "stream/events/{rateLimiterName}/{eventType}", produces = MEDIA_TYPE_TEXT_EVENT_STREAM)
public SseEmitter getEventsStreamFilteredByRateLimiterNameAndEventType(@PathVariable("rateLimiterName") String rateLimiterName, @PathVariable("eventType") String eventType) {
RateLimiterEvent.Type targetType = RateLimiterEvent.Type.valueOf(eventType.toUpperCase());
RateLimiter rateLimiter = rateLimiterRegistry.getAllRateLimiters().find(rL -> rL.getName().equals(rateLimiterName)).getOrElseThrow(() -> new IllegalArgumentException(String.format("rate limiter with name %s not found", rateLimiterName)));
Flux<RateLimiterEvent> eventStream = toFlux(rateLimiter.getEventPublisher()).filter(event -> event.getEventType() == targetType);
return RateLimiterEventsEmitter.createSseEmitter(eventStream);
}
Aggregations