@Override protected StatusType featureValueOf(Response<T> item) { return item.status(); } };
private Optional<Consumer<Response<ByteString>>> requestRateCounter(MetricId id) { return enabledMetrics.test(ENDPOINT_REQUEST_RATE) ? Optional.of(response -> requestRateMeter(id, response.status().code()).mark()) : Optional.empty(); }
@Override public void response(Response<ByteString> response) { requestRateCounter.ifPresent(consumer -> consumer.accept(response)); responseSizeHistogram .ifPresent(histogram -> response.payload() .ifPresent(payload -> histogram.update(payload.size()))); sentReplies.mark(); timerContext.ifPresent(timer -> { final long duration = timer.stop(); final long durationMs = TimeUnit.NANOSECONDS.toMillis(duration); durationThresholdTracker.ifPresent(counter -> { counter.markDurationThresholds(durationMs); }); }); StatusType.Family family = response.status().family(); if (family != INFORMATIONAL && family != SUCCESSFUL) { sentErrors.mark(); } if (family == CLIENT_ERROR) { sentErrors4xx.mark(); } if (family == SERVER_ERROR) { sentErrors5xx.mark(); } }
private void sendReply(Response<ByteString> response) { if (!replied.compareAndSet(false, true)) { LOGGER.warn("Already replied to ongoing request {} - spurious response {}", request, response); } else { final HttpServletResponse httpResponse = (HttpServletResponse) asyncContext.getResponse(); final StatusType status = response.status(); httpResponse.setStatus(status.code(), status.reasonPhrase()); response.headerEntries().forEach(entry -> httpResponse.addHeader(entry.getKey(), entry.getValue())); response.payload().ifPresent(payload -> { try { payload.write(httpResponse.getOutputStream()); } catch (IOException e) { LOGGER.warn("Failed to write response", e); } }); asyncContext.complete(); logger.accept(this, Optional.of(response)); } }
private static Response<ByteString> applyHttpPayloadSemantics( Request request, Response<ByteString> response) { Response<ByteString> result = response; Optional<ByteString> payload = response.payload(); if (setContentLengthForStatus(response.status())) { int payloadSize = payload.isPresent() ? payload.get().size() : 0; result = result.withHeader("Content-Length", String.valueOf(payloadSize)); } if (!setPayloadForMethod(request.method()) || !setPayloadForStatus(response.status())) { result = result.withPayload(null); } return result; }
@Override protected StatusType featureValueOf(Response<T> item) { return item.status(); } };
private CompletionStage<Boolean> haltActiveBackfillInstance(WorkflowInstance workflowInstance, Client client) { try { final ByteString payload = serialize(Event.halt(workflowInstance)); final Request request = Request.forUri(schedulerApiUrl("events"), "POST") .withPayload(payload); return client.send(request) .thenApply(response -> response.status().family().equals(SUCCESSFUL)); } catch (JsonProcessingException e) { return CompletableFuture.completedFuture(false); } }