You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@openwhisk.apache.org by GitBox <gi...@apache.org> on 2018/02/27 12:50:59 UTC

[GitHub] markusthoemmes commented on a change in pull request #3347: Generic Pooling REST Client

markusthoemmes commented on a change in pull request #3347: Generic Pooling REST Client
URL: https://github.com/apache/incubator-openwhisk/pull/3347#discussion_r170910847
 
 

 ##########
 File path: common/scala/src/main/scala/whisk/http/PoolingRestClient.scala
 ##########
 @@ -0,0 +1,154 @@
+/*
+ * 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 whisk.http
+
+import scala.concurrent.Future
+import scala.concurrent.Promise
+import scala.util.{Failure, Success}
+
+import akka.actor.ActorSystem
+import akka.http.scaladsl.Http
+import akka.http.scaladsl.Http.HostConnectionPool
+import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
+import akka.http.scaladsl.marshalling._
+import akka.http.scaladsl.model._
+import akka.http.scaladsl.unmarshalling._
+import akka.stream.ActorMaterializer
+import akka.stream.OverflowStrategy
+import akka.stream.QueueOfferResult
+import akka.stream.scaladsl._
+
+import spray.json._
+
+/**
+ * This class only handles the basic communication to the proper endpoints.
+ *  It is up to its clients to interpret the results. It is built on akka-http
+ *  host-level connection pools; compared to single requests, it saves some time
+ *  on each request because it doesn't need to look up the pool corresponding
+ *  to the host. It is also easier to add an extra queueing mechanism.
+ */
+class PoolingRestClient(protocol: String, host: String, port: Int, queueSize: Int)(implicit system: ActorSystem) {
+  require(protocol == "http" || protocol == "https", "Protocol must be one of { http, https }.")
+
+  implicit val context = system.dispatcher
+  implicit val materializer = ActorMaterializer()
+
+  // Creates or retrieves a connection pool for the host.
+  private val pool = if (protocol == "http") {
+    Http().cachedHostConnectionPool[Promise[HttpResponse]](host = host, port = port)
+  } else {
+    Http().cachedHostConnectionPoolHttps[Promise[HttpResponse]](host = host, port = port)
+  }
+
+  private val poolPromise = Promise[HostConnectionPool]
+
+  // Additional queue in case all connections are busy. Should hardly ever be
+  // filled in practice but can be useful, e.g., in tests starting many
+  // asynchronous requests in a very short period of time.
+  private val requestQueue = Source
+    .queue(queueSize, OverflowStrategy.dropNew)
+    .via(pool.mapMaterializedValue { x =>
+      poolPromise.success(x); x
+    })
+    .toMat(Sink.foreach({
+      case ((Success(response), p)) => p.success(response)
+      case ((Failure(error), p))    => p.failure(error)
+    }))(Keep.left)
+    .run
+
+  // Prepares a request with the proper headers.
+  def mkRequest0(method: HttpMethod,
+                 uri: Uri,
+                 body: Future[MessageEntity],
+                 headers: List[HttpHeader] = List()): Future[HttpRequest] = {
 
 Review comment:
   Please use `List.empty` instead. Much less garbage and more clear.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services