@Override
  protected void decode(ChannelHandlerContext ctx, RESPONSE msg, List<Object> out)
      throws Exception {
    if (currentDecodingState == DecodingState.INITIAL) {
      currentRequest = sentRequestQueue.poll();
      currentDecodingState = DecodingState.STARTED;
      if (currentRequest != null) {
        Long st = sentRequestTimings.poll();
        if (st != null) {
          currentOpTime = System.nanoTime() - st;
        } else {
          currentOpTime = -1;
        }
      }

      if (traceEnabled) {
        LOGGER.trace("{}Started decoding of {}", logIdent(ctx, endpoint), currentRequest);
      }
    }

    try {
      CouchbaseResponse response = decodeResponse(ctx, msg);
      if (response != null) {
        publishResponse(response, currentRequest.observable());

        if (currentDecodingState == DecodingState.FINISHED) {
          if (currentRequest != null
              && currentOpTime >= 0
              && env() != null
              && env().networkLatencyMetricsCollector().isEnabled()) {
            NetworkLatencyMetricsIdentifier identifier =
                new NetworkLatencyMetricsIdentifier(
                    remoteHostname,
                    serviceType().toString(),
                    currentRequest.getClass().getSimpleName(),
                    response.status().toString());
            env().networkLatencyMetricsCollector().record(identifier, currentOpTime);
          }
        }
      }
    } catch (CouchbaseException e) {
      currentRequest.observable().onError(e);
    } catch (Exception e) {
      currentRequest.observable().onError(new CouchbaseException(e));
    }

    if (currentDecodingState == DecodingState.FINISHED) {
      if (traceEnabled) {
        LOGGER.trace("{}Finished decoding of {}", logIdent(ctx, endpoint), currentRequest);
      }
      currentRequest = null;
      currentDecodingState = DecodingState.INITIAL;
    }
  }
Example #2
0
 @Override
 protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception {
   FullHttpRequest request = this.request = msg;
   QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.uri());
   uriRequest = queryStringDecoder.path();
   logger.debug("Msg: " + uriRequest);
   if (uriRequest.contains("gre/")
       || uriRequest.contains("img/")
       || uriRequest.contains("res/")
       || uriRequest.contains("favicon.ico")) {
     HttpWriteCacheEnable.writeFile(
         request,
         ctx,
         Configuration.configuration.getHttpBasePath() + uriRequest,
         R66SESSION + Configuration.configuration.getHOST_ID());
     ctx.flush();
     return;
   }
   checkSession(ctx.channel());
   if (!authentHttp.isAuthenticated()) {
     logger.debug("Not Authent: " + uriRequest + ":{}", authentHttp);
     checkAuthent(ctx);
     return;
   }
   String find = uriRequest;
   if (uriRequest.charAt(0) == '/') {
     find = uriRequest.substring(1);
   }
   find = find.substring(0, find.indexOf("."));
   REQUEST req = REQUEST.index;
   try {
     req = REQUEST.valueOf(find);
   } catch (IllegalArgumentException e1) {
     req = REQUEST.index;
     logger.debug("NotFound: " + find + ":" + uriRequest);
   }
   switch (req) {
     case index:
       responseContent.append(index());
       break;
     case Logon:
       responseContent.append(index());
       break;
     case System:
       responseContent.append(System());
       break;
     default:
       responseContent.append(index());
       break;
   }
   writeResponse(ctx);
 }
  /**
   * Cancells any outstanding operations which are currently on the wire.
   *
   * @param ctx the handler context.
   */
  private void handleOutstandingOperations(final ChannelHandlerContext ctx) {
    if (sentRequestQueue.isEmpty()) {
      LOGGER.trace(logIdent(ctx, endpoint) + "Not cancelling operations - sent queue is empty.");
      return;
    }

    LOGGER.debug(
        logIdent(ctx, endpoint)
            + "Cancelling "
            + sentRequestQueue.size()
            + " outstanding requests.");
    while (!sentRequestQueue.isEmpty()) {
      REQUEST req = sentRequestQueue.poll();
      try {
        sideEffectRequestToCancel(req);
        req.observable().onError(new RequestCancelledException("Request cancelled in-flight."));
      } catch (Exception ex) {
        LOGGER.info("Exception thrown while cancelling outstanding operation: " + req, ex);
      }
    }

    sentRequestTimings.clear();
  }