kfaraz commented on code in PR #20784:
URL: https://github.com/apache/druid/pull/20784#discussion_r4424337991


##########
server/src/main/java/org/apache/druid/server/http/AsyncRequest.java:
##########
@@ -0,0 +1,301 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.druid.server.http;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.JavaType;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.ObjectWriter;
+import com.google.common.base.Preconditions;
+import com.google.common.util.concurrent.FutureCallback;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.MoreExecutors;
+import org.apache.druid.java.util.common.logger.Logger;
+
+import javax.annotation.Nullable;
+import javax.servlet.AsyncContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.core.MediaType;
+import java.lang.reflect.Type;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.Function;
+
+/**
+ * Wraps the {@link AsyncContext} of a {@link HttpServletRequest} and ensures 
that the
+ * response is completed at most once, guarding against writing to a timed-out 
or already-completed request.
+ * Use {@link #completed(HttpServletRequest, ObjectMapper, AsyncResponse)} to 
complete a request immediately, or
+ * {@link #create(HttpServletRequest, ListenableFuture, long)} to obtain a 
{@link Builder} that responds based on
+ * the outcome of the future; call {@link Builder#contentType} to specify how 
to write the response entity.
+ */
+public class AsyncRequest
+{
+  private static final Logger log = new Logger(AsyncRequest.class);
+
+  private final AtomicBoolean completed = new AtomicBoolean(false);
+  private final AsyncContext context;
+  private final ObjectMapper objectMapper;
+  private final String contentType;
+
+  /**
+   * Starts async processing of the request and immediately completes it with 
the given response, writing any
+   * response entity as JSON using the given {@link ObjectMapper}.
+   */
+  public static <T> AsyncRequest completed(HttpServletRequest request, 
ObjectMapper jsonMapper, AsyncResponse<T> response)
+  {
+    return completed(request, jsonMapper, MediaType.APPLICATION_JSON, 
response);
+  }
+
+  /**
+   * Starts async processing of the request and immediately completes it with 
the given response, writing any
+   * response entity using the given {@link ObjectMapper} and content type.
+   */
+  public static <T> AsyncRequest completed(HttpServletRequest request, 
ObjectMapper objectMapper, String contentType, AsyncResponse<T> response)
+  {
+    final AsyncRequest asyncRequest = new AsyncRequest(request, objectMapper, 
contentType);
+    asyncRequest.complete(response);
+    return asyncRequest;
+  }
+
+  /**
+   * Creates a {@link Builder} that waits on the given future. Call {@link 
Builder#contentType} to specify the
+   * {@link ObjectMapper} and content type to use for writing the response 
entity.
+   */
+  public static <T> Builder<T> create(HttpServletRequest request, 
ListenableFuture<T> future, long timeoutMillis)
+  {
+    Preconditions.checkArgument(timeoutMillis > 0, "timeoutMillis[%s] must be 
greater than zero", timeoutMillis);
+    return new Builder<>(request, future, timeoutMillis);
+  }
+
+  private AsyncRequest(HttpServletRequest request, ObjectMapper objectMapper, 
String contentType)
+  {
+    this.context = request.startAsync();
+    this.objectMapper = objectMapper;
+    this.contentType = contentType;
+  }
+
+  /**
+   * Completes the request as described by the given {@link AsyncResponse}, if 
it hasn't already been completed.
+   */
+  private <T> void complete(AsyncResponse<T> response)
+  {
+    if (!completed.compareAndSet(false, true)) {
+      log.warn("Request has already been completed");

Review Comment:
   ```suggestion
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to