Esempio n. 1
0
  /**
   * Checks client health periodically to see if we are connected. Tries to reconnect if not
   * connected.
   */
  private void checkClient() {

    try {

      /** If the errorCount is greater than 0, make sure we are still connected. */
      if (errorCount.get() > 0) {
        errorCount.set(0);
        if (backendServiceHttpClient == null || backendServiceHttpClient.isClosed()) {

          if (backendServiceHttpClient != null) {
            try {
              backendServiceHttpClient.stop();
            } catch (Exception ex) {
              logger.debug("Was unable to stop the client connection", ex);
            }
          }
          backendServiceHttpClient = httpClientBuilder.buildAndStart();
          lastHttpClientStart = time;
        }
      }

      /** If the ping builder is present, use it to ping the service. */
      if (pingBuilder.isPresent()) {

        if (backendServiceHttpClient != null) {
          pingBuilder
              .get()
              .setBinaryReceiver(
                  (code, contentType, body) -> {
                    if (code >= 200 && code < 299) {
                      pingCount.incrementAndGet();
                    } else {
                      errorCount.incrementAndGet();
                    }
                  })
              .setErrorHandler(
                  e -> {
                    logger.error("Error doing ping operation", e);
                    errorCount.incrementAndGet();
                  });

          final HttpRequest httpRequest = pingBuilder.get().build();

          backendServiceHttpClient.sendHttpRequest(httpRequest);
        }
      }

    } catch (Exception ex) {
      errorHandler.accept(ex);
      logger.error("Unable to check connection");
    }
  }
Esempio n. 2
0
  /**
   * Creates a backend request from the client request and then forwards it.
   *
   * @param clientRequest clientRequest
   */
  private void createBackEndRequestPopulateAndForward(final HttpRequest clientRequest) {
    try {

      if (backendServiceHttpClient == null) {
        handleHttpClientErrorsForBackend(
            clientRequest, new HttpClientClosedConnectionException("Not connected"));
        long timeSinceLastStart = time - lastHttpClientStart;
        if (timeSinceLastStart > 10_000) {
          checkClient();
        }
        return;
      }
      /* forward request to backend client. */
      final HttpRequestBuilder httpRequestBuilder =
          HttpRequestBuilder.httpRequestBuilder()
              .copyRequest(clientRequest)
              .setBinaryReceiver(
                  new HttpBinaryReceiver() {
                    @Override
                    public void response(
                        final int code,
                        final String contentType,
                        final byte[] body,
                        final MultiMap<String, String> headers) {
                      handleBackendClientResponses(clientRequest, code, contentType, body, headers);
                    }

                    @Override
                    public void response(int code, String contentType, byte[] body) {
                      response(code, contentType, body, MultiMap.empty());
                    }
                  })
              .setErrorHandler(e -> handleHttpClientErrorsForBackend(clientRequest, e));

      /** Give user of the lib a chance to populate headers and such. */
      beforeSend.accept(httpRequestBuilder);

      backendServiceHttpClient.sendHttpRequest(httpRequestBuilder.build());
    } catch (HttpClientClosedConnectionException httpClientClosedConnectionException) {

      errorCount.incrementAndGet();
      errorHandler.accept(httpClientClosedConnectionException);
      logger.error("Unable to forward request", httpClientClosedConnectionException);
      handleHttpClientErrorsForBackend(clientRequest, httpClientClosedConnectionException);
      backendServiceHttpClient = null;

      long timeSinceLastStart = time - lastHttpClientStart;
      if (timeSinceLastStart > 10_000) {
        checkClient();
      }

    } catch (Exception ex) {
      errorCount.incrementAndGet();
      errorHandler.accept(ex);
      logger.error("Unable to forward request", ex);
      handleHttpClientErrorsForBackend(clientRequest, ex);

      long timeSinceLastStart = time - lastHttpClientStart;
      if (timeSinceLastStart > 10_000) {
        checkClient();
      }
    }
  }