{"action":"created","comment":{"url":"https://api.github.com/repos/akka/akka/pulls/comments/39523323","id":39523323,"diff_hunk":"@@ -0,0 +1,222 @@\n+package akka.stream.io\n+\n+import java.util.concurrent.TimeoutException\n+\n+import akka.actor.{ Cancellable, ActorSystem }\n+import akka.stream.{ FlowShape, Outlet, Inlet, BidiShape }\n+import akka.stream.scaladsl.{ BidiFlow, Flow }\n+import akka.stream.stage.{ OutHandler, InHandler, GraphStageLogic, GraphStage }\n+\n+import scala.concurrent.duration.{ Deadline, FiniteDuration }\n+\n+/**\n+ * Various stages for controlling timeouts on IO related streams (although not necessarily).\n+ *\n+ * The common theme among the processing stages here that\n+ *  - they wait for certain event or events to happen\n+ *  - they have a timer that may fire before these events\n+ *  - if the timer fires before the event happens, these stages all fail the stream\n+ *  - otherwise, these streams do not interfere with the element flow, ordinary completion or failure\n+ */\n+object Timeouts {\n+\n+  /**\n+   * If the first element has not passed through this stage before the provided timeout, the stream is failed.\n+   */\n+  // FIXME: Where should system come from? (Actually we need a Scheduler and ExecutionContext)\n+  def initalTimeout[T](timeout: FiniteDuration, system: ActorSystem): Flow[T, T, Unit] =\n+    Flow.wrap(new InitialTimeout[T](system, timeout))\n+\n+  /**\n+   * If the completion of the stream does not happen until the provided timeout, the stream is failed.\n+   */\n+  // FIXME: Where should system come from? (Actually we need a Scheduler and ExecutionContext)\n+  def completionTimeout[T](timeout: FiniteDuration, system: ActorSystem): Flow[T, T, Unit] =\n+    Flow.wrap(new CompletionTimeout[T](system, timeout))\n+\n+  /**\n+   * If the time between two processed elements exceed the provided timeout, the stream is failed.\n+   */\n+  // FIXME: Where should system come from? (Actually we need a Scheduler and ExecutionContext)\n+  def idleTimeout[T](timeout: FiniteDuration, system: ActorSystem): Flow[T, T, Unit] =\n+    Flow.wrap(new IdleTimeout[T](system, timeout))\n+\n+  /**\n+   * If the time between two processed elements *in any direction* exceed the provided timeout, the stream is failed.\n+   *\n+   * There is a difference between this stage and having two idleTimeout Flows assembled into a BidiStage.\n+   * If the timout is configured to be 1 seconds, then this stage will not fail even though there are elements flowing\n+   * every second in one direction, but no elements are flowing in the other direction. I.e. this stage considers\n+   * the *joint* frequencies of the elements in both directions.\n+   */\n+  // FIXME: Where should system come from? (Actually we need a Scheduler and ExecutionContext)\n+  def idleTimeoutBidi[A, B](timeout: FiniteDuration, system: ActorSystem): BidiFlow[A, A, B, B, Unit] =\n+    BidiFlow.wrap(new IdleTimeoutBidi[A, B](system, timeout))\n+\n+  private abstract class LinearTimeoutStage[T](val system: ActorSystem) extends GraphStage[FlowShape[T, T]] {\n+    val in = Inlet[T](\"in\")\n+    val out = Outlet[T](\"out\")\n+    val shape = FlowShape(in, out)\n+\n+    protected abstract class LinearTimeoutStageLogic extends GraphStageLogic {\n+      protected var timerCancellable: Option[Cancellable] = None\n+\n+      setHandler(out, new OutHandler {\n+        override def onPull(): Unit = pull(in)\n+        override def onDownstreamFinish(): Unit = completeStage()\n+      })\n+\n+      protected def onTimerTick(): Unit\n+\n+      final override def postStop(): Unit = {\n+        super.postStop()\n+        timerCancellable.foreach(_.cancel())\n+      }\n+    }\n+\n+  }\n+\n+  private class InitialTimeout[T](_system: ActorSystem, timeout: FiniteDuration) extends LinearTimeoutStage[T](_system) {\n+\n+    override def createLogic: GraphStageLogic = new LinearTimeoutStageLogic {\n+      private var initialHasPassed = false\n+\n+      setHandler(in, new InHandler {\n+        override def onPush(): Unit = {\n+          initialHasPassed = true\n+          push(out, grab(in))\n+        }\n+        override def onUpstreamFinish(): Unit = completeStage()\n+        override def onUpstreamFailure(ex: Throwable): Unit = failStage(ex)\n+      })\n+\n+      final override protected def onTimerTick(): Unit =\n+        if (!initialHasPassed)\n+          failStage(new TimeoutException(s\"The first element has not yet passed through in $timeout.\"))\n+\n+      override def preStart(): Unit = {\n+        super.preStart()\n+        val callback = getAsyncCallback[Unit]((_) ⇒ onTimerTick())\n+        import system.dispatcher\n+        timerCancellable = Some(system.scheduler.scheduleOnce(timeout)(callback.invoke(())))","path":"akka-stream/src/main/scala/akka/stream/io/Timeouts.scala","position":101,"original_position":101,"commit_id":"e7169349fa030ac94019546c5e873df75225970f","original_commit_id":"e7169349fa030ac94019546c5e873df75225970f","user":{"login":"drewhk","id":932285,"avatar_url":"https://avatars.githubusercontent.com/u/932285?v=3","gravatar_id":"","url":"https://api.github.com/users/drewhk","html_url":"https://github.com/drewhk","followers_url":"https://api.github.com/users/drewhk/followers","following_url":"https://api.github.com/users/drewhk/following{/other_user}","gists_url":"https://api.github.com/users/drewhk/gists{/gist_id}","starred_url":"https://api.github.com/users/drewhk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/drewhk/subscriptions","organizations_url":"https://api.github.com/users/drewhk/orgs","repos_url":"https://api.github.com/users/drewhk/repos","events_url":"https://api.github.com/users/drewhk/events{/privacy}","received_events_url":"https://api.github.com/users/drewhk/received_events","type":"User","site_admin":false},"body":"There is no \"context.scheduler\". This is not an ActorContext.","created_at":"2015-09-15T15:18:42Z","updated_at":"2015-09-15T15:18:42Z","html_url":"https://github.com/akka/akka/pull/18467#discussion_r39523323","pull_request_url":"https://api.github.com/repos/akka/akka/pulls/18467","_links":{"self":{"href":"https://api.github.com/repos/akka/akka/pulls/comments/39523323"},"html":{"href":"https://github.com/akka/akka/pull/18467#discussion_r39523323"},"pull_request":{"href":"https://api.github.com/repos/akka/akka/pulls/18467"}}},"pull_request":{"url":"https://api.github.com/repos/akka/akka/pulls/18467","id":44927670,"html_url":"https://github.com/akka/akka/pull/18467","diff_url":"https://github.com/akka/akka/pull/18467.diff","patch_url":"https://github.com/akka/akka/pull/18467.patch","issue_url":"https://api.github.com/repos/akka/akka/issues/18467","number":18467,"state":"open","locked":false,"title":"Wip 16597 16410 timeout stages drewhk","user":{"login":"drewhk","id":932285,"avatar_url":"https://avatars.githubusercontent.com/u/932285?v=3","gravatar_id":"","url":"https://api.github.com/users/drewhk","html_url":"https://github.com/drewhk","followers_url":"https://api.github.com/users/drewhk/followers","following_url":"https://api.github.com/users/drewhk/following{/other_user}","gists_url":"https://api.github.com/users/drewhk/gists{/gist_id}","starred_url":"https://api.github.com/users/drewhk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/drewhk/subscriptions","organizations_url":"https://api.github.com/users/drewhk/orgs","repos_url":"https://api.github.com/users/drewhk/repos","events_url":"https://api.github.com/users/drewhk/events{/privacy}","received_events_url":"https://api.github.com/users/drewhk/received_events","type":"User","site_admin":false},"body":"**DO NOT MERGE YET***\r\n\r\nIntroducing TimerStage to replace TimerTransform, also implementing various timeout stages with the new GraphStage support.\r\n\r\nQuestions:\r\n - how to provide Scheduler and ExecutionContext to these stages (one version takes it from materializer, other version takes an explicit ActorSystem parameter)\r\n - TimerStage is hard to use I think, here two approaches can be compared (TimerStage vs GraphStage)\r\n\r\n(In addition fixed reusability bug in GraphStage)","created_at":"2015-09-14T13:09:44Z","updated_at":"2015-09-15T15:18:42Z","closed_at":null,"merged_at":null,"merge_commit_sha":"557f34a5186f9fd1da346223214b06e0fbe29d3a","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/akka/akka/pulls/18467/commits","review_comments_url":"https://api.github.com/repos/akka/akka/pulls/18467/comments","review_comment_url":"https://api.github.com/repos/akka/akka/pulls/comments{/number}","comments_url":"https://api.github.com/repos/akka/akka/issues/18467/comments","statuses_url":"https://api.github.com/repos/akka/akka/statuses/e7169349fa030ac94019546c5e873df75225970f","head":{"label":"drewhk:wip-16597-16410-timeout-stages-drewhk","ref":"wip-16597-16410-timeout-stages-drewhk","sha":"e7169349fa030ac94019546c5e873df75225970f","user":{"login":"drewhk","id":932285,"avatar_url":"https://avatars.githubusercontent.com/u/932285?v=3","gravatar_id":"","url":"https://api.github.com/users/drewhk","html_url":"https://github.com/drewhk","followers_url":"https://api.github.com/users/drewhk/followers","following_url":"https://api.github.com/users/drewhk/following{/other_user}","gists_url":"https://api.github.com/users/drewhk/gists{/gist_id}","starred_url":"https://api.github.com/users/drewhk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/drewhk/subscriptions","organizations_url":"https://api.github.com/users/drewhk/orgs","repos_url":"https://api.github.com/users/drewhk/repos","events_url":"https://api.github.com/users/drewhk/events{/privacy}","received_events_url":"https://api.github.com/users/drewhk/received_events","type":"User","site_admin":false},"repo":{"id":4322312,"name":"akka","full_name":"drewhk/akka","owner":{"login":"drewhk","id":932285,"avatar_url":"https://avatars.githubusercontent.com/u/932285?v=3","gravatar_id":"","url":"https://api.github.com/users/drewhk","html_url":"https://github.com/drewhk","followers_url":"https://api.github.com/users/drewhk/followers","following_url":"https://api.github.com/users/drewhk/following{/other_user}","gists_url":"https://api.github.com/users/drewhk/gists{/gist_id}","starred_url":"https://api.github.com/users/drewhk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/drewhk/subscriptions","organizations_url":"https://api.github.com/users/drewhk/orgs","repos_url":"https://api.github.com/users/drewhk/repos","events_url":"https://api.github.com/users/drewhk/events{/privacy}","received_events_url":"https://api.github.com/users/drewhk/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/drewhk/akka","description":"Akka Project","fork":true,"url":"https://api.github.com/repos/drewhk/akka","forks_url":"https://api.github.com/repos/drewhk/akka/forks","keys_url":"https://api.github.com/repos/drewhk/akka/keys{/key_id}","collaborators_url":"https://api.github.com/repos/drewhk/akka/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/drewhk/akka/teams","hooks_url":"https://api.github.com/repos/drewhk/akka/hooks","issue_events_url":"https://api.github.com/repos/drewhk/akka/issues/events{/number}","events_url":"https://api.github.com/repos/drewhk/akka/events","assignees_url":"https://api.github.com/repos/drewhk/akka/assignees{/user}","branches_url":"https://api.github.com/repos/drewhk/akka/branches{/branch}","tags_url":"https://api.github.com/repos/drewhk/akka/tags","blobs_url":"https://api.github.com/repos/drewhk/akka/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/drewhk/akka/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/drewhk/akka/git/refs{/sha}","trees_url":"https://api.github.com/repos/drewhk/akka/git/trees{/sha}","statuses_url":"https://api.github.com/repos/drewhk/akka/statuses/{sha}","languages_url":"https://api.github.com/repos/drewhk/akka/languages","stargazers_url":"https://api.github.com/repos/drewhk/akka/stargazers","contributors_url":"https://api.github.com/repos/drewhk/akka/contributors","subscribers_url":"https://api.github.com/repos/drewhk/akka/subscribers","subscription_url":"https://api.github.com/repos/drewhk/akka/subscription","commits_url":"https://api.github.com/repos/drewhk/akka/commits{/sha}","git_commits_url":"https://api.github.com/repos/drewhk/akka/git/commits{/sha}","comments_url":"https://api.github.com/repos/drewhk/akka/comments{/number}","issue_comment_url":"https://api.github.com/repos/drewhk/akka/issues/comments{/number}","contents_url":"https://api.github.com/repos/drewhk/akka/contents/{+path}","compare_url":"https://api.github.com/repos/drewhk/akka/compare/{base}...{head}","merges_url":"https://api.github.com/repos/drewhk/akka/merges","archive_url":"https://api.github.com/repos/drewhk/akka/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/drewhk/akka/downloads","issues_url":"https://api.github.com/repos/drewhk/akka/issues{/number}","pulls_url":"https://api.github.com/repos/drewhk/akka/pulls{/number}","milestones_url":"https://api.github.com/repos/drewhk/akka/milestones{/number}","notifications_url":"https://api.github.com/repos/drewhk/akka/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/drewhk/akka/labels{/name}","releases_url":"https://api.github.com/repos/drewhk/akka/releases{/id}","created_at":"2012-05-14T09:57:34Z","updated_at":"2015-02-17T12:40:08Z","pushed_at":"2015-09-15T08:59:41Z","git_url":"git://github.com/drewhk/akka.git","ssh_url":"git@github.com:drewhk/akka.git","clone_url":"https://github.com/drewhk/akka.git","svn_url":"https://github.com/drewhk/akka","homepage":"http://akka.io","size":167277,"stargazers_count":1,"watchers_count":1,"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":1,"default_branch":"master"}},"base":{"label":"akka:release-2.3-dev","ref":"release-2.3-dev","sha":"deaa2d3a0137ee1168e0c36bc2c6fe6fecbaf673","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}","created_at":"2009-02-16T12:51:54Z","updated_at":"2015-09-15T10:34:33Z","pushed_at":"2015-09-15T14:46:45Z","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":269519,"stargazers_count":4449,"watchers_count":4449,"language":"Scala","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":1314,"mirror_url":null,"open_issues_count":705,"forks":1314,"open_issues":705,"watchers":4449,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/akka/akka/pulls/18467"},"html":{"href":"https://github.com/akka/akka/pull/18467"},"issue":{"href":"https://api.github.com/repos/akka/akka/issues/18467"},"comments":{"href":"https://api.github.com/repos/akka/akka/issues/18467/comments"},"review_comments":{"href":"https://api.github.com/repos/akka/akka/pulls/18467/comments"},"review_comment":{"href":"https://api.github.com/repos/akka/akka/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/akka/akka/pulls/18467/commits"},"statuses":{"href":"https://api.github.com/repos/akka/akka/statuses/e7169349fa030ac94019546c5e873df75225970f"}}}}