io.github.resilience4j.ratelimiter.RequestNotPermitted Java Examples

The following examples show how to use io.github.resilience4j.ratelimiter.RequestNotPermitted. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CompletableRateLimiter.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Override
protected void subscribeActual(CompletableObserver downstream) {
    long waitDuration = rateLimiter.reservePermission();
    if (waitDuration >= 0) {
        if (waitDuration > 0) {
            Completable.timer(waitDuration, TimeUnit.NANOSECONDS)
                .subscribe(
                    () -> upstream.subscribe(new RateLimiterCompletableObserver(downstream)));
        } else {
            upstream.subscribe(new RateLimiterCompletableObserver(downstream));
        }
    } else {
        downstream.onSubscribe(EmptyDisposable.INSTANCE);
        downstream.onError(RequestNotPermitted.createRequestNotPermitted(rateLimiter));
    }
}
 
Example #2
Source File: DecoratorsTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testDecoratorBuilderWithRateLimiter() {
    given(helloWorldService.returnHelloWorld()).willReturn("Hello world");
    RateLimiterConfig config = RateLimiterConfig.custom()
        .timeoutDuration(Duration.ofMillis(100))
        .limitRefreshPeriod(Duration.ofSeconds(1))
        .limitForPeriod(1)
        .build();
    RateLimiter rateLimiter = RateLimiter.of("backendName", config);
    CheckedFunction0<String> restrictedSupplier = Decorators
        .ofCheckedSupplier(() -> helloWorldService.returnHelloWorld())
        .withRateLimiter(rateLimiter)
        .decorate();
    alignTime(rateLimiter);

    Try<String> firstTry = Try.of(restrictedSupplier);
    Try<String> secondTry = Try.of(restrictedSupplier);

    assertThat(firstTry.isSuccess()).isTrue();
    assertThat(secondTry.isFailure()).isTrue();
    assertThat(secondTry.getCause()).isInstanceOf(RequestNotPermitted.class);
    then(helloWorldService).should(times(1)).returnHelloWorld();
}
 
Example #3
Source File: CompletableRateLimiter.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Override
protected void subscribeActual(CompletableObserver downstream) {
    long waitDuration = rateLimiter.reservePermission();
    if (waitDuration >= 0) {
        if (waitDuration > 0) {
            Completable.timer(waitDuration, TimeUnit.NANOSECONDS)
                .subscribe(
                    () -> upstream.subscribe(new RateLimiterCompletableObserver(downstream)));
        } else {
            upstream.subscribe(new RateLimiterCompletableObserver(downstream));
        }
    } else {
        downstream.onSubscribe(EmptyDisposable.INSTANCE);
        downstream.onError(RequestNotPermitted.createRequestNotPermitted(rateLimiter));
    }
}
 
Example #4
Source File: SingleRateLimiter.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Override
protected void subscribeActual(SingleObserver<? super T> downstream) {
    long waitDuration = rateLimiter.reservePermission();
    if (waitDuration >= 0) {
        if (waitDuration > 0) {
            Completable.timer(waitDuration, TimeUnit.NANOSECONDS)
                .subscribe(() -> upstream.subscribe(new RateLimiterSingleObserver(downstream)));
        } else {
            upstream.subscribe(new RateLimiterSingleObserver(downstream));
        }
    } else {
        downstream.onSubscribe(EmptyDisposable.INSTANCE);
        downstream.onError(RequestNotPermitted.createRequestNotPermitted(rateLimiter));
    }
}
 
Example #5
Source File: RetrofitRateLimiter.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
private Response<T> handleFailure(Try<Response<T>> response) throws IOException {
    try {
        throw response.getCause();
    } catch (RequestNotPermitted | IllegalStateException e) {
        return tooManyRequestsError();
    } catch (IOException ioe) {
        throw ioe;
    } catch (Throwable t) {
        throw new RuntimeException("Exception executing call", t);
    }
}
 
Example #6
Source File: RetrofitRateLimiter.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Override
public void enqueue(final Callback<T> callback) {
    try {
        RateLimiter.waitForPermission(rateLimiter);
    } catch (RequestNotPermitted | IllegalStateException e) {
        callback.onResponse(call, tooManyRequestsError());
        return;
    }

    call.enqueue(callback);
}
 
Example #7
Source File: FluxRateLimiterTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEmitRequestNotPermittedExceptionEvenWhenErrorDuringSubscribe() {
    given(rateLimiter.reservePermission()).willReturn(-1L);

    StepVerifier.create(
        Flux.error(new IOException("BAM!"))
            .transformDeferred(RateLimiterOperator.of(rateLimiter)))
        .expectSubscription()
        .expectError(RequestNotPermitted.class)
        .verify(Duration.ofMillis(100));
}
 
Example #8
Source File: FluxRateLimiterTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEmitRequestNotPermittedException() {
    given(rateLimiter.reservePermission()).willReturn(-1L);

    StepVerifier.create(
        Flux.just("Event")
            .transformDeferred(RateLimiterOperator.of(rateLimiter)))
        .expectSubscription()
        .expectError(RequestNotPermitted.class)
        .verify(Duration.ofMillis(100));
}
 
