Java Code Examples for io.vertx.circuitbreaker.CircuitBreakerState#CLOSED

The following examples show how to use io.vertx.circuitbreaker.CircuitBreakerState#CLOSED . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CircuitBreakerImpl.java    From vertx-circuit-breaker with Apache License 2.0 6 votes vote down vote up
/**
 * A version of reset that can force the the state to `close` even if the circuit breaker is open. This is an
 * internal API.
 *
 * @param force whether or not we force the state and allow an illegal transition
 * @return the current circuit breaker.
 */
public synchronized CircuitBreaker reset(boolean force) {
  rollingFailures.reset();

  if (state == CircuitBreakerState.CLOSED) {
    // Do nothing else.
    return this;
  }

  if (!force && state == CircuitBreakerState.OPEN) {
    // Resetting the circuit breaker while we are in the open state is an illegal transition
    return this;
  }

  state = CircuitBreakerState.CLOSED;
  closeHandler.handle(null);
  sendUpdateOnEventBus();
  return this;
}
 
Example 2
Source File: CircuitBreaker.java    From prebid-server-java with Apache License 2.0 5 votes vote down vote up
/**
 * Resets failure counter to adjust open-circuit time frame.
 * <p>
 * Note: the operations {@link io.vertx.circuitbreaker.CircuitBreaker#state()}
 * and {@link io.vertx.circuitbreaker.CircuitBreaker#reset()} can take a while,
 * so it is better to perform them on a worker thread.
 */
private <T> void ensureState(Promise<T> executeBlockingPromise) {
    final long currentTime = clock.millis();
    if (breaker.state() == CircuitBreakerState.CLOSED && lastFailureTime > 0
            && currentTime - lastFailureTime > openingIntervalMs) {
        breaker.reset();
    }

    lastFailureTime = currentTime;
    executeBlockingPromise.complete();
}
 
Example 3
Source File: CircuitBreakerImpl.java    From vertx-circuit-breaker with Apache License 2.0 4 votes vote down vote up
@Override
public <T> CircuitBreaker executeAndReportWithFallback(
  Promise<T> userFuture,
  Handler<Promise<T>> command,
  Function<Throwable, T> fallback) {

  Context context = vertx.getOrCreateContext();

  CircuitBreakerState currentState;
  synchronized (this) {
    currentState = state;
  }

  CircuitBreakerMetrics.Operation call = metrics.enqueue();

  // this future object tracks the completion of the operation
  // This future is marked as failed on operation failures and timeout.
  Promise<T> operationResult = Promise.promise();

  if (currentState == CircuitBreakerState.CLOSED) {
    operationResult.future().onComplete(event -> {
      context.runOnContext(v -> {
        if (event.failed()) {
          incrementFailures();
          call.failed();
          if (options.isFallbackOnFailure()) {
            invokeFallback(event.cause(), userFuture, fallback, call);
          } else {
            userFuture.fail(event.cause());
          }
        } else {
          call.complete();
          reset();
          userFuture.complete(event.result());
        }
        // Else the operation has been canceled because of a time out.
      });
    });

    if (options.getMaxRetries() > 0) {
      executeOperation(context, command, retryFuture(context, 0, command, operationResult, call), call);
    } else {
      executeOperation(context, command, operationResult, call);
    }
  } else if (currentState == CircuitBreakerState.OPEN) {
    // Fallback immediately
    call.shortCircuited();
    invokeFallback(OpenCircuitException.INSTANCE, userFuture, fallback, call);
  } else if (currentState == CircuitBreakerState.HALF_OPEN) {
    if (passed.incrementAndGet() == 1) {
      operationResult.future().onComplete(event -> {
        context.runOnContext(v -> {
          if (event.failed()) {
            open();
            call.failed();
            if (options.isFallbackOnFailure()) {
              invokeFallback(event.cause(), userFuture, fallback, call);
            } else {
              userFuture.fail(event.cause());
            }
          } else {
            call.complete();
            reset();
            userFuture.complete(event.result());
          }
        });
      });
      // Execute the operation
      executeOperation(context, command, operationResult, call);
    } else {
      // Not selected, fallback.
      call.shortCircuited();
      invokeFallback(OpenCircuitException.INSTANCE, userFuture, fallback, call);
    }
  }
  return this;
}