synchronized void maybeUpdateCurrentEndpoint()

in grpc-gcp/src/main/java/com/google/cloud/grpc/multiendpoint/MultiEndpoint.java [217:238]


  synchronized void maybeUpdateCurrentEndpoint() {
    Optional<Endpoint> topEndpoint =
        endpointsMap
            .values()
            .stream()
            .filter((c) -> c.getState().equals(EndpointState.AVAILABLE))
            .min(comparingInt(Endpoint::getPriority));

    Endpoint current = endpointsMap.get(currentId);
    if (current != null && current.getState().equals(EndpointState.RECOVERING)) {
      // Keep recovering endpoint as current unless a higher priority endpoint became available.
      if (!topEndpoint.isPresent() || topEndpoint.get().getPriority() >= current.getPriority()) {
        return;
      }
    }

    if (!topEndpoint.isPresent() && current == null) {
      topEndpoint = endpointsMap.values().stream().min(comparingInt(Endpoint::getPriority));
    }

    topEndpoint.ifPresent(endpoint -> updateCurrentEndpoint(current, endpoint.getId()));
  }