Search in sources :

Example 1 with RateLimiterEvent

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);
        });
    });
}
Also used : Function(ratpack.func.Function) RxJava2Adapter(io.github.resilience4j.adapter.RxJava2Adapter) RateLimiterEvent(io.github.resilience4j.ratelimiter.event.RateLimiterEvent) RateLimiter(io.github.resilience4j.ratelimiter.RateLimiter) Promise(ratpack.exec.Promise) Resilience4jConfig(io.github.resilience4j.ratpack.Resilience4jConfig) Jackson(ratpack.jackson.Jackson) ServerSentEvents(ratpack.sse.ServerSentEvents) Chain(ratpack.handling.Chain) Inject(javax.inject.Inject) List(java.util.List) Flowable(io.reactivex.Flowable) CircularEventConsumer(io.github.resilience4j.consumer.CircularEventConsumer) Action(ratpack.func.Action) Seq(io.vavr.collection.Seq) Comparator(java.util.Comparator) EventConsumerRegistry(io.github.resilience4j.consumer.EventConsumerRegistry) RateLimiterRegistry(io.github.resilience4j.ratelimiter.RateLimiterRegistry) Resilience4jConfig(io.github.resilience4j.ratpack.Resilience4jConfig) RateLimiterEvent(io.github.resilience4j.ratelimiter.event.RateLimiterEvent) RateLimiter(io.github.resilience4j.ratelimiter.RateLimiter) ServerSentEvents(ratpack.sse.ServerSentEvents) List(java.util.List) Flowable(io.reactivex.Flowable)

Example 2 with RateLimiterEvent

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);
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) RateLimiterEvent(io.github.resilience4j.ratelimiter.event.RateLimiterEvent) RateLimiterEventsEndpointResponse(io.github.resilience4j.ratelimiter.monitoring.model.RateLimiterEventsEndpointResponse) RateLimiter(io.github.resilience4j.ratelimiter.RateLimiter) MediaType(org.springframework.http.MediaType) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Controller(org.springframework.stereotype.Controller) RateLimiterEventDTO(io.github.resilience4j.ratelimiter.monitoring.model.RateLimiterEventDTO) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) ReactorAdapter.toFlux(io.github.resilience4j.adapter.ReactorAdapter.toFlux) Flux(reactor.core.publisher.Flux) List(java.util.List) CircularEventConsumer(io.github.resilience4j.consumer.CircularEventConsumer) Seq(io.vavr.collection.Seq) Comparator(java.util.Comparator) EventConsumerRegistry(io.github.resilience4j.consumer.EventConsumerRegistry) RateLimiterRegistry(io.github.resilience4j.ratelimiter.RateLimiterRegistry) SseEmitter(org.springframework.web.servlet.mvc.method.annotation.SseEmitter) RateLimiterEvent(io.github.resilience4j.ratelimiter.event.RateLimiterEvent) RateLimiter(io.github.resilience4j.ratelimiter.RateLimiter) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

CircularEventConsumer (io.github.resilience4j.consumer.CircularEventConsumer)2 EventConsumerRegistry (io.github.resilience4j.consumer.EventConsumerRegistry)2 RateLimiter (io.github.resilience4j.ratelimiter.RateLimiter)2 RateLimiterRegistry (io.github.resilience4j.ratelimiter.RateLimiterRegistry)2 RateLimiterEvent (io.github.resilience4j.ratelimiter.event.RateLimiterEvent)2 Seq (io.vavr.collection.Seq)2 Comparator (java.util.Comparator)2 List (java.util.List)2 ReactorAdapter.toFlux (io.github.resilience4j.adapter.ReactorAdapter.toFlux)1 RxJava2Adapter (io.github.resilience4j.adapter.RxJava2Adapter)1 RateLimiterEventDTO (io.github.resilience4j.ratelimiter.monitoring.model.RateLimiterEventDTO)1 RateLimiterEventsEndpointResponse (io.github.resilience4j.ratelimiter.monitoring.model.RateLimiterEventsEndpointResponse)1 Resilience4jConfig (io.github.resilience4j.ratpack.Resilience4jConfig)1 Flowable (io.reactivex.Flowable)1 Inject (javax.inject.Inject)1 MediaType (org.springframework.http.MediaType)1 Controller (org.springframework.stereotype.Controller)1 PathVariable (org.springframework.web.bind.annotation.PathVariable)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 RequestMethod (org.springframework.web.bind.annotation.RequestMethod)1