/** * Constructor that allows a tolerance for a certain number of * failures within a given window of time without tripping. * @param limit the number of failures that will be tolerated * (i.e. the number of failures has to be strictly <em>greater * than</em> this number in order to trip the breaker). For * example, if the limit is 3, the fourth failure during * the window will cause the breaker to trip. * @param windowMillis length of the window in milliseconds */ public DefaultFailureInterpreter(int limit, long windowMillis) { setIgnore(defaultIgnore); setLimit(limit); setWindowMillis(windowMillis); initCounter(); }
private void configureDefaultFailureInterpreter(String name, long resetMillis, CircuitBreaker circuit) { DefaultFailureInterpreter fi = (DefaultFailureInterpreter) circuit.getFailureInterpreter(); Integer limitOverride = getIntegerPropertyOverrideValue(name, LIMIT_KEY); if (limitOverride != null) { fi.setLimit(limitOverride); } Long windowMillisOverride = getLongPropertyOverrideValue(name, WINDOWMILLIS_KEY); if (windowMillisOverride != null) { fi.setWindowMillis(windowMillisOverride); } logger.info( "Created CircuitBreaker '{}', limit={}, windowMillis={}, resetMillis={}", new Object[] { name, fi.getLimit(), fi.getWindowMillis(), resetMillis }); }
/** * Specifies the failure tolerance limit for the {@link * DefaultFailureInterpreter} that comes with a {@link * CircuitBreaker} by default. * @see DefaultFailureInterpreter * @param limit the number of tolerated failures in a window */ public void setLimit(int limit) { FailureInterpreter fi = getFailureInterpreter(); if (!(fi instanceof DefaultFailureInterpreter)) { throw new IllegalStateException("setLimit() not supported: this CircuitBreaker's FailureInterpreter isn't a DefaultFailureInterpreter."); } ((DefaultFailureInterpreter)fi).setLimit(limit); }
new DefaultFailureInterpreter( circuitBreakerAnnotation.ignore(), circuitBreakerAnnotation.limit(),
/** * Gets the failure tolerance limit for the {@link DefaultFailureInterpreter} that * comes with a {@link CircuitBreaker} by default. * * @see DefaultFailureInterpreter * * @return the number of tolerated failures in a window */ @ManagedAttribute public int getLimit() { return ((DefaultFailureInterpreter) super.getFailureInterpreter()).getLimit(); }
/** * Gets the tolerance window in milliseconds for the {@link DefaultFailureInterpreter} * that comes with a {@link CircuitBreaker} by default. * * @see DefaultFailureInterpreter * * @return length of the window in milliseconds */ @ManagedAttribute public long getWindowMillis() { return ((DefaultFailureInterpreter) super.getFailureInterpreter()).getWindowMillis(); }
/** * 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 DefaultFailureInterpreter(Class<? extends Throwable>[] ignore) { setIgnore(ignore); }
/** * Specifies the number of tolerated failures within the * configured time window. If limit is set to <em>n</em> then the * <em>(n+1)</em>th failure will trip the breaker. Mutating the * limit at runtime can reset previous failure counts. * @param limit <code>int</code> */ public void setLimit(int limit) { this.limit=limit; initCounter(); }
public boolean shouldTrip(Throwable cause) { for(Class<?> clazz : ignore) { if (clazz.isInstance(cause)) { return false; } } // if Exception is of specified type, and window conditions exist, // keep circuit open unless exception threshold has passed if (hasWindowConditions()) { counter.mark(); // Trip if the exception count has passed the limit return (counter.tally() > limit); } return true; }
/** * Specifies the tolerance window in milliseconds for the {@link * DefaultFailureInterpreter} that comes with a {@link * CircuitBreaker} by default. * @see DefaultFailureInterpreter * @param windowMillis length of the window in milliseconds */ public void setWindowMillis(long windowMillis) { FailureInterpreter fi = getFailureInterpreter(); if (!(fi instanceof DefaultFailureInterpreter)) { throw new IllegalStateException("setWindowMillis() not supported: this CircuitBreaker's FailureInterpreter isn't a DefaultFailureInterpreter."); } ((DefaultFailureInterpreter)fi).setWindowMillis(windowMillis); }
/** * Specifies the failure tolerance limit for the {@link * DefaultFailureInterpreter} that comes with a {@link * CircuitBreaker} by default. * * @see DefaultFailureInterpreter * * @param limit the number of tolerated failures in a window */ @ManagedAttribute @Override public void setLimit(int limit) { ((DefaultFailureInterpreter) super.getFailureInterpreter()).setLimit(limit); }
/** * If packageScanBase is defined will search packages for {@link org.fishwife.jrugged.aspects.CircuitBreaker} * annotations and create circuitbreakers for them. */ public void buildAnnotatedCircuitBreakers() { if (packageScanBase != null) { AnnotatedMethodScanner methodScanner = new AnnotatedMethodScanner(); for (Method m : methodScanner.findAnnotatedMethods(packageScanBase, org.fishwife.jrugged.aspects.CircuitBreaker.class)) { org.fishwife.jrugged.aspects.CircuitBreaker circuitBreakerAnnotation = m.getAnnotation(org.fishwife.jrugged.aspects.CircuitBreaker.class); DefaultFailureInterpreter dfi = new DefaultFailureInterpreter(circuitBreakerAnnotation.ignore(), circuitBreakerAnnotation.limit(), circuitBreakerAnnotation.windowMillis()); CircuitBreakerConfig config = new CircuitBreakerConfig(circuitBreakerAnnotation.resetMillis(), dfi); createCircuitBreaker(circuitBreakerAnnotation.name(), config); } } }
/** * Default constructor. Any {@link Throwable} will cause the breaker to trip. */ public DefaultFailureInterpreter() { setIgnore(defaultIgnore); }
/** * Specifies the length of the tolerance window in milliseconds. * @param windowMillis <code>long</code> */ public void setWindowMillis(long windowMillis) { this.windowMillis=windowMillis; initCounter(); } }
private void initCounter() { if (hasWindowConditions()) { int capacity = limit + 1; if (counter == null) { this.counter = new WindowedEventCounter(capacity,windowMillis); } else { if (capacity != counter.getCapacity()) { counter.setCapacity(capacity); } if (windowMillis != counter.getWindowMillis()) { counter.setWindowMillis(windowMillis); } } } else { // we're not under windowConditions, no counter needed counter = null; } }
/** * Constructor where we specify tolerance and a set of ignored failures. * * @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 limit the number of failures that will be tolerated * (i.e. the number of failures has to be strictly <em>greater * than</em> this number in order to trip the breaker). For * example, if the limit is 3, the fourth failure during * the window will cause the breaker to trip. * @param windowMillis length of the window in milliseconds */ public DefaultFailureInterpreter(Class<? extends Throwable>[] ignore, int limit, long windowMillis) { setIgnore(ignore); setLimit(limit); setWindowMillis(windowMillis); initCounter(); }
/** * Specifies a set of {@link Throwable} classes that should not * be considered failures by the {@link CircuitBreaker}. * @see DefaultFailureInterpreter * @param ignore a {@link java.util.Collection} of {@link Throwable} * classes */ public void setIgnore(Collection<Class<? extends Throwable>> ignore) { FailureInterpreter fi = getFailureInterpreter(); if (!(fi instanceof DefaultFailureInterpreter)) { throw new IllegalStateException("setIgnore() not supported: this CircuitBreaker's FailureInterpreter isn't a DefaultFailureInterpreter."); } @SuppressWarnings("unchecked") Class<? extends Throwable>[] classes = new Class[ignore.size()]; int i = 0; for(Class<? extends Throwable> c : ignore) { classes[i] = c; i++; } ((DefaultFailureInterpreter)fi).setIgnore(classes); }