{"action":"created","comment":{"url":"https://api.github.com/repos/akka/akka/pulls/comments/57872935","id":57872935,"diff_hunk":"@@ -0,0 +1,140 @@\n+package akka.http.impl.engine.client\n+\n+import java.util.concurrent.atomic.AtomicInteger\n+\n+import akka.Done\n+import akka.actor.{ Actor, ActorRef, DeadLetterSuppression, Deploy, NoSerializationVerificationNeeded, Props, Terminated }\n+import akka.http.impl.engine.client.PoolInterfaceActor.PoolRequest\n+import akka.http.impl.settings.HostConnectionPoolSetup\n+import akka.http.scaladsl.HttpExt\n+import akka.http.scaladsl.model.{ HttpRequest, HttpResponse }\n+import akka.stream.Materializer\n+\n+import scala.concurrent.Promise\n+\n+/**\n+ * Manages access to a host connection pool or rather: a sequence of pool incarnations.\n+ *\n+ * A host connection pool for a given [[HostConnectionPoolSetup]] is a running stream, whose outside interface is\n+ * provided by its [[PoolInterfaceActor]] actor. The actor accepts [[PoolInterfaceActor.PoolRequest]] messages\n+ * and completes their `responsePromise` whenever the respective response has been received (or an error occurred).\n+ *\n+ * The [[PoolMasterActor]] provides a layer of indirection between a [[PoolGateway]], which represents a pool,\n+ * and the [[PoolInterfaceActor]] instances which are created on-demand and stopped after an idle-timeout.\n+ *\n+ * Several [[PoolGateway]] objects may be mapped to the same pool if they have the same [[HostConnectionPoolSetup]]\n+ * and are marked as being shared. This is the case for example for gateways obtained through\n+ * [[HttpExt.cachedHostConnectionPool]]. Some other gateways are not shared, such as those obtained through\n+ * [[HttpExt.newHostConnectionPool]], and will have their dedicated restartable pool.\n+ *\n+ */\n+private[http] class PoolMasterActor extends Actor {\n+\n+  import PoolMasterActor._\n+\n+  private[this] implicit val dispatcher = context.dispatcher\n+\n+  private[this] var poolStatus = Map[PoolGateway, PoolInterfaceStatus]()\n+  private[this] var poolInterfaces = Map[ActorRef, PoolGateway]()\n+\n+  /**\n+   * Start a new pool interface actor, register it in our maps, and watch its death.\n+   *\n+   * @param gateway the pool gateway this pool corresponds to\n+   * @param fm the materializer to use for this pool\n+   * @return the newly created actor ref\n+   */\n+  private[this] def startPoolInterfaceActor(gateway: PoolGateway)(implicit fm: Materializer): ActorRef = {\n+    val props = Props(new PoolInterfaceActor(gateway)).withDeploy(Deploy.local)\n+    val ref = context.system.actorOf(props, PoolInterfaceActor.name.next())\n+    poolStatus += gateway -> PoolInterfaceRunning(ref)\n+    poolInterfaces += ref -> gateway\n+    context.watch(ref)\n+  }\n+\n+  def receive = {\n+\n+    // Start or restart a pool without sending it a request. This is used to ensure that\n+    // freshly created pools will be ready to serve requests immediately.\n+    case s @ StartPool(gateway, materializer) ⇒\n+      poolStatus.get(gateway) match {\n+        case Some(PoolInterfaceRunning(_)) ⇒\n+        case Some(PoolInterfaceShuttingDown(shutdownCompletedPromise)) ⇒\n+          shutdownCompletedPromise.future.foreach(_ ⇒ self ! s)\n+        case None ⇒\n+          startPoolInterfaceActor(gateway)(materializer)\n+      }\n+\n+    // Send a request to a pool. If needed, the pool will be started or restarted.\n+    case s @ SendRequest(gateway, request, responsePromise, materializer) ⇒\n+      poolStatus.get(gateway) match {\n+        case Some(PoolInterfaceRunning(ref)) ⇒\n+          ref ! PoolRequest(request, responsePromise)\n+        case Some(PoolInterfaceShuttingDown(shutdownCompletedPromise)) ⇒\n+          // The request will be resent when the pool shutdown is complete (the first\n+          // request will recreate the pool).\n+          shutdownCompletedPromise.future.foreach(_ ⇒ self ! s)\n+        case None ⇒\n+          startPoolInterfaceActor(gateway)(materializer) ! PoolRequest(request, responsePromise)\n+      }\n+\n+    // Shutdown a pool and signal its termination.\n+    case Shutdown(gateway, shutdownCompletedPromise) ⇒\n+      poolStatus.get(gateway).foreach {\n+        case PoolInterfaceRunning(ref) ⇒\n+          ref ! PoolInterfaceActor.Shutdown\n+          poolStatus += gateway -> PoolInterfaceShuttingDown(shutdownCompletedPromise)\n+        case PoolInterfaceShuttingDown(formerPromise) ⇒\n+          shutdownCompletedPromise.completeWith(formerPromise.future)\n+        case _ ⇒\n+          shutdownCompletedPromise.success(Done)\n+      }\n+\n+    // Shutdown all known pools and signal their termination.\n+    case ShutdownAll(shutdownCompletedPromise) ⇒\n+      val allGateways = poolStatus.keys\n+      val counter = new AtomicInteger(allGateways.size)\n+      for (gateway ← allGateways)\n+        gateway.shutdown().foreach(_ ⇒ if (counter.decrementAndGet() == 0) shutdownCompletedPromise.success(Done))\n+\n+    // When a pool actor terminate, signal its termination and remove it from our maps.\n+    case Terminated(ref) ⇒\n+      poolInterfaces.get(ref).foreach { gateway ⇒\n+        poolStatus.get(gateway) match {\n+          case Some(PoolInterfaceShuttingDown(shutdownCompletedPromise)) ⇒\n+            shutdownCompletedPromise.success(Done)\n+          case _ ⇒","path":"akka-http-core/src/main/scala/akka/http/impl/engine/client/PoolMasterActor.scala","position":106,"original_position":106,"commit_id":"b2680c58c23cd4807acf376e0ba07047b040a159","original_commit_id":"b2680c58c23cd4807acf376e0ba07047b040a159","user":{"login":"samueltardieu","id":44656,"avatar_url":"https://avatars.githubusercontent.com/u/44656?v=3","gravatar_id":"","url":"https://api.github.com/users/samueltardieu","html_url":"https://github.com/samueltardieu","followers_url":"https://api.github.com/users/samueltardieu/followers","following_url":"https://api.github.com/users/samueltardieu/following{/other_user}","gists_url":"https://api.github.com/users/samueltardieu/gists{/gist_id}","starred_url":"https://api.github.com/users/samueltardieu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/samueltardieu/subscriptions","organizations_url":"https://api.github.com/users/samueltardieu/orgs","repos_url":"https://api.github.com/users/samueltardieu/repos","events_url":"https://api.github.com/users/samueltardieu/events{/privacy}","received_events_url":"https://api.github.com/users/samueltardieu/received_events","type":"User","site_admin":false},"body":"Ok.","created_at":"2016-03-30T11:26:43Z","updated_at":"2016-03-30T11:26:43Z","html_url":"https://github.com/akka/akka/pull/20082#discussion_r57872935","pull_request_url":"https://api.github.com/repos/akka/akka/pulls/20082","_links":{"self":{"href":"https://api.github.com/repos/akka/akka/pulls/comments/57872935"},"html":{"href":"https://github.com/akka/akka/pull/20082#discussion_r57872935"},"pull_request":{"href":"https://api.github.com/repos/akka/akka/pulls/20082"}}},"pull_request":{"url":"https://api.github.com/repos/akka/akka/pulls/20082","id":63464812,"html_url":"https://github.com/akka/akka/pull/20082","diff_url":"https://github.com/akka/akka/pull/20082.diff","patch_url":"https://github.com/akka/akka/pull/20082.patch","issue_url":"https://api.github.com/repos/akka/akka/issues/20082","number":20082,"state":"open","locked":false,"title":"=htc Simplify and fix pool gateway synchronization","user":{"login":"samueltardieu","id":44656,"avatar_url":"https://avatars.githubusercontent.com/u/44656?v=3","gravatar_id":"","url":"https://api.github.com/users/samueltardieu","html_url":"https://github.com/samueltardieu","followers_url":"https://api.github.com/users/samueltardieu/followers","following_url":"https://api.github.com/users/samueltardieu/following{/other_user}","gists_url":"https://api.github.com/users/samueltardieu/gists{/gist_id}","starred_url":"https://api.github.com/users/samueltardieu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/samueltardieu/subscriptions","organizations_url":"https://api.github.com/users/samueltardieu/orgs","repos_url":"https://api.github.com/users/samueltardieu/repos","events_url":"https://api.github.com/users/samueltardieu/events{/privacy}","received_events_url":"https://api.github.com/users/samueltardieu/received_events","type":"User","site_admin":false},"body":"Rewrite the pool gateway synchronization so that:\r\n- The documented race condition in PoolInterfaceActor is gone. No\r\n  PoolInterfaceActor will receive new requests after the gateway\r\n  shutdown has been initiated (fix #20081).\r\n- A gateway created without using the cache by newHostConnectionPool\r\n  will build a (collapsing) chain of gateways and keep different\r\n  chains independent of each other. Each chain will use the same\r\n  materializer in every gateway incarnation (fix #20080).\r\n- Gateways created through the cache will build, for a given hcps,\r\n  a tree with every branch collapsing to the top. At most one gateway\r\n  (at the top) is in the running state at a given moment. The same\r\n  materializer (used initially when going through the cache) will\r\n  be used for all the incarnations.\r\n- The locked sections have been kept as short as possible and only\r\n  contain fast-executing operations.","created_at":"2016-03-19T10:38:56Z","updated_at":"2016-03-30T11:26:43Z","closed_at":null,"merged_at":null,"merge_commit_sha":"090608290c8912a4f1a99183c0b2a88659fdf613","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/akka/akka/pulls/20082/commits","review_comments_url":"https://api.github.com/repos/akka/akka/pulls/20082/comments","review_comment_url":"https://api.github.com/repos/akka/akka/pulls/comments{/number}","comments_url":"https://api.github.com/repos/akka/akka/issues/20082/comments","statuses_url":"https://api.github.com/repos/akka/akka/statuses/b2680c58c23cd4807acf376e0ba07047b040a159","head":{"label":"samueltardieu:pool-gateway-synchronization","ref":"pool-gateway-synchronization","sha":"b2680c58c23cd4807acf376e0ba07047b040a159","user":{"login":"samueltardieu","id":44656,"avatar_url":"https://avatars.githubusercontent.com/u/44656?v=3","gravatar_id":"","url":"https://api.github.com/users/samueltardieu","html_url":"https://github.com/samueltardieu","followers_url":"https://api.github.com/users/samueltardieu/followers","following_url":"https://api.github.com/users/samueltardieu/following{/other_user}","gists_url":"https://api.github.com/users/samueltardieu/gists{/gist_id}","starred_url":"https://api.github.com/users/samueltardieu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/samueltardieu/subscriptions","organizations_url":"https://api.github.com/users/samueltardieu/orgs","repos_url":"https://api.github.com/users/samueltardieu/repos","events_url":"https://api.github.com/users/samueltardieu/events{/privacy}","received_events_url":"https://api.github.com/users/samueltardieu/received_events","type":"User","site_admin":false},"repo":{"id":54109220,"name":"akka","full_name":"samueltardieu/akka","owner":{"login":"samueltardieu","id":44656,"avatar_url":"https://avatars.githubusercontent.com/u/44656?v=3","gravatar_id":"","url":"https://api.github.com/users/samueltardieu","html_url":"https://github.com/samueltardieu","followers_url":"https://api.github.com/users/samueltardieu/followers","following_url":"https://api.github.com/users/samueltardieu/following{/other_user}","gists_url":"https://api.github.com/users/samueltardieu/gists{/gist_id}","starred_url":"https://api.github.com/users/samueltardieu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/samueltardieu/subscriptions","organizations_url":"https://api.github.com/users/samueltardieu/orgs","repos_url":"https://api.github.com/users/samueltardieu/repos","events_url":"https://api.github.com/users/samueltardieu/events{/privacy}","received_events_url":"https://api.github.com/users/samueltardieu/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/samueltardieu/akka","description":"Akka Project","fork":true,"url":"https://api.github.com/repos/samueltardieu/akka","forks_url":"https://api.github.com/repos/samueltardieu/akka/forks","keys_url":"https://api.github.com/repos/samueltardieu/akka/keys{/key_id}","collaborators_url":"https://api.github.com/repos/samueltardieu/akka/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/samueltardieu/akka/teams","hooks_url":"https://api.github.com/repos/samueltardieu/akka/hooks","issue_events_url":"https://api.github.com/repos/samueltardieu/akka/issues/events{/number}","events_url":"https://api.github.com/repos/samueltardieu/akka/events","assignees_url":"https://api.github.com/repos/samueltardieu/akka/assignees{/user}","branches_url":"https://api.github.com/repos/samueltardieu/akka/branches{/branch}","tags_url":"https://api.github.com/repos/samueltardieu/akka/tags","blobs_url":"https://api.github.com/repos/samueltardieu/akka/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/samueltardieu/akka/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/samueltardieu/akka/git/refs{/sha}","trees_url":"https://api.github.com/repos/samueltardieu/akka/git/trees{/sha}","statuses_url":"https://api.github.com/repos/samueltardieu/akka/statuses/{sha}","languages_url":"https://api.github.com/repos/samueltardieu/akka/languages","stargazers_url":"https://api.github.com/repos/samueltardieu/akka/stargazers","contributors_url":"https://api.github.com/repos/samueltardieu/akka/contributors","subscribers_url":"https://api.github.com/repos/samueltardieu/akka/subscribers","subscription_url":"https://api.github.com/repos/samueltardieu/akka/subscription","commits_url":"https://api.github.com/repos/samueltardieu/akka/commits{/sha}","git_commits_url":"https://api.github.com/repos/samueltardieu/akka/git/commits{/sha}","comments_url":"https://api.github.com/repos/samueltardieu/akka/comments{/number}","issue_comment_url":"https://api.github.com/repos/samueltardieu/akka/issues/comments{/number}","contents_url":"https://api.github.com/repos/samueltardieu/akka/contents/{+path}","compare_url":"https://api.github.com/repos/samueltardieu/akka/compare/{base}...{head}","merges_url":"https://api.github.com/repos/samueltardieu/akka/merges","archive_url":"https://api.github.com/repos/samueltardieu/akka/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/samueltardieu/akka/downloads","issues_url":"https://api.github.com/repos/samueltardieu/akka/issues{/number}","pulls_url":"https://api.github.com/repos/samueltardieu/akka/pulls{/number}","milestones_url":"https://api.github.com/repos/samueltardieu/akka/milestones{/number}","notifications_url":"https://api.github.com/repos/samueltardieu/akka/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/samueltardieu/akka/labels{/name}","releases_url":"https://api.github.com/repos/samueltardieu/akka/releases{/id}","deployments_url":"https://api.github.com/repos/samueltardieu/akka/deployments","created_at":"2016-03-17T10:27:40Z","updated_at":"2016-03-17T10:27:53Z","pushed_at":"2016-03-30T06:28:44Z","git_url":"git://github.com/samueltardieu/akka.git","ssh_url":"git@github.com:samueltardieu/akka.git","clone_url":"https://github.com/samueltardieu/akka.git","svn_url":"https://github.com/samueltardieu/akka","homepage":"http://akka.io","size":191872,"stargazers_count":0,"watchers_count":0,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"akka:master","ref":"master","sha":"cb91c60266cc1a5dd86d31b4e639fd90ab48e199","user":{"login":"akka","id":1496237,"avatar_url":"https://avatars.githubusercontent.com/u/1496237?v=3","gravatar_id":"","url":"https://api.github.com/users/akka","html_url":"https://github.com/akka","followers_url":"https://api.github.com/users/akka/followers","following_url":"https://api.github.com/users/akka/following{/other_user}","gists_url":"https://api.github.com/users/akka/gists{/gist_id}","starred_url":"https://api.github.com/users/akka/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/akka/subscriptions","organizations_url":"https://api.github.com/users/akka/orgs","repos_url":"https://api.github.com/users/akka/repos","events_url":"https://api.github.com/users/akka/events{/privacy}","received_events_url":"https://api.github.com/users/akka/received_events","type":"Organization","site_admin":false},"repo":{"id":130013,"name":"akka","full_name":"akka/akka","owner":{"login":"akka","id":1496237,"avatar_url":"https://avatars.githubusercontent.com/u/1496237?v=3","gravatar_id":"","url":"https://api.github.com/users/akka","html_url":"https://github.com/akka","followers_url":"https://api.github.com/users/akka/followers","following_url":"https://api.github.com/users/akka/following{/other_user}","gists_url":"https://api.github.com/users/akka/gists{/gist_id}","starred_url":"https://api.github.com/users/akka/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/akka/subscriptions","organizations_url":"https://api.github.com/users/akka/orgs","repos_url":"https://api.github.com/users/akka/repos","events_url":"https://api.github.com/users/akka/events{/privacy}","received_events_url":"https://api.github.com/users/akka/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/akka/akka","description":"Akka Project","fork":false,"url":"https://api.github.com/repos/akka/akka","forks_url":"https://api.github.com/repos/akka/akka/forks","keys_url":"https://api.github.com/repos/akka/akka/keys{/key_id}","collaborators_url":"https://api.github.com/repos/akka/akka/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/akka/akka/teams","hooks_url":"https://api.github.com/repos/akka/akka/hooks","issue_events_url":"https://api.github.com/repos/akka/akka/issues/events{/number}","events_url":"https://api.github.com/repos/akka/akka/events","assignees_url":"https://api.github.com/repos/akka/akka/assignees{/user}","branches_url":"https://api.github.com/repos/akka/akka/branches{/branch}","tags_url":"https://api.github.com/repos/akka/akka/tags","blobs_url":"https://api.github.com/repos/akka/akka/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/akka/akka/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/akka/akka/git/refs{/sha}","trees_url":"https://api.github.com/repos/akka/akka/git/trees{/sha}","statuses_url":"https://api.github.com/repos/akka/akka/statuses/{sha}","languages_url":"https://api.github.com/repos/akka/akka/languages","stargazers_url":"https://api.github.com/repos/akka/akka/stargazers","contributors_url":"https://api.github.com/repos/akka/akka/contributors","subscribers_url":"https://api.github.com/repos/akka/akka/subscribers","subscription_url":"https://api.github.com/repos/akka/akka/subscription","commits_url":"https://api.github.com/repos/akka/akka/commits{/sha}","git_commits_url":"https://api.github.com/repos/akka/akka/git/commits{/sha}","comments_url":"https://api.github.com/repos/akka/akka/comments{/number}","issue_comment_url":"https://api.github.com/repos/akka/akka/issues/comments{/number}","contents_url":"https://api.github.com/repos/akka/akka/contents/{+path}","compare_url":"https://api.github.com/repos/akka/akka/compare/{base}...{head}","merges_url":"https://api.github.com/repos/akka/akka/merges","archive_url":"https://api.github.com/repos/akka/akka/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/akka/akka/downloads","issues_url":"https://api.github.com/repos/akka/akka/issues{/number}","pulls_url":"https://api.github.com/repos/akka/akka/pulls{/number}","milestones_url":"https://api.github.com/repos/akka/akka/milestones{/number}","notifications_url":"https://api.github.com/repos/akka/akka/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/akka/akka/labels{/name}","releases_url":"https://api.github.com/repos/akka/akka/releases{/id}","deployments_url":"https://api.github.com/repos/akka/akka/deployments","created_at":"2009-02-16T12:51:54Z","updated_at":"2016-03-30T11:11:50Z","pushed_at":"2016-03-30T11:15:01Z","git_url":"git://github.com/akka/akka.git","ssh_url":"git@github.com:akka/akka.git","clone_url":"https://github.com/akka/akka.git","svn_url":"https://github.com/akka/akka","homepage":"http://akka.io","size":203303,"stargazers_count":5309,"watchers_count":5309,"language":"Scala","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":1650,"mirror_url":null,"open_issues_count":736,"forks":1650,"open_issues":736,"watchers":5309,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/akka/akka/pulls/20082"},"html":{"href":"https://github.com/akka/akka/pull/20082"},"issue":{"href":"https://api.github.com/repos/akka/akka/issues/20082"},"comments":{"href":"https://api.github.com/repos/akka/akka/issues/20082/comments"},"review_comments":{"href":"https://api.github.com/repos/akka/akka/pulls/20082/comments"},"review_comment":{"href":"https://api.github.com/repos/akka/akka/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/akka/akka/pulls/20082/commits"},"statuses":{"href":"https://api.github.com/repos/akka/akka/statuses/b2680c58c23cd4807acf376e0ba07047b040a159"}}}}