ThresholdCircuitBreaker.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.lang3.concurrent;

  18. import java.util.concurrent.atomic.AtomicLong;

  19. /**
  20.  * A simple implementation of the <a
  21.  * href="https://martinfowler.com/bliki/CircuitBreaker.html">Circuit Breaker</a> pattern
  22.  * that opens if the requested increment amount is greater than a given threshold.
  23.  *
  24.  * <p>
  25.  * It contains an internal counter that starts in zero, and each call increments the counter by a given amount.
  26.  * If the threshold is zero, the circuit breaker will be in a permanent <em>open</em> state.
  27.  * </p>
  28.  *
  29.  * <p>
  30.  * An example of use case could be a memory circuit breaker.
  31.  * </p>
  32.  *
  33.  * <pre>
  34.  * long threshold = 10L;
  35.  * ThresholdCircuitBreaker breaker = new ThresholdCircuitBreaker(10L);
  36.  * ...
  37.  * public void handleRequest(Request request) {
  38.  *     long memoryUsed = estimateMemoryUsage(request);
  39.  *     if (breaker.incrementAndCheckState(memoryUsed)) {
  40.  *         // actually handle this request
  41.  *     } else {
  42.  *         // do something else, e.g. send an error code
  43.  *     }
  44.  * }
  45.  * </pre>
  46.  *
  47.  * <p>#Thread safe#</p>
  48.  * @since 3.5
  49.  */
  50. public class ThresholdCircuitBreaker extends AbstractCircuitBreaker<Long> {
  51.     /**
  52.      * The initial value of the internal counter.
  53.      */
  54.     private static final long INITIAL_COUNT = 0L;

  55.     /**
  56.      * The threshold.
  57.      */
  58.     private final long threshold;

  59.     /**
  60.      * Controls the amount used.
  61.      */
  62.     private final AtomicLong used;

  63.     /**
  64.      * Creates a new instance of {@link ThresholdCircuitBreaker} and initializes the threshold.
  65.      *
  66.      * @param threshold the threshold.
  67.      */
  68.     public ThresholdCircuitBreaker(final long threshold) {
  69.         this.used = new AtomicLong(INITIAL_COUNT);
  70.         this.threshold = threshold;
  71.     }

  72.     /**
  73.      * {@inheritDoc}
  74.      */
  75.     @Override
  76.     public boolean checkState() {
  77.         return !isOpen();
  78.     }

  79.     /**
  80.      * {@inheritDoc}
  81.      *
  82.      * <p>Resets the internal counter back to its initial value (zero).</p>
  83.      */
  84.     @Override
  85.     public void close() {
  86.         super.close();
  87.         this.used.set(INITIAL_COUNT);
  88.     }

  89.     /**
  90.      * Gets the threshold.
  91.      *
  92.      * @return the threshold
  93.      */
  94.     public long getThreshold() {
  95.         return threshold;
  96.     }

  97.     /**
  98.      * {@inheritDoc}
  99.      *
  100.      * <p>If the threshold is zero, the circuit breaker will be in a permanent <em>open</em> state.</p>
  101.      */
  102.     @Override
  103.     public boolean incrementAndCheckState(final Long increment) {
  104.         if (threshold == 0) {
  105.             open();
  106.         }

  107.         final long used = this.used.addAndGet(increment);
  108.         if (used > threshold) {
  109.             open();
  110.         }

  111.         return checkState();
  112.     }

  113. }