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);
}
}
};
}
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);
}
}
};
}
Aggregations