title: Correct use of Akka http client connection pools
body: <p>I need to consume a REST service using Akka's HTTP client (v2.0.2). The logical approach is to do this via a host connection pool because we expect large numbers of simultaneous connections. The <code>Flow</code> for this consumes a <code>(HttpRequest, T)</code> and returns a <code>(Try[HttpResponse, T)</code>. The <a href="http://doc.akka.io/docs/akka-stream-and-http-experimental/2.0.2/scala/http/client-side/host-level.html#Using_a_Host_Connection_Pool" rel="nofollow noreferrer">documentation</a> indicates that some arbitrary type <code>T</code> is needed to manage potential out of order responses to requests but does not point out what the caller is supposed to do with the returned <code>T</code>. </p>
  
  <p>My first attempt is the function below using an <code>Int</code> as <code>T</code>. It is called from many places to ensure that the connections use a single pool. </p>
  
  <pre><code>val pool = Http().cachedHostConnectionPool[Int]("127.0.0.1", 8888, ConnectionPoolSettings(system))
  
  def pooledRequest(req: HttpRequest): Future[HttpResponse] = {
    val unique = Random.nextInt
    Source.single(req → unique).via(pool).runWith(Sink.head).flatMap {
      case (Success(r: HttpResponse), `unique`) ⇒ Future.successful(r)
      case (Failure(f), `unique`) ⇒ Future.failed(f)
      case (_, i) ⇒ Future.failed(new Exception("Return does not match the request"))
    }
  }
  </code></pre>
  
  <p>The question is how should the client use this <code>T</code>? Is there a cleaner more efficient solution? And finally, Is my paranoia that something may arrive out of order not actually paranoia?</p>