Example #9
Source File: MonoRateLimiterTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEmitRequestNotPermittedExceptionEvenWhenErrorDuringSubscribe() {
    given(rateLimiter.reservePermission()).willReturn(-1L);

    StepVerifier.create(
        Mono.error(new IOException("BAM!"))
            .transformDeferred(RateLimiterOperator.of(rateLimiter)))
        .expectError(RequestNotPermitted.class)
        .verify(Duration.ofSeconds(1));
}
 
Example #10
Source File: MonoRateLimiterTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEmitErrorWithBulkheadFullException() {
    given(rateLimiter.reservePermission()).willReturn(-1L);

    StepVerifier.create(
        Mono.just("Event")
            .transformDeferred(RateLimiterOperator.of(rateLimiter)))
        .expectSubscription()
        .expectError(RequestNotPermitted.class)
        .verify(Duration.ofSeconds(1));
}
 
Example #11
Source File: ObserverRateLimiterTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEmitErrorWithRequestNotPermittedException() {
    given(rateLimiter.reservePermission()).willReturn(-1L);

    Observable.just(1)
        .compose(RateLimiterOperator.of(rateLimiter))
        .test()
        .assertSubscribed()
        .assertError(RequestNotPermitted.class)
        .assertNotComplete();
}
 
Example #12
Source File: FlowableRateLimiterTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEmitErrorWithRequestNotPermittedException() {
    given(rateLimiter.reservePermission()).willReturn(-1L);

    Flowable.just(1)
        .compose(RateLimiterOperator.of(rateLimiter))
        .test()
        .assertSubscribed()
        .assertError(RequestNotPermitted.class)
        .assertNotComplete();
}
 
Example #13
Source File: SingleRateLimiterTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEmitErrorWithRequestNotPermittedException() {
    given(rateLimiter.reservePermission()).willReturn(-1L);

    Single.just(1)
        .compose(RateLimiterOperator.of(rateLimiter))
        .test()
        .assertSubscribed()
        .assertError(RequestNotPermitted.class)
        .assertNotComplete();
}
 
Example #14
Source File: MaybeRateLimiterTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEmitErrorWithRequestNotPermittedException() {
    given(rateLimiter.reservePermission()).willReturn(-1L);

    Maybe.just(1)
        .compose(RateLimiterOperator.of(rateLimiter))
        .test()
        .assertSubscribed()
        .assertError(RequestNotPermitted.class)
        .assertNotComplete();
}
 
Example #15
Source File: CompletableRateLimiterTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEmitErrorWithRequestNotPermittedException() {
    given(rateLimiter.reservePermission()).willReturn(-1L);

    Completable.complete()
        .compose(RateLimiterOperator.of(rateLimiter))
        .test()
        .assertSubscribed()
        .assertError(RequestNotPermitted.class)
        .assertNotComplete();
}
 
Example #16
Source File: MaybeRateLimiter.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Override
protected void subscribeActual(MaybeObserver<? super T> downstream) {
    long waitDuration = rateLimiter.reservePermission();
    if (waitDuration >= 0) {
        if (waitDuration > 0) {
            Completable.timer(waitDuration, TimeUnit.NANOSECONDS)
                .subscribe(() -> upstream.subscribe(new RateLimiterMaybeObserver(downstream)));
        } else {
            upstream.subscribe(new RateLimiterMaybeObserver(downstream));
        }
    } else {
        downstream.onSubscribe(EmptyDisposable.INSTANCE);
        downstream.onError(RequestNotPermitted.createRequestNotPermitted(rateLimiter));
    }
}
 
Example #17
Source File: RateLimiterManager.java    From spring-cloud-formula with Apache License 2.0 5 votes vote down vote up
public void waitForPermit(RateLimiter rateLimiter) {
    if (rateLimiter != null) {
        try {
            // wait for permission, fast fail
            RateLimiter.waitForPermission(rateLimiter);
        } catch (IllegalStateException e) {
            throw e;
        } catch (RequestNotPermitted requestNotPermitted) {
            // convert exception
            throw new BlockException("The request has been block, please try later");
        }
    }
}
 
Example #18
Source File: RateLimiterTransformer.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Override
public Upstream<T> apply(Upstream<? extends T> upstream) throws Exception {
    return down -> {
        boolean permission = rateLimiter.acquirePermission();
        if (Thread.interrupted()) {
            throw new IllegalStateException("Thread was interrupted during permission wait");
        }
        if (!permission) {
            Throwable t = RequestNotPermitted.createRequestNotPermitted(rateLimiter);
            if (recoverer != null) {
                down.success(recoverer.apply(t));
            } else {
                down.error(t);
            }
        } else {
            upstream.connect(new Downstream<T>() {

                @Override
                public void success(T value) {
                    down.success(value);
                }

                @Override
                public void error(Throwable throwable) {
                    down.error(throwable);
                }

                @Override
                public void complete() {
                    down.complete();
                }
            });
        }
    };
}
 
