public void receiveResponse(UaResponseMessage response) {
    ResponseHeader header = response.getResponseHeader();
    UInteger requestHandle = header.getRequestHandle();

    CompletableFuture<UaResponseMessage> future = pending.remove(requestHandle);

    if (future != null) {
      if (header.getServiceResult().isGood()) {
        future.complete(response);
      } else {
        ServiceFault serviceFault;

        if (response instanceof ServiceFault) {
          serviceFault = (ServiceFault) response;
        } else {
          serviceFault = new ServiceFault(header);
        }

        future.completeExceptionally(new UaServiceFaultException(serviceFault));
      }

      Timeout timeout = timeouts.remove(requestHandle);
      if (timeout != null) timeout.cancel();
    } else {
      logger.debug(
          "Received {} for unknown requestHandle: {}",
          response.getClass().getSimpleName(),
          requestHandle);
    }
  }
 private CompletableFuture<HRegionLocation> withTimeout(
     CompletableFuture<HRegionLocation> future, long timeoutNs, Supplier<String> timeoutMsg) {
   if (future.isDone() || timeoutNs <= 0) {
     return future;
   }
   Timeout timeoutTask =
       retryTimer.newTimeout(
           t -> {
             if (future.isDone()) {
               return;
             }
             future.completeExceptionally(new TimeoutIOException(timeoutMsg.get()));
           },
           timeoutNs,
           TimeUnit.NANOSECONDS);
   return future.whenComplete(
       (loc, error) -> {
         if (error != null && error.getClass() != TimeoutIOException.class) {
           // cancel timeout task if we are not completed by it.
           timeoutTask.cancel();
         }
       });
 }