    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.lang3.concurrent;
    018
    019import java.util.concurrent.atomic.AtomicLong;
    020
    021/**
    022 * <p>
    023 * A simple implementation of the <a
    024 * href="http://martinfowler.com/bliki/CircuitBreaker.html">Circuit Breaker</a> pattern
    025 * that opens if the requested increment amount is greater than a given threshold.
    026 * </p>
    027 *
    028 * <p>
    029 * It contains an internal counter that starts in zero, and each call increments the counter by a given amount.
    030 * If the threshold is zero, the circuit breaker will be in a permanent <em>open</em> state.
    031 * </p>
    032 *
    033 * <p>
    034 * An example of use case could be a memory circuit breaker.
    035 * </p>
    036 *
    037 * <pre>
    038 * long threshold = 10L;
    039 * ThresholdCircuitBreaker breaker = new ThresholdCircuitBreaker(10L);
    040 * ...
    041 * public void handleRequest(Request request) {
    042 *     long memoryUsed = estimateMemoryUsage(request);
    043 *     if (breaker.incrementAndCheckState(memoryUsed)) {
    044 *         // actually handle this request
    045 *     } else {
    046 *         // do something else, e.g. send an error code
    047 *     }
    048 * }
    049 * </pre>
    050 *
    051 * <p>#Thread safe#</p>
    052 * @since 3.5
    053 */
    054public class ThresholdCircuitBreaker extends AbstractCircuitBreaker<Long> {
    055    /**
    056     * The initial value of the internal counter.
    057     */
    058    private static final long INITIAL_COUNT = 0L;
    059
    060    /**
    061     * The threshold.
    062     */
    063    private final long threshold;
    064
    065    /**
    066     * Controls the amount used.
    067     */
    068    private final AtomicLong used;
    069
    070    /**
    071     * <p>Creates a new instance of {@code ThresholdCircuitBreaker} and initializes the threshold.</p>
    072     *
    073     * @param threshold the threshold.
    074     */
    075    public ThresholdCircuitBreaker(final long threshold) {
    076        this.used = new AtomicLong(INITIAL_COUNT);
    077        this.threshold = threshold;
    078    }
    079
    080    /**
    081     * Gets the threshold.
    082     *
    083     * @return the threshold
    084     */
    085    public long getThreshold() {
    086        return threshold;
    087    }
    088
    089    /**
    090     * {@inheritDoc}
    091     */
    092    @Override
    093    public boolean checkState() {
    094        return isOpen();
    095    }
    096
    097    /**
    098     * {@inheritDoc}
    099     *
    100     * <p>Resets the internal counter back to its initial value (zero).</p>
    101     */
    102    @Override
    103    public void close() {
    104        super.close();
    105        this.used.set(INITIAL_COUNT);
    106    }
    107
    108    /**
    109     * {@inheritDoc}
    110     *
    111     * <p>If the threshold is zero, the circuit breaker will be in a permanent <em>open</em> state.</p>
    112     */
    113    @Override
    114    public boolean incrementAndCheckState(final Long increment) {
    115        if (threshold == 0) {
    116            open();
    117        }
    118
    119        final long used = this.used.addAndGet(increment);
    120        if (used > threshold) {
    121            open();
    122        }
    123
    124        return checkState();
    125    }
    126
    127}