Example #19
Source File: ObserverRateLimiterTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEmitErrorWithRequestNotPermittedException() {
    given(rateLimiter.reservePermission()).willReturn(-1L);

    Observable.just(1)
        .compose(RateLimiterOperator.of(rateLimiter))
        .test()
        .assertError(RequestNotPermitted.class)
        .assertNotComplete();
}
 
Example #20
Source File: FlowableRateLimiterTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEmitErrorWithRequestNotPermittedException() {
    given(rateLimiter.reservePermission()).willReturn(-1L);

    Flowable.just(1)
        .compose(RateLimiterOperator.of(rateLimiter))
        .test()
        .assertError(RequestNotPermitted.class)
        .assertNotComplete();
}
 
Example #21
Source File: SingleRateLimiterTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEmitErrorWithRequestNotPermittedException() {
    given(rateLimiter.reservePermission()).willReturn(-1L);

    Single.just(1)
        .compose(RateLimiterOperator.of(rateLimiter))
        .test()
        .assertError(RequestNotPermitted.class)
        .assertNotComplete();
}
 
Example #22
Source File: MaybeRateLimiterTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEmitErrorWithRequestNotPermittedException() {
    given(rateLimiter.reservePermission()).willReturn(-1L);

    Maybe.just(1)
        .compose(RateLimiterOperator.of(rateLimiter))
        .test()
        .assertError(RequestNotPermitted.class)
        .assertNotComplete();
}
 
Example #23
Source File: CompletableRateLimiterTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEmitErrorWithRequestNotPermittedException() {
    given(rateLimiter.reservePermission()).willReturn(-1L);

    Completable.complete()
        .compose(RateLimiterOperator.of(rateLimiter))
        .test()
        .assertError(RequestNotPermitted.class)
        .assertNotComplete();
}
 
Example #24
Source File: MaybeRateLimiter.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Override
protected void subscribeActual(MaybeObserver<? super T> downstream) {
    long waitDuration = rateLimiter.reservePermission();
    if (waitDuration >= 0) {
        if (waitDuration > 0) {
            Completable.timer(waitDuration, TimeUnit.NANOSECONDS)
                .subscribe(() -> upstream.subscribe(new RateLimiterMaybeObserver(downstream)));
        } else {
            upstream.subscribe(new RateLimiterMaybeObserver(downstream));
        }
    } else {
        downstream.onSubscribe(EmptyDisposable.INSTANCE);
        downstream.onError(RequestNotPermitted.createRequestNotPermitted(rateLimiter));
    }
}
 
Example #25
Source File: SingleRateLimiter.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Override
protected void subscribeActual(SingleObserver<? super T> downstream) {
    long waitDuration = rateLimiter.reservePermission();
    if (waitDuration >= 0) {
        if (waitDuration > 0) {
            Completable.timer(waitDuration, TimeUnit.NANOSECONDS)
                .subscribe(() -> upstream.subscribe(new RateLimiterSingleObserver(downstream)));
        } else {
            upstream.subscribe(new RateLimiterSingleObserver(downstream));
        }
    } else {
        downstream.onSubscribe(EmptyDisposable.INSTANCE);
        downstream.onError(RequestNotPermitted.createRequestNotPermitted(rateLimiter));
    }
}
 
Example #26
Source File: Resilience4jFeignRateLimiterTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test(expected = RequestNotPermitted.class)
public void testRateLimiterCreateByStaticMethod() {
    testService = TestService.create(MOCK_URL, rateLimiter);
    givenResponse(200);
    when(rateLimiter.acquirePermission(1)).thenReturn(false);
    when(rateLimiter.getRateLimiterConfig()).thenReturn(RateLimiterConfig.ofDefaults());

    testService.greeting();

    verify(0, getRequestedFor(urlPathEqualTo("/greeting")));
}
 
Example #27
Source File: Resilience4jFeignRateLimiterTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test(expected = RequestNotPermitted.class)
public void testRateLimiterLimiting() {
    givenResponse(200);
    when(rateLimiter.acquirePermission(1)).thenReturn(false);
    when(rateLimiter.getRateLimiterConfig()).thenReturn(RateLimiterConfig.ofDefaults());

    testService.greeting();

    verify(0, getRequestedFor(urlPathEqualTo("/greeting")));
}
 
Example #28
Source File: Resilience4jRateLimiterFilter.java    From dubbo-samples with Apache License 2.0 5 votes vote down vote up
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    try {
        System.out.println("**************** Enter RateLimiter ****************");
        RateLimiter.waitForPermission(rateLimiter);
        return invoker.invoke(invocation);
    } catch (RequestNotPermitted rnp) {
        System.err.println("---------------- Rate Limiter! Try it later! ----------------");
        throw rnp;
    } catch (Throwable throwable) {
        System.err.println("........" + throwable.getMessage());
        throw throwable;
    }
}