/** * Constructor that allows a tolerance for a certain number of * failures within a given window of time without tripping. * @param rc A {@link RequestCounter} wrapped around the same thing that this * {@link org.fishwife.jrugged.CircuitBreaker} is protecting. This is * needed in order to keep track of the total number of requests, enabling a * percentage calculation to be done. * @param percent the whole number percentage of failures that will be tolerated * (i.e. the percentage of failures has to be strictly <em>greater * than</em> this number in order to trip the breaker). For * example, if the percentage is 3, any calculated failure percentage * above that number during the window will cause the breaker to trip. * @param windowMillis length of the window in milliseconds */ @SuppressWarnings("unchecked") public PercentErrPerTimeFailureInterpreter(RequestCounter rc, int percent, long windowMillis) { setIgnore(defaultIgnore); setPercent(percent); setWindowMillis(windowMillis); setRequestCounter(rc); }
public boolean shouldTrip(Throwable cause) { if (isExceptionIgnorable(cause)) return false; // if Exception is of specified type, and window conditions exist, // keep circuit open unless exception threshold has passed if (hasWindowConditions()) { Long currentRequestCount = -1L; long numberOfErrorsAfter; synchronized (modificationLock) { errorTimes.add(System.currentTimeMillis()); requestCounts.add(requestCounter.sample()[0]); // calculates time for which we remove any errors before final long removeTimeBeforeMillis = System.currentTimeMillis() - windowMillis; final int numberOfErrorsBefore = this.errorTimes.size(); removeErrorsPriorToCutoffTime(numberOfErrorsBefore, removeTimeBeforeMillis); numberOfErrorsAfter = this.errorTimes.size(); currentRequestCount = this.requestCounts.get(requestCounts.size() - 1); } long windowRequests = (currentRequestCount - previousRequestHighWaterMark); // Trip if the number of errors over the total of requests over the same period // is over the percentage limit. return windowRequests >= requestThreshold && (((double) numberOfErrorsAfter / (double) windowRequests) * 100d >= percent); } return true; }
/** * Constructor where we specify certain {@link Throwable} classes * that will be ignored by the breaker and not be treated as * failures (they will be passed through transparently without * causing the breaker to trip). * @param ignore an array of {@link Throwable} classes that will * be ignored. Any given <code>Throwable</code> that is a * subclass of one of these classes will be ignored. */ public PercentErrPerTimeFailureInterpreter(Class<? extends Throwable>[] ignore) { setIgnore(ignore); }
/** * Default constructor. Any {@link Throwable} will cause the breaker to trip. */ @SuppressWarnings("unchecked") public PercentErrPerTimeFailureInterpreter() { setIgnore(defaultIgnore); requestCounter = new RequestCounter(); }
/** * Constructor where we specify tolerance and a set of ignored failures. * * @param rc A {@link RequestCounter} wrapped around the same thing that this * {@link org.fishwife.jrugged.CircuitBreaker} is protecting. This is * needed in order to keep track of the total number of requests, enabling a * percentage calculation to be done. * @param ignore an array of {@link Throwable} classes that will * be ignored. Any given <code>Throwable</code> that is a * subclass of one of these classes will be ignored. * @param percent the whole number percentage of failures that will be tolerated * (i.e. the percentage of failures has to be strictly <em>greater * than</em> this number in order to trip the breaker). For * example, if the percentage is 3, any calculated failure percentage * above that number during the window will cause the breaker to trip. * @param windowMillis length of the window in milliseconds */ public PercentErrPerTimeFailureInterpreter(RequestCounter rc, Class<? extends Throwable>[] ignore, int percent, long windowMillis) { setRequestCounter(rc); setIgnore(ignore); setPercent(percent); setWindowMillis(windowMillis); }
/** * Constructor that allows a tolerance for a certain number of * failures within a given window of time without tripping. * @param p A {@link PerformanceMonitor} from which we can get an underlying * {@link RequestCounter} that is wrapped around the same thing that this * {@link org.fishwife.jrugged.CircuitBreaker} is protecting. This is * needed in order to keep track of the total number of requests, enabling a * percentage calculation to be done. * @param percent the whole number percentage of failures that will be tolerated * (i.e. the percentage of failures has to be strictly <em>greater * than</em> this number in order to trip the breaker). For * example, if the percentage is 3, any calculated failure percentage * above that number during the window will cause the breaker to trip. * @param windowMillis length of the window in milliseconds */ @SuppressWarnings("unchecked") public PercentErrPerTimeFailureInterpreter(PerformanceMonitor p, int percent, long windowMillis) { setIgnore(defaultIgnore); setPercent(percent); setWindowMillis(windowMillis); setRequestCounter(p.getRequestCounter()); }