use of io.github.resilience4j.ratelimiter.RateLimiter 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.RateLimiter in project resilience4j by resilience4j.
the class SemaphoreBasedRateLimiterImplTest method changeDefaultTimeoutDuration.
@Test
public void changeDefaultTimeoutDuration() throws Exception {
ScheduledExecutorService scheduledExecutorService = mock(ScheduledExecutorService.class);
RateLimiter rateLimiter = new SemaphoreBasedRateLimiter("some", config, scheduledExecutorService);
RateLimiterConfig rateLimiterConfig = rateLimiter.getRateLimiterConfig();
then(rateLimiterConfig.getTimeoutDuration()).isEqualTo(TIMEOUT);
then(rateLimiterConfig.getTimeoutDurationInNanos()).isEqualTo(TIMEOUT.toNanos());
then(rateLimiterConfig.getLimitForPeriod()).isEqualTo(LIMIT);
then(rateLimiterConfig.getLimitRefreshPeriod()).isEqualTo(REFRESH_PERIOD);
then(rateLimiterConfig.getLimitRefreshPeriodInNanos()).isEqualTo(REFRESH_PERIOD.toNanos());
rateLimiter.changeTimeoutDuration(Duration.ofSeconds(1));
then(rateLimiterConfig != rateLimiter.getRateLimiterConfig()).isTrue();
rateLimiterConfig = rateLimiter.getRateLimiterConfig();
then(rateLimiterConfig.getTimeoutDuration()).isEqualTo(Duration.ofSeconds(1));
then(rateLimiterConfig.getTimeoutDurationInNanos()).isEqualTo(Duration.ofSeconds(1).toNanos());
then(rateLimiterConfig.getLimitForPeriod()).isEqualTo(LIMIT);
then(rateLimiterConfig.getLimitRefreshPeriod()).isEqualTo(REFRESH_PERIOD);
then(rateLimiterConfig.getLimitRefreshPeriodInNanos()).isEqualTo(REFRESH_PERIOD.toNanos());
}
use of io.github.resilience4j.ratelimiter.RateLimiter in project resilience4j by resilience4j.
the class RateLimiterExportsTest method testExportsRateLimiterMetrics.
@Test
public void testExportsRateLimiterMetrics() {
// Given
final CollectorRegistry registry = new CollectorRegistry();
final RateLimiter rateLimiter = RateLimiter.ofDefaults("foo");
RateLimiterExports.ofIterable("boo_rate_limiter", singletonList(rateLimiter)).register(registry);
final Supplier<Map<String, Double>> values = () -> HashSet.of("available_permissions", "waiting_threads").map(param -> Tuple.of(param, registry.getSampleValue("boo_rate_limiter", new String[] { "name", "param" }, new String[] { "foo", param }))).toMap(t -> t);
// When
final Map<String, Double> initialValues = values.get();
// Then
assertThat(initialValues).isEqualTo(HashMap.of("available_permissions", 50.0, "waiting_threads", 0.0));
}
use of io.github.resilience4j.ratelimiter.RateLimiter in project resilience4j by resilience4j.
the class RateLimiterMetrics method bindTo.
@Override
public void bindTo(MeterRegistry registry) {
for (RateLimiter rateLimiter : rateLimiters) {
final String name = rateLimiter.getName();
Gauge.builder(getName(prefix, name, AVAILABLE_PERMISSIONS), rateLimiter, (cb) -> cb.getMetrics().getAvailablePermissions()).register(registry);
Gauge.builder(getName(prefix, name, WAITING_THREADS), rateLimiter, (cb) -> cb.getMetrics().getNumberOfWaitingThreads()).register(registry);
}
}
use of io.github.resilience4j.ratelimiter.RateLimiter 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