Search in sources :

Example 1 with Upstream

use of ratpack.exec.Upstream in project resilience4j by resilience4j.

the class BulkheadTransformer method apply.

@Override
public Upstream<T> apply(Upstream<? extends T> upstream) throws Exception {
    return down -> {
        if (bulkhead.isCallPermitted()) {
            // do not allow permits to leak
            upstream.connect(new Downstream<T>() {

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

                @Override
                public void error(Throwable throwable) {
                    bulkhead.onComplete();
                    try {
                        if (recover != null) {
                            down.success(recover.apply(throwable));
                        } else {
                            down.error(throwable);
                        }
                    } catch (Throwable t) {
                        down.error(t);
                    }
                }

                @Override
                public void complete() {
                    bulkhead.onComplete();
                    down.complete();
                }
            });
        } else {
            Throwable t = new BulkheadFullException(String.format("Bulkhead '%s' is full", bulkhead.getName()));
            if (recover != null) {
                try {
                    down.success(recover.apply(t));
                } catch (Throwable t2) {
                    down.error(t2);
                }
            } else {
                down.error(t);
            }
        }
    };
}
Also used : Downstream(ratpack.exec.Downstream) Function(ratpack.func.Function) BulkheadFullException(io.github.resilience4j.bulkhead.BulkheadFullException) Bulkhead(io.github.resilience4j.bulkhead.Bulkhead) Upstream(ratpack.exec.Upstream) BulkheadFullException(io.github.resilience4j.bulkhead.BulkheadFullException) Downstream(ratpack.exec.Downstream)

Example 2 with Upstream

use of ratpack.exec.Upstream in project resilience4j by resilience4j.

the class CircuitBreakerTransformer method apply.

@Override
public Upstream<T> apply(Upstream<? extends T> upstream) throws Exception {
    return down -> {
        long start;
        if (circuitBreaker.isCallPermitted()) {
            start = System.nanoTime();
            upstream.connect(new Downstream<T>() {

                @Override
                public void success(T value) {
                    long durationInNanos = System.nanoTime() - start;
                    circuitBreaker.onSuccess(durationInNanos);
                    down.success(value);
                }

                @Override
                public void error(Throwable throwable) {
                    long durationInNanos = System.nanoTime() - start;
                    circuitBreaker.onError(durationInNanos, throwable);
                    try {
                        if (recoverer != null) {
                            down.success(recoverer.apply(throwable));
                        } else {
                            down.error(throwable);
                        }
                    } catch (Throwable t) {
                        down.error(t);
                    }
                }

                @Override
                public void complete() {
                    down.complete();
                }
            });
        } else {
            Throwable t = new CircuitBreakerOpenException(String.format("CircuitBreaker '%s' is open", circuitBreaker.getName()));
            if (recoverer != null) {
                try {
                    down.success(recoverer.apply(t));
                } catch (Throwable t2) {
                    down.error(t2);
                }
            } else {
                down.error(t);
            }
        }
    };
}
Also used : Downstream(ratpack.exec.Downstream) Function(ratpack.func.Function) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) Upstream(ratpack.exec.Upstream) CircuitBreakerOpenException(io.github.resilience4j.circuitbreaker.CircuitBreakerOpenException) Downstream(ratpack.exec.Downstream) CircuitBreakerOpenException(io.github.resilience4j.circuitbreaker.CircuitBreakerOpenException)

Aggregations

Downstream (ratpack.exec.Downstream)2 Upstream (ratpack.exec.Upstream)2 Function (ratpack.func.Function)2 Bulkhead (io.github.resilience4j.bulkhead.Bulkhead)1 BulkheadFullException (io.github.resilience4j.bulkhead.BulkheadFullException)1 CircuitBreaker (io.github.resilience4j.circuitbreaker.CircuitBreaker)1 CircuitBreakerOpenException (io.github.resilience4j.circuitbreaker.CircuitBreakerOpenException)1