    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
    019/**
    020 * An interface describing a <a
    021 * href="https://martinfowler.com/bliki/CircuitBreaker.html">Circuit Breaker</a> component.
    022 *
    023 * <p>
    024 * A <em>circuit breaker</em> can be used to protect an application against unreliable
    025 * services or unexpected load. It typically monitors a specific resource. As long as this
    026 * resource works as expected, it stays in state <em>closed</em>, meaning that the
    027 * resource can be used. If problems are encountered when using the resource, the circuit
    028 * breaker can switch into state <em>open</em>; then access to this resource is
    029 * prohibited. Depending on a concrete implementation, it is possible that the circuit
    030 * breaker switches back to state <em>closed</em> when the resource becomes available
    031 * again.
    032 * </p>
    033 * <p>
    034 * This interface defines a generic protocol of a circuit breaker component. It should be
    035 * sufficiently generic to be applied to multiple different use cases.
    036 * </p>
    037 *
    038 * @param <T> the type of the value monitored by this circuit breaker
    039 * @since 3.5
    040 */
    041public interface CircuitBreaker<T> {
    042    /**
    043     * Returns the current open state of this circuit breaker. A return value of
    044     * <strong>true</strong> means that the circuit breaker is currently open indicating a
    045     * problem in the monitored sub system.
    046     *
    047     * @return the current open state of this circuit breaker
    048     */
    049    boolean isOpen();
    050
    051    /**
    052     * Returns the current closed state of this circuit breaker. A return value of
    053     * <strong>true</strong> means that the circuit breaker is currently closed. This
    054     * means that everything is okay with the monitored sub system.
    055     *
    056     * @return the current closed state of this circuit breaker
    057     */
    058    boolean isClosed();
    059
    060    /**
    061     * Checks the state of this circuit breaker and changes it if necessary. The return
    062     * value indicates whether the circuit breaker is now in state <em>closed</em>; a value
    063     * of <strong>true</strong> typically means that the current operation can continue.
    064     *
    065     * @return <strong>true</strong> if the circuit breaker is now closed;
    066     * <strong>false</strong> otherwise
    067     */
    068    boolean checkState();
    069
    070    /**
    071     * Closes this circuit breaker. Its state is changed to closed. If this circuit
    072     * breaker is already closed, this method has no effect.
    073     */
    074    void close();
    075
    076    /**
    077     * Opens this circuit breaker. Its state is changed to open. Depending on a concrete
    078     * implementation, it may close itself again if the monitored sub system becomes
    079     * available. If this circuit breaker is already open, this method has no effect.
    080     */
    081    void open();
    082
    083    /**
    084     * Increments the monitored value and performs a check of the current state of this
    085     * circuit breaker. This method works like {@link #checkState()}, but the monitored
    086     * value is incremented before the state check is performed.
    087     *
    088     * @param increment value to increment in the monitored value of the circuit breaker
    089     * @return <strong>true</strong> if the circuit breaker is now closed;
    090     * <strong>false</strong> otherwise
    091     */
    092    boolean incrementAndCheckState(T increment);
    093}