/** * Creates a new instance of {@code EventCountCircuitBreaker} and initializes all properties for * opening and closing it based on threshold values for events occurring in specific * intervals. * * @param openingThreshold the threshold for opening the circuit breaker; if this * number of events is received in the time span determined by the opening interval, * the circuit breaker is opened * @param openingInterval the interval for opening the circuit breaker * @param openingUnit the {@code TimeUnit} defining the opening interval * @param closingThreshold the threshold for closing the circuit breaker; if the * number of events received in the time span determined by the closing interval goes * below this threshold, the circuit breaker is closed again * @param closingInterval the interval for closing the circuit breaker * @param closingUnit the {@code TimeUnit} defining the closing interval */ public EventCountCircuitBreaker(final int openingThreshold, final long openingInterval, final TimeUnit openingUnit, final int closingThreshold, final long closingInterval, final TimeUnit closingUnit) { super(); checkIntervalData = new AtomicReference<>(new CheckIntervalData(0, 0)); this.openingThreshold = openingThreshold; this.openingInterval = openingUnit.toNanos(openingInterval); this.closingThreshold = closingThreshold; this.closingInterval = closingUnit.toNanos(closingInterval); }
/** * Changes the state of this circuit breaker and also initializes a new * {@code CheckIntervalData} object. * * @param newState the new state to be set */ private void changeStateAndStartNewCheckInterval(final State newState) { changeState(newState); checkIntervalData.set(new CheckIntervalData(0, now())); }
/** * {@inheritDoc} A new check interval is started. If too many events are received in * this interval, the circuit breaker changes again to state open. If this circuit * breaker is already closed, this method has no effect, except that a new check * interval is started. */ @Override public void close() { super.close(); checkIntervalData.set(new CheckIntervalData(0, now())); }
/** * Creates a new instance of {@code EventCountCircuitBreaker} and initializes all properties for * opening and closing it based on threshold values for events occurring in specific * intervals. * * @param openingThreshold the threshold for opening the circuit breaker; if this * number of events is received in the time span determined by the opening interval, * the circuit breaker is opened * @param openingInterval the interval for opening the circuit breaker * @param openingUnit the {@code TimeUnit} defining the opening interval * @param closingThreshold the threshold for closing the circuit breaker; if the * number of events received in the time span determined by the closing interval goes * below this threshold, the circuit breaker is closed again * @param closingInterval the interval for closing the circuit breaker * @param closingUnit the {@code TimeUnit} defining the closing interval */ public EventCountCircuitBreaker(final int openingThreshold, final long openingInterval, final TimeUnit openingUnit, final int closingThreshold, final long closingInterval, final TimeUnit closingUnit) { super(); checkIntervalData = new AtomicReference<>(new CheckIntervalData(0, 0)); this.openingThreshold = openingThreshold; this.openingInterval = openingUnit.toNanos(openingInterval); this.closingThreshold = closingThreshold; this.closingInterval = closingUnit.toNanos(closingInterval); }
/** * Calculates the next {@code CheckIntervalData} object based on the current data and * the current state. The next data object takes the counter increment and the current * time into account. * * @param increment the increment for the internal counter * @param currentData the current check data object * @param currentState the current state of the circuit breaker * @param time the current time * @return the updated {@code CheckIntervalData} object */ private CheckIntervalData nextCheckIntervalData(final int increment, final CheckIntervalData currentData, final State currentState, final long time) { CheckIntervalData nextData; if (stateStrategy(currentState).isCheckIntervalFinished(this, currentData, time)) { nextData = new CheckIntervalData(increment, time); } else { nextData = currentData.increment(increment); } return nextData; }
/** * Returns a new instance of {@code CheckIntervalData} with the event counter * incremented by the given delta. If the delta is 0, this object is returned. * * @param delta the delta * @return the updated instance */ public CheckIntervalData increment(final int delta) { return (delta == 0) ? this : new CheckIntervalData(getEventCount() + delta, getCheckIntervalStart()); } }
/** * Returns a new instance of {@code CheckIntervalData} with the event counter * incremented by the given delta. If the delta is 0, this object is returned. * * @param delta the delta * @return the updated instance */ public CheckIntervalData increment(final int delta) { return (delta == 0) ? this : new CheckIntervalData(getEventCount() + delta, getCheckIntervalStart()); } }
/** * {@inheritDoc} This circuit breaker may close itself again if the number of events * received during a check interval goes below the closing threshold. If this circuit * breaker is already open, this method has no effect, except that a new check * interval is started. */ @Override public void open() { super.open(); checkIntervalData.set(new CheckIntervalData(0, now())); }
/** * Changes the state of this circuit breaker and also initializes a new * {@code CheckIntervalData} object. * * @param newState the new state to be set */ private void changeStateAndStartNewCheckInterval(final State newState) { changeState(newState); checkIntervalData.set(new CheckIntervalData(0, now())); }
/** * {@inheritDoc} A new check interval is started. If too many events are received in * this interval, the circuit breaker changes again to state open. If this circuit * breaker is already closed, this method has no effect, except that a new check * interval is started. */ @Override public void close() { super.close(); checkIntervalData.set(new CheckIntervalData(0, now())); }
/** * {@inheritDoc} A new check interval is started. If too many events are received in * this interval, the circuit breaker changes again to state open. If this circuit * breaker is already closed, this method has no effect, except that a new check * interval is started. */ @Override public void close() { super.close(); checkIntervalData.set(new CheckIntervalData(0, now())); }
/** * {@inheritDoc} This circuit breaker may close itself again if the number of events * received during a check interval goes below the closing threshold. If this circuit * breaker is already open, this method has no effect, except that a new check * interval is started. */ @Override public void open() { super.open(); checkIntervalData.set(new CheckIntervalData(0, now())); }
/** * Changes the state of this circuit breaker and also initializes a new * {@code CheckIntervalData} object. * * @param newState the new state to be set */ private void changeStateAndStartNewCheckInterval(final State newState) { changeState(newState); checkIntervalData.set(new CheckIntervalData(0, now())); }
/** * {@inheritDoc} This circuit breaker may close itself again if the number of events * received during a check interval goes below the closing threshold. If this circuit * breaker is already open, this method has no effect, except that a new check * interval is started. */ @Override public void open() { super.open(); checkIntervalData.set(new CheckIntervalData(0, now())); }
/** * Returns a new instance of {@code CheckIntervalData} with the event counter * incremented by the given delta. If the delta is 0, this object is returned. * * @param delta the delta * @return the updated instance */ public CheckIntervalData increment(final int delta) { return (delta == 0) ? this : new CheckIntervalData(getEventCount() + delta, getCheckIntervalStart()); } }
/** * {@inheritDoc} A new check interval is started. If too many events are received in * this interval, the circuit breaker changes again to state open. If this circuit * breaker is already closed, this method has no effect, except that a new check * interval is started. */ @Override public void close() { super.close(); checkIntervalData.set(new CheckIntervalData(0, now())); }
/** * Changes the state of this circuit breaker and also initializes a new * {@code CheckIntervalData} object. * * @param newState the new state to be set */ private void changeStateAndStartNewCheckInterval(final State newState) { changeState(newState); checkIntervalData.set(new CheckIntervalData(0, now())); }
/** * Calculates the next {@code CheckIntervalData} object based on the current data and * the current state. The next data object takes the counter increment and the current * time into account. * * @param increment the increment for the internal counter * @param currentData the current check data object * @param currentState the current state of the circuit breaker * @param time the current time * @return the updated {@code CheckIntervalData} object */ private CheckIntervalData nextCheckIntervalData(final int increment, final CheckIntervalData currentData, final State currentState, final long time) { CheckIntervalData nextData; if (stateStrategy(currentState).isCheckIntervalFinished(this, currentData, time)) { nextData = new CheckIntervalData(increment, time); } else { nextData = currentData.increment(increment); } return nextData; }
/** * Returns a new instance of {@code CheckIntervalData} with the event counter * incremented by the given delta. If the delta is 0, this object is returned. * * @param delta the delta * @return the updated instance */ public CheckIntervalData increment(final int delta) { return (delta == 0) ? this : new CheckIntervalData(getEventCount() + delta, getCheckIntervalStart()); } }
/** * {@inheritDoc} This circuit breaker may close itself again if the number of events * received during a check interval goes below the closing threshold. If this circuit * breaker is already open, this method has no effect, except that a new check * interval is started. */ @Override public void open() { super.open(); checkIntervalData.set(new CheckIntervalData(0, now())); }