1 /* 2 * Copyright 2021 TiKV Project Authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 */ 17 18 package org.tikv.service.failsafe; 19 20 import java.io.Closeable; 21 22 public interface CircuitBreaker extends Closeable { 23 24 enum Status { 25 CLOSED(0), 26 HALF_OPEN(1), 27 OPEN(2); 28 29 private final int value; 30 31 private Status(int value) { 32 this.value = value; 33 } 34 35 public int getValue() { 36 return value; 37 } 38 } 39 40 /** 41 * Every requests asks this if it is allowed to proceed or not. It is idempotent and does not 42 * modify any internal state. 43 * 44 * @return boolean whether a request should be permitted 45 */ 46 boolean allowRequest(); 47 48 /** 49 * Invoked at start of command execution to attempt an execution. This is non-idempotent - it may 50 * modify internal state. 51 */ 52 boolean attemptExecution(); 53 54 /** Invoked on successful executions as part of feedback mechanism when in a half-open state. */ 55 void recordAttemptSuccess(); 56 57 /** Invoked on unsuccessful executions as part of feedback mechanism when in a half-open state. */ 58 void recordAttemptFailure(); 59 60 /** Get the Circuit Breaker Metrics Object. */ 61 CircuitBreakerMetrics getMetrics(); 62 }