2018-10-29 17:19:37 +08:00
|
|
|
/*
|
2019-01-02 18:55:26 +08:00
|
|
|
* Copyright (C) 2009-2019 Lightbend Inc. <https://www.lightbend.com>
|
2014-10-08 18:16:57 +02:00
|
|
|
*/
|
2018-03-13 23:45:55 +09:00
|
|
|
|
2014-10-08 18:16:57 +02:00
|
|
|
package akka.stream.impl.fusing
|
|
|
|
|
|
2016-09-13 16:10:49 +02:00
|
|
|
import java.util.concurrent.TimeUnit.NANOSECONDS
|
2017-03-16 21:04:07 +02:00
|
|
|
|
2018-01-14 00:21:00 +09:00
|
|
|
import akka.actor.{ ActorRef, Terminated }
|
2017-03-16 21:04:07 +02:00
|
|
|
import akka.annotation.{ DoNotInherit, InternalApi }
|
2018-02-22 08:11:31 +01:00
|
|
|
import akka.dispatch.ExecutionContexts
|
2015-04-09 12:21:12 +02:00
|
|
|
import akka.event.Logging.LogLevel
|
2015-05-27 00:27:05 +02:00
|
|
|
import akka.event.{ LogSource, Logging, LoggingAdapter }
|
2015-11-25 21:29:35 -05:00
|
|
|
import akka.stream.Attributes.{ InputBuffer, LogLevels }
|
2016-01-16 12:17:19 -05:00
|
|
|
import akka.stream.OverflowStrategies._
|
2015-09-11 15:50:17 +02:00
|
|
|
import akka.stream.impl.fusing.GraphStages.SimpleLinearGraphStage
|
2019-02-09 15:25:39 +01:00
|
|
|
import akka.stream.impl.{ ReactiveStreamsCompliance, Buffer => BufferImpl }
|
2018-03-16 19:08:29 +08:00
|
|
|
import akka.stream.scaladsl.{ Flow, Keep, Source }
|
2014-11-12 10:43:39 +01:00
|
|
|
import akka.stream.stage._
|
2015-05-27 00:27:05 +02:00
|
|
|
import akka.stream.{ Supervision, _ }
|
2019-04-05 13:06:33 +02:00
|
|
|
|
2015-04-09 22:28:16 +02:00
|
|
|
import scala.annotation.tailrec
|
2015-04-09 12:21:12 +02:00
|
|
|
import scala.collection.immutable
|
2015-09-11 15:50:17 +02:00
|
|
|
import scala.collection.immutable.VectorBuilder
|
2018-03-19 14:42:37 +01:00
|
|
|
import scala.concurrent.{ Future, Promise }
|
2017-12-05 18:51:58 +01:00
|
|
|
import scala.util.control.{ NoStackTrace, NonFatal }
|
2015-05-27 00:27:05 +02:00
|
|
|
import scala.util.{ Failure, Success, Try }
|
2018-09-19 11:06:24 +02:00
|
|
|
import akka.stream.ActorAttributes.SupervisionStrategy
|
2019-04-05 13:06:33 +02:00
|
|
|
|
2015-11-25 21:29:35 -05:00
|
|
|
import scala.concurrent.duration.{ FiniteDuration, _ }
|
2018-09-19 11:06:24 +02:00
|
|
|
import scala.util.control.Exception.Catcher
|
2016-01-18 11:29:14 +01:00
|
|
|
import akka.stream.impl.Stages.DefaultAttributes
|
2018-03-16 19:08:29 +08:00
|
|
|
import akka.util.OptionVal
|
2019-04-05 13:06:33 +02:00
|
|
|
import akka.util.unused
|
|
|
|
|
import com.github.ghik.silencer.silent
|
2014-10-08 18:16:57 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2019-02-09 15:25:39 +01:00
|
|
|
@InternalApi private[akka] final case class Map[In, Out](f: In => Out) extends GraphStage[FlowShape[In, Out]] {
|
2016-07-20 13:26:27 +02:00
|
|
|
val in = Inlet[In]("Map.in")
|
|
|
|
|
val out = Outlet[Out]("Map.out")
|
|
|
|
|
override val shape = FlowShape(in, out)
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2016-07-20 13:26:27 +02:00
|
|
|
override def initialAttributes: Attributes = DefaultAttributes.map
|
2015-02-04 09:26:32 +01:00
|
|
|
|
2016-07-20 13:26:27 +02:00
|
|
|
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
|
|
|
|
|
new GraphStageLogic(shape) with InHandler with OutHandler {
|
|
|
|
|
private def decider =
|
2017-11-22 13:51:24 +01:00
|
|
|
inheritedAttributes.mandatoryAttribute[SupervisionStrategy].decider
|
2016-07-20 13:26:27 +02:00
|
|
|
|
|
|
|
|
override def onPush(): Unit = {
|
|
|
|
|
try {
|
|
|
|
|
push(out, f(grab(in)))
|
|
|
|
|
} catch {
|
2019-03-11 10:38:24 +01:00
|
|
|
case NonFatal(ex) =>
|
|
|
|
|
decider(ex) match {
|
|
|
|
|
case Supervision.Stop => failStage(ex)
|
|
|
|
|
case _ => pull(in)
|
|
|
|
|
}
|
2016-07-20 13:26:27 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def onPull(): Unit = pull(in)
|
|
|
|
|
|
|
|
|
|
setHandlers(in, out, this)
|
|
|
|
|
}
|
2014-10-08 18:16:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2019-02-09 15:25:39 +01:00
|
|
|
@InternalApi private[akka] final case class Filter[T](p: T => Boolean) extends SimpleLinearGraphStage[T] {
|
2016-04-14 11:04:08 +02:00
|
|
|
override def initialAttributes: Attributes = DefaultAttributes.filter
|
2015-02-04 09:26:32 +01:00
|
|
|
|
2016-04-14 11:04:08 +02:00
|
|
|
override def toString: String = "Filter"
|
|
|
|
|
|
|
|
|
|
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
|
|
|
|
|
new GraphStageLogic(shape) with OutHandler with InHandler {
|
2017-11-22 13:51:24 +01:00
|
|
|
def decider = inheritedAttributes.mandatoryAttribute[SupervisionStrategy].decider
|
2016-04-14 11:04:08 +02:00
|
|
|
|
|
|
|
|
override def onPush(): Unit = {
|
|
|
|
|
try {
|
|
|
|
|
val elem = grab(in)
|
|
|
|
|
if (p(elem)) {
|
|
|
|
|
push(out, elem)
|
|
|
|
|
} else {
|
|
|
|
|
pull(in)
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
2019-03-11 10:38:24 +01:00
|
|
|
case NonFatal(ex) =>
|
|
|
|
|
decider(ex) match {
|
|
|
|
|
case Supervision.Stop => failStage(ex)
|
|
|
|
|
case _ => pull(in)
|
|
|
|
|
}
|
2016-04-14 11:04:08 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def onPull(): Unit = pull(in)
|
|
|
|
|
|
|
|
|
|
setHandlers(in, out, this)
|
|
|
|
|
}
|
2015-06-12 23:22:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-04-04 13:11:22 +02:00
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2019-03-11 10:38:24 +01:00
|
|
|
@InternalApi private[akka] final case class TakeWhile[T](p: T => Boolean, inclusive: Boolean = false)
|
|
|
|
|
extends SimpleLinearGraphStage[T] {
|
2016-02-29 13:20:00 +03:00
|
|
|
override def initialAttributes: Attributes = DefaultAttributes.takeWhile
|
2015-06-12 23:22:36 -04:00
|
|
|
|
2016-02-29 13:20:00 +03:00
|
|
|
override def toString: String = "TakeWhile"
|
2015-06-12 23:22:36 -04:00
|
|
|
|
2016-02-29 13:20:00 +03:00
|
|
|
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
|
|
|
|
|
new GraphStageLogic(shape) with OutHandler with InHandler {
|
|
|
|
|
override def toString = "TakeWhileLogic"
|
|
|
|
|
|
2017-11-22 13:51:24 +01:00
|
|
|
def decider = inheritedAttributes.mandatoryAttribute[SupervisionStrategy].decider
|
2016-02-29 13:20:00 +03:00
|
|
|
|
|
|
|
|
override def onPush(): Unit = {
|
|
|
|
|
try {
|
|
|
|
|
val elem = grab(in)
|
|
|
|
|
if (p(elem)) {
|
|
|
|
|
push(out, elem)
|
|
|
|
|
} else {
|
2016-04-20 14:47:32 -07:00
|
|
|
if (inclusive) push(out, elem)
|
2016-02-29 13:20:00 +03:00
|
|
|
completeStage()
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
2019-03-11 10:38:24 +01:00
|
|
|
case NonFatal(ex) =>
|
|
|
|
|
decider(ex) match {
|
|
|
|
|
case Supervision.Stop => failStage(ex)
|
|
|
|
|
case _ => pull(in)
|
|
|
|
|
}
|
2016-02-29 13:20:00 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def onPull(): Unit = pull(in)
|
|
|
|
|
|
|
|
|
|
setHandlers(in, out, this)
|
|
|
|
|
}
|
2015-06-12 23:22:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2019-02-09 15:25:39 +01:00
|
|
|
@InternalApi private[akka] final case class DropWhile[T](p: T => Boolean) extends SimpleLinearGraphStage[T] {
|
2016-03-09 20:46:42 -05:00
|
|
|
override def initialAttributes: Attributes = DefaultAttributes.dropWhile
|
2015-06-12 23:22:36 -04:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
def createLogic(inheritedAttributes: Attributes) =
|
|
|
|
|
new SupervisedGraphStageLogic(inheritedAttributes, shape) with InHandler with OutHandler {
|
|
|
|
|
override def onPush(): Unit = {
|
|
|
|
|
val elem = grab(in)
|
|
|
|
|
withSupervision(() => p(elem)) match {
|
|
|
|
|
case Some(flag) if flag => pull(in)
|
|
|
|
|
case Some(flag) if !flag =>
|
|
|
|
|
push(out, elem)
|
|
|
|
|
setHandler(in, rest)
|
|
|
|
|
case None => // do nothing
|
|
|
|
|
}
|
2016-03-09 20:46:42 -05:00
|
|
|
}
|
2015-06-12 23:22:36 -04:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
def rest = new InHandler {
|
|
|
|
|
def onPush() = push(out, grab(in))
|
|
|
|
|
}
|
2016-03-09 20:46:42 -05:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def onResume(t: Throwable): Unit = if (!hasBeenPulled(in)) pull(in)
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def onPull(): Unit = pull(in)
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
setHandlers(in, out, this)
|
|
|
|
|
}
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2016-03-09 20:46:42 -05:00
|
|
|
override def toString = "DropWhile"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2019-03-11 10:38:24 +01:00
|
|
|
@DoNotInherit private[akka] abstract class SupervisedGraphStageLogic(inheritedAttributes: Attributes, shape: Shape)
|
|
|
|
|
extends GraphStageLogic(shape) {
|
2017-11-22 13:51:24 +01:00
|
|
|
private lazy val decider = inheritedAttributes.mandatoryAttribute[SupervisionStrategy].decider
|
2016-08-24 21:02:32 +02:00
|
|
|
|
2019-02-09 15:25:39 +01:00
|
|
|
def withSupervision[T](f: () => T): Option[T] =
|
2016-10-17 08:02:54 +01:00
|
|
|
try {
|
|
|
|
|
Some(f())
|
|
|
|
|
} catch {
|
2019-02-09 15:25:39 +01:00
|
|
|
case NonFatal(ex) =>
|
2016-03-09 20:46:42 -05:00
|
|
|
decider(ex) match {
|
2019-02-09 15:25:39 +01:00
|
|
|
case Supervision.Stop => onStop(ex)
|
|
|
|
|
case Supervision.Resume => onResume(ex)
|
|
|
|
|
case Supervision.Restart => onRestart(ex)
|
2016-03-09 20:46:42 -05:00
|
|
|
}
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def onResume(t: Throwable): Unit
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2016-03-09 20:46:42 -05:00
|
|
|
def onStop(t: Throwable): Unit = failStage(t)
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2016-03-09 20:46:42 -05:00
|
|
|
def onRestart(t: Throwable): Unit = onResume(t)
|
2014-10-08 18:16:57 +02:00
|
|
|
}
|
|
|
|
|
|
2016-03-09 20:46:42 -05:00
|
|
|
private[stream] object Collect {
|
2014-11-09 21:09:50 +01:00
|
|
|
// Cached function that can be used with PartialFunction.applyOrElse to ensure that A) the guard is only applied once,
|
|
|
|
|
// and the caller can check the returned value with Collect.notApplied to query whether the PF was applied or not.
|
|
|
|
|
// Prior art: https://github.com/scala/scala/blob/v2.11.4/src/library/scala/collection/immutable/List.scala#L458
|
2019-02-09 15:25:39 +01:00
|
|
|
final val NotApplied: Any => Any = _ => Collect.NotApplied
|
2014-11-09 21:09:50 +01:00
|
|
|
}
|
|
|
|
|
|
2016-03-09 20:46:42 -05:00
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2019-03-11 10:38:24 +01:00
|
|
|
@InternalApi private[akka] final case class Collect[In, Out](pf: PartialFunction[In, Out])
|
|
|
|
|
extends GraphStage[FlowShape[In, Out]] {
|
2016-03-09 20:46:42 -05:00
|
|
|
val in = Inlet[In]("Collect.in")
|
|
|
|
|
val out = Outlet[Out]("Collect.out")
|
|
|
|
|
override val shape = FlowShape(in, out)
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2016-03-09 20:46:42 -05:00
|
|
|
override def initialAttributes: Attributes = DefaultAttributes.collect
|
2015-04-09 12:21:12 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
def createLogic(inheritedAttributes: Attributes) =
|
|
|
|
|
new SupervisedGraphStageLogic(inheritedAttributes, shape) with InHandler with OutHandler {
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
import Collect.NotApplied
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
val wrappedPf = () => pf.applyOrElse(grab(in), NotApplied)
|
2015-04-09 12:21:12 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def onPush(): Unit = withSupervision(wrappedPf) match {
|
|
|
|
|
case Some(result) =>
|
|
|
|
|
result match {
|
|
|
|
|
case NotApplied => pull(in)
|
|
|
|
|
case result: Out @unchecked => push(out, result)
|
|
|
|
|
}
|
|
|
|
|
case None => //do nothing
|
2016-03-09 20:46:42 -05:00
|
|
|
}
|
2015-02-04 09:26:32 +01:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def onResume(t: Throwable): Unit = if (!hasBeenPulled(in)) pull(in)
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def onPull(): Unit = pull(in)
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
setHandlers(in, out, this)
|
|
|
|
|
}
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2016-03-09 20:46:42 -05:00
|
|
|
override def toString = "Collect"
|
2014-11-09 21:09:50 +01:00
|
|
|
}
|
|
|
|
|
|
2015-06-13 14:02:37 -04:00
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2019-03-11 10:38:24 +01:00
|
|
|
@InternalApi private[akka] final case class Recover[T](pf: PartialFunction[Throwable, T])
|
|
|
|
|
extends SimpleLinearGraphStage[T] {
|
2016-11-18 17:13:15 +08:00
|
|
|
override protected def initialAttributes: Attributes = DefaultAttributes.recover
|
2016-04-18 15:20:32 +08:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
|
|
|
|
|
new GraphStageLogic(shape) with InHandler with OutHandler {
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
import Collect.NotApplied
|
2016-04-18 15:20:32 +08:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
var recovered: Option[T] = None
|
2015-06-13 14:02:37 -04:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def onPush(): Unit = {
|
|
|
|
|
push(out, grab(in))
|
2016-04-18 15:20:32 +08:00
|
|
|
}
|
2015-06-13 14:02:37 -04:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def onPull(): Unit = {
|
|
|
|
|
recovered match {
|
|
|
|
|
case Some(elem) =>
|
|
|
|
|
push(out, elem)
|
2016-04-18 15:20:32 +08:00
|
|
|
completeStage()
|
2019-03-11 10:38:24 +01:00
|
|
|
case None =>
|
|
|
|
|
pull(in)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-28 10:44:02 +02:00
|
|
|
override def onUpstreamFailure(ex: Throwable): Unit =
|
|
|
|
|
try pf.applyOrElse(ex, NotApplied) match {
|
2019-03-11 10:38:24 +01:00
|
|
|
case NotApplied => failStage(ex)
|
|
|
|
|
case result: T @unchecked => {
|
|
|
|
|
if (isAvailable(out)) {
|
|
|
|
|
push(out, result)
|
|
|
|
|
completeStage()
|
|
|
|
|
} else {
|
|
|
|
|
recovered = Some(result)
|
|
|
|
|
}
|
2016-04-18 15:20:32 +08:00
|
|
|
}
|
2019-08-28 10:44:02 +02:00
|
|
|
} catch {
|
|
|
|
|
case NonFatal(ex) => failStage(ex)
|
2016-04-18 15:20:32 +08:00
|
|
|
}
|
2015-06-13 14:02:37 -04:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
setHandlers(in, out, this)
|
|
|
|
|
}
|
2015-06-13 14:02:37 -04:00
|
|
|
}
|
|
|
|
|
|
2016-12-12 17:57:14 +01:00
|
|
|
/**
|
|
|
|
|
* Maps error with the provided function if it is defined for an error or, otherwise, passes it on unchanged.
|
|
|
|
|
*
|
2018-06-09 17:42:56 +09:00
|
|
|
* While similar to [[Recover]] this operator can be used to transform an error signal to a different one *without* logging
|
2016-12-12 17:57:14 +01:00
|
|
|
* it as an error in the process. So in that sense it is NOT exactly equivalent to `recover(t => throw t2)` since recover
|
|
|
|
|
* would log the `t2` error.
|
|
|
|
|
*/
|
2019-03-11 10:38:24 +01:00
|
|
|
@InternalApi private[akka] final case class MapError[T](f: PartialFunction[Throwable, Throwable])
|
|
|
|
|
extends SimpleLinearGraphStage[T] {
|
2016-12-12 17:57:14 +01:00
|
|
|
override def createLogic(attr: Attributes) =
|
|
|
|
|
new GraphStageLogic(shape) with InHandler with OutHandler {
|
|
|
|
|
override def onPush(): Unit = push(out, grab(in))
|
|
|
|
|
|
|
|
|
|
override def onUpstreamFailure(ex: Throwable): Unit =
|
|
|
|
|
if (f.isDefinedAt(ex)) super.onUpstreamFailure(f(ex))
|
|
|
|
|
else super.onUpstreamFailure(ex)
|
|
|
|
|
|
|
|
|
|
override def onPull(): Unit = pull(in)
|
|
|
|
|
|
|
|
|
|
setHandlers(in, out, this)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-08 18:16:57 +02:00
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2017-03-16 21:04:07 +02:00
|
|
|
@InternalApi private[akka] final case class Take[T](count: Long) extends SimpleLinearGraphStage[T] {
|
2016-02-28 23:14:29 +02:00
|
|
|
override def initialAttributes: Attributes = DefaultAttributes.take
|
2014-10-08 18:16:57 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
|
|
|
|
|
new GraphStageLogic(shape) with InHandler with OutHandler {
|
|
|
|
|
private var left: Long = count
|
2016-02-28 23:14:29 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def onPush(): Unit = {
|
|
|
|
|
if (left > 0) {
|
|
|
|
|
push(out, grab(in))
|
|
|
|
|
left -= 1
|
|
|
|
|
}
|
|
|
|
|
if (left <= 0) completeStage()
|
2016-02-28 23:14:29 +02:00
|
|
|
}
|
|
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def onPull(): Unit = {
|
|
|
|
|
if (left > 0) pull(in)
|
|
|
|
|
else completeStage()
|
|
|
|
|
}
|
2016-02-28 23:14:29 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
setHandlers(in, out, this)
|
|
|
|
|
}
|
2015-09-02 13:23:32 +02:00
|
|
|
|
2016-02-28 23:14:29 +02:00
|
|
|
override def toString: String = "Take"
|
2014-10-08 18:16:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2017-03-16 21:04:07 +02:00
|
|
|
@InternalApi private[akka] final case class Drop[T](count: Long) extends SimpleLinearGraphStage[T] {
|
2016-02-26 02:51:07 +02:00
|
|
|
override def initialAttributes: Attributes = DefaultAttributes.drop
|
2015-04-09 12:21:12 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
|
|
|
|
|
new GraphStageLogic(shape) with InHandler with OutHandler {
|
|
|
|
|
private var left: Long = count
|
2016-02-26 02:51:07 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def onPush(): Unit = {
|
|
|
|
|
if (left > 0) {
|
|
|
|
|
left -= 1
|
|
|
|
|
pull(in)
|
|
|
|
|
} else push(out, grab(in))
|
|
|
|
|
}
|
2016-02-26 02:51:07 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def onPull(): Unit = pull(in)
|
2016-02-26 02:51:07 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
setHandlers(in, out, this)
|
|
|
|
|
}
|
2016-02-26 02:51:07 +02:00
|
|
|
|
|
|
|
|
override def toString: String = "Drop"
|
2014-10-08 18:16:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2019-03-11 10:38:24 +01:00
|
|
|
@InternalApi private[akka] final case class Scan[In, Out](zero: Out, f: (Out, In) => Out)
|
|
|
|
|
extends GraphStage[FlowShape[In, Out]] {
|
2016-04-14 17:33:19 +02:00
|
|
|
override val shape = FlowShape[In, Out](Inlet("Scan.in"), Outlet("Scan.out"))
|
2014-11-09 21:09:50 +01:00
|
|
|
|
2016-04-14 17:33:19 +02:00
|
|
|
override def initialAttributes: Attributes = DefaultAttributes.scan
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2016-04-14 17:33:19 +02:00
|
|
|
override def toString: String = "Scan"
|
2014-11-09 21:09:50 +01:00
|
|
|
|
2016-04-14 17:33:19 +02:00
|
|
|
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
|
2019-02-09 15:25:39 +01:00
|
|
|
new GraphStageLogic(shape) with InHandler with OutHandler { self =>
|
2014-11-09 21:09:50 +01:00
|
|
|
|
2016-04-14 17:33:19 +02:00
|
|
|
private var aggregator = zero
|
2017-11-22 13:51:24 +01:00
|
|
|
private lazy val decider = inheritedAttributes.mandatoryAttribute[SupervisionStrategy].decider
|
2015-02-04 09:26:32 +01:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
import Supervision.{ Restart, Resume, Stop }
|
2016-04-14 17:33:19 +02:00
|
|
|
import shape.{ in, out }
|
|
|
|
|
|
|
|
|
|
// Initial behavior makes sure that the zero gets flushed if upstream is empty
|
|
|
|
|
setHandler(out, new OutHandler {
|
|
|
|
|
override def onPull(): Unit = {
|
|
|
|
|
push(out, aggregator)
|
|
|
|
|
setHandlers(in, out, self)
|
|
|
|
|
}
|
|
|
|
|
})
|
2016-07-05 00:00:52 -04:00
|
|
|
|
2019-03-13 10:56:20 +01:00
|
|
|
setHandler(
|
|
|
|
|
in,
|
|
|
|
|
new InHandler {
|
|
|
|
|
override def onPush(): Unit = ()
|
|
|
|
|
|
|
|
|
|
override def onUpstreamFinish(): Unit =
|
|
|
|
|
setHandler(out, new OutHandler {
|
|
|
|
|
override def onPull(): Unit = {
|
|
|
|
|
push(out, aggregator)
|
|
|
|
|
completeStage()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|
2015-02-04 09:26:32 +01:00
|
|
|
|
2016-04-14 17:33:19 +02:00
|
|
|
override def onPull(): Unit = pull(in)
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2016-04-14 17:33:19 +02:00
|
|
|
override def onPush(): Unit = {
|
|
|
|
|
try {
|
|
|
|
|
aggregator = f(aggregator, grab(in))
|
|
|
|
|
push(out, aggregator)
|
|
|
|
|
} catch {
|
2019-03-11 10:38:24 +01:00
|
|
|
case NonFatal(ex) =>
|
|
|
|
|
decider(ex) match {
|
|
|
|
|
case Resume => if (!hasBeenPulled(in)) pull(in)
|
|
|
|
|
case Stop => failStage(ex)
|
|
|
|
|
case Restart =>
|
|
|
|
|
aggregator = zero
|
|
|
|
|
push(out, aggregator)
|
|
|
|
|
}
|
2016-04-14 17:33:19 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-11-09 21:09:50 +01:00
|
|
|
}
|
|
|
|
|
|
2016-10-17 12:43:11 -02:00
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2019-03-11 10:38:24 +01:00
|
|
|
@InternalApi private[akka] final case class ScanAsync[In, Out](zero: Out, f: (Out, In) => Future[Out])
|
|
|
|
|
extends GraphStage[FlowShape[In, Out]] {
|
2016-10-17 12:43:11 -02:00
|
|
|
|
|
|
|
|
import akka.dispatch.ExecutionContexts
|
|
|
|
|
|
|
|
|
|
val in = Inlet[In]("ScanAsync.in")
|
|
|
|
|
val out = Outlet[Out]("ScanAsync.out")
|
|
|
|
|
override val shape: FlowShape[In, Out] = FlowShape[In, Out](in, out)
|
|
|
|
|
|
|
|
|
|
override val initialAttributes: Attributes = Attributes.name("scanAsync")
|
|
|
|
|
|
|
|
|
|
override val toString: String = "ScanAsync"
|
|
|
|
|
|
|
|
|
|
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
|
2019-02-09 15:25:39 +01:00
|
|
|
new GraphStageLogic(shape) with InHandler with OutHandler { self =>
|
2016-10-17 12:43:11 -02:00
|
|
|
|
|
|
|
|
private var current: Out = zero
|
2018-05-30 12:01:18 +02:00
|
|
|
private var elementHandled: Boolean = false
|
2016-10-17 12:43:11 -02:00
|
|
|
|
|
|
|
|
private def ec = ExecutionContexts.sameThreadExecutionContext
|
|
|
|
|
|
2017-11-22 13:51:24 +01:00
|
|
|
private lazy val decider = inheritedAttributes.mandatoryAttribute[SupervisionStrategy].decider
|
2016-10-17 12:43:11 -02:00
|
|
|
|
|
|
|
|
private val ZeroHandler: OutHandler with InHandler = new OutHandler with InHandler {
|
2017-12-05 09:08:33 +01:00
|
|
|
override def onPush(): Unit =
|
|
|
|
|
throw new IllegalStateException("No push should happen before zero value has been consumed")
|
2016-10-17 12:43:11 -02:00
|
|
|
|
|
|
|
|
override def onPull(): Unit = {
|
2018-05-30 12:01:18 +02:00
|
|
|
elementHandled = true
|
2016-10-17 12:43:11 -02:00
|
|
|
push(out, current)
|
|
|
|
|
setHandlers(in, out, self)
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def onUpstreamFinish(): Unit =
|
|
|
|
|
setHandler(out, new OutHandler {
|
|
|
|
|
override def onPull(): Unit = {
|
|
|
|
|
push(out, current)
|
|
|
|
|
completeStage()
|
|
|
|
|
}
|
|
|
|
|
})
|
2016-10-17 12:43:11 -02:00
|
|
|
}
|
|
|
|
|
|
2019-04-05 13:06:33 +02:00
|
|
|
private def onRestart(): Unit = {
|
2016-10-17 12:43:11 -02:00
|
|
|
current = zero
|
2018-05-30 12:01:18 +02:00
|
|
|
elementHandled = false
|
2016-10-17 12:43:11 -02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private def safePull(): Unit = {
|
2018-05-30 12:01:18 +02:00
|
|
|
if (isClosed(in)) {
|
|
|
|
|
completeStage()
|
|
|
|
|
} else if (isAvailable(out)) {
|
|
|
|
|
if (!hasBeenPulled(in)) {
|
|
|
|
|
tryPull(in)
|
|
|
|
|
}
|
2016-10-17 12:43:11 -02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private def pushAndPullOrFinish(update: Out): Unit = {
|
|
|
|
|
push(out, update)
|
2018-05-30 12:01:18 +02:00
|
|
|
safePull()
|
2016-10-17 12:43:11 -02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private def doSupervision(t: Throwable): Unit = {
|
|
|
|
|
decider(t) match {
|
2019-02-09 15:25:39 +01:00
|
|
|
case Supervision.Stop => failStage(t)
|
|
|
|
|
case Supervision.Resume => safePull()
|
|
|
|
|
case Supervision.Restart =>
|
2019-04-05 13:06:33 +02:00
|
|
|
onRestart()
|
2016-10-17 12:43:11 -02:00
|
|
|
safePull()
|
|
|
|
|
}
|
2018-05-30 12:01:18 +02:00
|
|
|
elementHandled = true
|
2016-10-17 12:43:11 -02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private val futureCB = getAsyncCallback[Try[Out]] {
|
2019-02-09 15:25:39 +01:00
|
|
|
case Success(next) if next != null =>
|
2016-10-17 12:43:11 -02:00
|
|
|
current = next
|
|
|
|
|
pushAndPullOrFinish(next)
|
2018-05-30 12:01:18 +02:00
|
|
|
elementHandled = true
|
2019-02-09 15:25:39 +01:00
|
|
|
case Success(null) => doSupervision(ReactiveStreamsCompliance.elementMustNotBeNullException)
|
|
|
|
|
case Failure(t) => doSupervision(t)
|
2016-10-17 12:43:11 -02:00
|
|
|
}.invoke _
|
|
|
|
|
|
|
|
|
|
setHandlers(in, out, ZeroHandler)
|
|
|
|
|
|
|
|
|
|
def onPull(): Unit = safePull()
|
|
|
|
|
|
|
|
|
|
def onPush(): Unit = {
|
|
|
|
|
try {
|
2018-05-30 12:01:18 +02:00
|
|
|
elementHandled = false
|
|
|
|
|
|
|
|
|
|
val eventualCurrent = f(current, grab(in))
|
2016-10-17 12:43:11 -02:00
|
|
|
|
|
|
|
|
eventualCurrent.value match {
|
2019-02-09 15:25:39 +01:00
|
|
|
case Some(result) => futureCB(result)
|
|
|
|
|
case _ => eventualCurrent.onComplete(futureCB)(ec)
|
2016-10-17 12:43:11 -02:00
|
|
|
}
|
|
|
|
|
} catch {
|
2019-02-09 15:25:39 +01:00
|
|
|
case NonFatal(ex) =>
|
2016-10-17 12:43:11 -02:00
|
|
|
decider(ex) match {
|
2019-02-09 15:25:39 +01:00
|
|
|
case Supervision.Stop => failStage(ex)
|
2019-04-05 13:06:33 +02:00
|
|
|
case Supervision.Restart => onRestart()
|
2019-02-09 15:25:39 +01:00
|
|
|
case Supervision.Resume => ()
|
2016-10-17 12:43:11 -02:00
|
|
|
}
|
|
|
|
|
tryPull(in)
|
2018-05-30 12:01:18 +02:00
|
|
|
elementHandled = true
|
2016-10-17 12:43:11 -02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-05 09:08:33 +01:00
|
|
|
override def onUpstreamFinish(): Unit = {
|
2018-05-30 12:01:18 +02:00
|
|
|
if (elementHandled) {
|
|
|
|
|
completeStage()
|
2017-12-05 09:08:33 +01:00
|
|
|
}
|
|
|
|
|
}
|
2016-10-17 12:43:11 -02:00
|
|
|
|
2018-05-30 12:01:18 +02:00
|
|
|
override val toString: String = s"ScanAsync.Logic(completed=$elementHandled)"
|
2016-10-17 12:43:11 -02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-09 21:09:50 +01:00
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2019-03-11 10:38:24 +01:00
|
|
|
@InternalApi private[akka] final case class Fold[In, Out](zero: Out, f: (Out, In) => Out)
|
|
|
|
|
extends GraphStage[FlowShape[In, Out]] {
|
2014-10-08 18:16:57 +02:00
|
|
|
|
2016-07-08 14:22:18 +02:00
|
|
|
val in = Inlet[In]("Fold.in")
|
|
|
|
|
val out = Outlet[Out]("Fold.out")
|
|
|
|
|
override val shape: FlowShape[In, Out] = FlowShape(in, out)
|
2014-10-08 18:16:57 +02:00
|
|
|
|
2016-08-24 21:02:32 +02:00
|
|
|
override def toString: String = "Fold"
|
|
|
|
|
|
2016-07-08 14:22:18 +02:00
|
|
|
override val initialAttributes = DefaultAttributes.fold
|
2014-10-08 18:16:57 +02:00
|
|
|
|
2016-07-08 14:22:18 +02:00
|
|
|
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
|
2016-07-20 13:39:23 +02:00
|
|
|
new GraphStageLogic(shape) with InHandler with OutHandler {
|
2016-07-08 14:22:18 +02:00
|
|
|
private var aggregator: Out = zero
|
2015-02-04 09:26:32 +01:00
|
|
|
|
2016-07-20 13:39:23 +02:00
|
|
|
private def decider =
|
2017-11-22 13:51:24 +01:00
|
|
|
inheritedAttributes.mandatoryAttribute[SupervisionStrategy].decider
|
2016-07-08 14:22:18 +02:00
|
|
|
|
2016-07-20 13:39:23 +02:00
|
|
|
override def onPush(): Unit = {
|
2016-11-04 16:13:11 +00:00
|
|
|
val elem = grab(in)
|
2016-07-20 13:39:23 +02:00
|
|
|
try {
|
2016-11-04 16:13:11 +00:00
|
|
|
aggregator = f(aggregator, elem)
|
2016-07-20 13:39:23 +02:00
|
|
|
} catch {
|
2019-03-11 10:38:24 +01:00
|
|
|
case NonFatal(ex) =>
|
|
|
|
|
decider(ex) match {
|
|
|
|
|
case Supervision.Stop => failStage(ex)
|
|
|
|
|
case Supervision.Restart => aggregator = zero
|
|
|
|
|
case _ => ()
|
|
|
|
|
}
|
2016-11-04 16:13:11 +00:00
|
|
|
} finally {
|
|
|
|
|
if (!isClosed(in)) pull(in)
|
2016-07-08 14:22:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def onPull(): Unit = {
|
|
|
|
|
if (isClosed(in)) {
|
|
|
|
|
push(out, aggregator)
|
|
|
|
|
completeStage()
|
|
|
|
|
} else {
|
|
|
|
|
pull(in)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def onUpstreamFinish(): Unit = {
|
|
|
|
|
if (isAvailable(out)) {
|
|
|
|
|
push(out, aggregator)
|
|
|
|
|
completeStage()
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-02-04 09:26:32 +01:00
|
|
|
|
2016-07-08 14:22:18 +02:00
|
|
|
setHandlers(in, out, this)
|
|
|
|
|
}
|
2014-10-08 18:16:57 +02:00
|
|
|
}
|
|
|
|
|
|
2016-08-24 21:02:32 +02:00
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2019-03-11 10:38:24 +01:00
|
|
|
@InternalApi private[akka] final class FoldAsync[In, Out](zero: Out, f: (Out, In) => Future[Out])
|
|
|
|
|
extends GraphStage[FlowShape[In, Out]] {
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2016-08-24 21:02:32 +02:00
|
|
|
import akka.dispatch.ExecutionContexts
|
|
|
|
|
|
|
|
|
|
val in = Inlet[In]("FoldAsync.in")
|
|
|
|
|
val out = Outlet[Out]("FoldAsync.out")
|
|
|
|
|
val shape = FlowShape.of(in, out)
|
|
|
|
|
|
|
|
|
|
override def toString: String = "FoldAsync"
|
|
|
|
|
|
|
|
|
|
override val initialAttributes = DefaultAttributes.foldAsync
|
|
|
|
|
|
2016-10-17 08:02:54 +01:00
|
|
|
def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
|
|
|
|
|
new GraphStageLogic(shape) with InHandler with OutHandler {
|
2017-11-22 13:51:24 +01:00
|
|
|
val decider = inheritedAttributes.mandatoryAttribute[SupervisionStrategy].decider
|
2016-08-24 21:02:32 +02:00
|
|
|
|
2016-10-17 08:02:54 +01:00
|
|
|
private var aggregator: Out = zero
|
|
|
|
|
private var aggregating: Future[Out] = Future.successful(aggregator)
|
2016-08-24 21:02:32 +02:00
|
|
|
|
2019-04-05 13:06:33 +02:00
|
|
|
private def onRestart(@unused t: Throwable): Unit = {
|
2016-10-17 08:02:54 +01:00
|
|
|
aggregator = zero
|
|
|
|
|
}
|
2016-08-24 21:02:32 +02:00
|
|
|
|
2016-10-17 08:02:54 +01:00
|
|
|
private def ec = ExecutionContexts.sameThreadExecutionContext
|
2016-08-24 21:02:32 +02:00
|
|
|
|
2016-10-17 08:02:54 +01:00
|
|
|
private val futureCB = getAsyncCallback[Try[Out]] {
|
2019-02-09 15:25:39 +01:00
|
|
|
case Success(update) if update != null =>
|
2016-08-24 21:02:32 +02:00
|
|
|
aggregator = update
|
|
|
|
|
|
|
|
|
|
if (isClosed(in)) {
|
|
|
|
|
push(out, update)
|
|
|
|
|
completeStage()
|
|
|
|
|
} else if (isAvailable(out) && !hasBeenPulled(in)) tryPull(in)
|
|
|
|
|
|
2019-02-09 15:25:39 +01:00
|
|
|
case other =>
|
2016-08-24 21:02:32 +02:00
|
|
|
val ex = other match {
|
2019-02-09 15:25:39 +01:00
|
|
|
case Failure(t) => t
|
|
|
|
|
case Success(s) if s == null =>
|
2016-08-24 21:02:32 +02:00
|
|
|
ReactiveStreamsCompliance.elementMustNotBeNullException
|
|
|
|
|
}
|
|
|
|
|
val supervision = decider(ex)
|
|
|
|
|
|
|
|
|
|
if (supervision == Supervision.Stop) failStage(ex)
|
|
|
|
|
else {
|
|
|
|
|
if (supervision == Supervision.Restart) onRestart(ex)
|
|
|
|
|
|
|
|
|
|
if (isClosed(in)) {
|
|
|
|
|
push(out, aggregator)
|
|
|
|
|
completeStage()
|
|
|
|
|
} else if (isAvailable(out) && !hasBeenPulled(in)) tryPull(in)
|
|
|
|
|
}
|
2016-10-17 08:02:54 +01:00
|
|
|
}.invoke _
|
|
|
|
|
|
|
|
|
|
def onPush(): Unit = {
|
|
|
|
|
try {
|
|
|
|
|
aggregating = f(aggregator, grab(in))
|
|
|
|
|
handleAggregatingValue()
|
|
|
|
|
} catch {
|
2019-03-11 10:38:24 +01:00
|
|
|
case NonFatal(ex) =>
|
|
|
|
|
decider(ex) match {
|
|
|
|
|
case Supervision.Stop => failStage(ex)
|
|
|
|
|
case supervision => {
|
|
|
|
|
supervision match {
|
|
|
|
|
case Supervision.Restart => onRestart(ex)
|
|
|
|
|
case _ => () // just ignore on Resume
|
|
|
|
|
}
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
tryPull(in)
|
|
|
|
|
}
|
2016-10-17 08:02:54 +01:00
|
|
|
}
|
2016-08-24 21:02:32 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-17 08:02:54 +01:00
|
|
|
override def onUpstreamFinish(): Unit = {
|
|
|
|
|
handleAggregatingValue()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def onPull(): Unit = if (!hasBeenPulled(in)) tryPull(in)
|
2016-08-24 21:02:32 +02:00
|
|
|
|
2016-10-17 08:02:54 +01:00
|
|
|
private def handleAggregatingValue(): Unit = {
|
2016-08-24 21:02:32 +02:00
|
|
|
aggregating.value match {
|
2019-02-09 15:25:39 +01:00
|
|
|
case Some(result) => futureCB(result) // already completed
|
|
|
|
|
case _ => aggregating.onComplete(futureCB)(ec)
|
2016-08-24 21:02:32 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-17 08:02:54 +01:00
|
|
|
setHandlers(in, out, this)
|
2016-08-24 21:02:32 +02:00
|
|
|
|
2016-10-17 08:02:54 +01:00
|
|
|
override def toString =
|
|
|
|
|
s"FoldAsync.Logic(completed=${aggregating.isCompleted})"
|
|
|
|
|
}
|
2016-08-24 21:02:32 +02:00
|
|
|
}
|
|
|
|
|
|
2015-10-16 01:55:20 +02:00
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2019-03-11 10:38:24 +01:00
|
|
|
@InternalApi private[akka] final case class Intersperse[T](start: Option[T], inject: T, end: Option[T])
|
|
|
|
|
extends SimpleLinearGraphStage[T] {
|
2016-01-18 17:49:32 +01:00
|
|
|
ReactiveStreamsCompliance.requireNonNullElement(inject)
|
2016-01-20 00:16:53 +01:00
|
|
|
if (start.isDefined) ReactiveStreamsCompliance.requireNonNullElement(start.get)
|
|
|
|
|
if (end.isDefined) ReactiveStreamsCompliance.requireNonNullElement(end.get)
|
2015-12-03 00:04:00 +08:00
|
|
|
|
2016-08-29 14:00:48 +02:00
|
|
|
override def createLogic(attr: Attributes): GraphStageLogic = new GraphStageLogic(shape) with OutHandler {
|
2015-12-07 22:48:57 +08:00
|
|
|
val startInHandler = new InHandler {
|
2015-12-06 15:02:35 +08:00
|
|
|
override def onPush(): Unit = {
|
2015-12-07 22:48:57 +08:00
|
|
|
// if else (to avoid using Iterator[T].flatten in hot code)
|
2015-12-09 01:26:42 +08:00
|
|
|
if (start.isDefined) emitMultiple(out, Iterator(start.get, grab(in)))
|
2015-12-07 22:48:57 +08:00
|
|
|
else emit(out, grab(in))
|
|
|
|
|
setHandler(in, restInHandler) // switch handler
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def onUpstreamFinish(): Unit = {
|
2015-12-09 01:26:42 +08:00
|
|
|
emitMultiple(out, Iterator(start, end).flatten)
|
2015-12-07 22:48:57 +08:00
|
|
|
completeStage()
|
2015-12-06 15:02:35 +08:00
|
|
|
}
|
2015-10-16 01:55:20 +02:00
|
|
|
}
|
|
|
|
|
|
2015-12-07 22:48:57 +08:00
|
|
|
val restInHandler = new InHandler {
|
2015-12-09 01:26:42 +08:00
|
|
|
override def onPush(): Unit = emitMultiple(out, Iterator(inject, grab(in)))
|
2015-10-16 01:55:20 +02:00
|
|
|
|
2015-12-06 15:02:35 +08:00
|
|
|
override def onUpstreamFinish(): Unit = {
|
2015-12-07 22:48:57 +08:00
|
|
|
if (end.isDefined) emit(out, end.get)
|
2015-12-06 15:02:35 +08:00
|
|
|
completeStage()
|
2015-12-03 00:04:00 +08:00
|
|
|
}
|
2015-10-16 01:55:20 +02:00
|
|
|
}
|
|
|
|
|
|
2016-08-29 14:00:48 +02:00
|
|
|
def onPull(): Unit = pull(in)
|
2015-12-07 22:48:57 +08:00
|
|
|
|
|
|
|
|
setHandler(in, startInHandler)
|
2016-08-29 14:00:48 +02:00
|
|
|
setHandler(out, this)
|
2015-10-16 01:55:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-08 18:16:57 +02:00
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2017-03-16 21:04:07 +02:00
|
|
|
@InternalApi private[akka] final case class Grouped[T](n: Int) extends GraphStage[FlowShape[T, immutable.Seq[T]]] {
|
2016-07-08 14:22:18 +02:00
|
|
|
require(n > 0, "n must be greater than 0")
|
2014-10-08 18:16:57 +02:00
|
|
|
|
2016-07-08 14:22:18 +02:00
|
|
|
val in = Inlet[T]("Grouped.in")
|
|
|
|
|
val out = Outlet[immutable.Seq[T]]("Grouped.out")
|
|
|
|
|
override val shape: FlowShape[T, immutable.Seq[T]] = FlowShape(in, out)
|
|
|
|
|
|
|
|
|
|
override protected val initialAttributes: Attributes = DefaultAttributes.grouped
|
|
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
|
|
|
|
|
new GraphStageLogic(shape) with InHandler with OutHandler {
|
|
|
|
|
private val buf = {
|
|
|
|
|
val b = Vector.newBuilder[T]
|
|
|
|
|
b.sizeHint(n)
|
|
|
|
|
b
|
|
|
|
|
}
|
|
|
|
|
var left = n
|
2016-07-08 14:22:18 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def onPush(): Unit = {
|
|
|
|
|
buf += grab(in)
|
|
|
|
|
left -= 1
|
|
|
|
|
if (left == 0) {
|
|
|
|
|
val elements = buf.result()
|
|
|
|
|
buf.clear()
|
|
|
|
|
left = n
|
|
|
|
|
push(out, elements)
|
|
|
|
|
} else {
|
|
|
|
|
pull(in)
|
|
|
|
|
}
|
2016-07-08 14:22:18 +02:00
|
|
|
}
|
|
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def onPull(): Unit = {
|
|
|
|
|
pull(in)
|
|
|
|
|
}
|
2016-07-08 14:22:18 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def onUpstreamFinish(): Unit = {
|
|
|
|
|
// This means the buf is filled with some elements but not enough (left < n) to group together.
|
|
|
|
|
// Since the upstream has finished we have to push them to downstream though.
|
|
|
|
|
if (left < n) {
|
|
|
|
|
val elements = buf.result()
|
|
|
|
|
buf.clear()
|
|
|
|
|
left = n
|
|
|
|
|
push(out, elements)
|
|
|
|
|
}
|
|
|
|
|
completeStage()
|
2016-07-08 14:22:18 +02:00
|
|
|
}
|
|
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
setHandlers(in, out, this)
|
|
|
|
|
}
|
2014-10-08 18:16:57 +02:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-19 00:11:07 +08:00
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2019-03-11 10:38:24 +01:00
|
|
|
@InternalApi private[akka] final case class LimitWeighted[T](val n: Long, val costFn: T => Long)
|
|
|
|
|
extends SimpleLinearGraphStage[T] {
|
2016-03-09 20:46:42 -05:00
|
|
|
override def initialAttributes: Attributes = DefaultAttributes.limitWeighted
|
2015-11-19 00:11:07 +08:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
def createLogic(inheritedAttributes: Attributes) =
|
|
|
|
|
new SupervisedGraphStageLogic(inheritedAttributes, shape) with InHandler with OutHandler {
|
|
|
|
|
private var left = n
|
2015-11-19 00:11:07 +08:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def onPush(): Unit = {
|
|
|
|
|
val elem = grab(in)
|
|
|
|
|
withSupervision(() => costFn(elem)) match {
|
|
|
|
|
case Some(weight) =>
|
|
|
|
|
left -= weight
|
|
|
|
|
if (left >= 0) push(out, elem) else failStage(new StreamLimitReachedException(n))
|
|
|
|
|
case None => //do nothing
|
|
|
|
|
}
|
2016-03-09 20:46:42 -05:00
|
|
|
}
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def onResume(t: Throwable): Unit = if (!hasBeenPulled(in)) pull(in)
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def onRestart(t: Throwable): Unit = {
|
|
|
|
|
left = n
|
|
|
|
|
if (!hasBeenPulled(in)) pull(in)
|
|
|
|
|
}
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def onPull(): Unit = pull(in)
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
setHandlers(in, out, this)
|
|
|
|
|
}
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2016-03-09 20:46:42 -05:00
|
|
|
override def toString = "LimitWeighted"
|
2015-11-19 00:11:07 +08:00
|
|
|
}
|
|
|
|
|
|
2015-07-27 11:39:54 +02:00
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2019-03-11 10:38:24 +01:00
|
|
|
@InternalApi private[akka] final case class Sliding[T](val n: Int, val step: Int)
|
|
|
|
|
extends GraphStage[FlowShape[T, immutable.Seq[T]]] {
|
2016-07-08 14:22:18 +02:00
|
|
|
require(n > 0, "n must be greater than 0")
|
|
|
|
|
require(step > 0, "step must be greater than 0")
|
|
|
|
|
|
|
|
|
|
val in = Inlet[T]("Sliding.in")
|
|
|
|
|
val out = Outlet[immutable.Seq[T]]("Sliding.out")
|
|
|
|
|
override val shape: FlowShape[T, immutable.Seq[T]] = FlowShape(in, out)
|
|
|
|
|
|
|
|
|
|
override protected val initialAttributes: Attributes = DefaultAttributes.sliding
|
|
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
|
|
|
|
|
new GraphStageLogic(shape) with InHandler with OutHandler {
|
|
|
|
|
private var buf = Vector.empty[T]
|
2016-07-08 14:22:18 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def onPush(): Unit = {
|
|
|
|
|
buf :+= grab(in)
|
|
|
|
|
if (buf.size < n) {
|
|
|
|
|
pull(in)
|
|
|
|
|
} else if (buf.size == n) {
|
2016-07-08 14:22:18 +02:00
|
|
|
push(out, buf)
|
2019-03-11 10:38:24 +01:00
|
|
|
} else if (step <= n) {
|
2016-07-08 14:22:18 +02:00
|
|
|
buf = buf.drop(step)
|
2019-03-11 10:38:24 +01:00
|
|
|
if (buf.size == n) {
|
|
|
|
|
push(out, buf)
|
|
|
|
|
} else pull(in)
|
|
|
|
|
} else if (step > n) {
|
|
|
|
|
if (buf.size == step) {
|
|
|
|
|
buf = buf.drop(step)
|
|
|
|
|
}
|
|
|
|
|
pull(in)
|
2016-07-08 14:22:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
2015-07-27 11:39:54 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def onPull(): Unit = {
|
|
|
|
|
pull(in)
|
|
|
|
|
}
|
2016-07-08 14:22:18 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def onUpstreamFinish(): Unit = {
|
2016-07-08 14:22:18 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
// We can finish current stage directly if:
|
|
|
|
|
// 1. the buf is empty or
|
|
|
|
|
// 2. when the step size is greater than the sliding size (step > n) and current stage is in between
|
|
|
|
|
// two sliding (ie. buf.size >= n && buf.size < step).
|
|
|
|
|
//
|
|
|
|
|
// Otherwise it means there is still a not finished sliding so we have to push them before finish current stage.
|
|
|
|
|
if (buf.size < n && buf.size > 0) {
|
|
|
|
|
push(out, buf)
|
|
|
|
|
}
|
|
|
|
|
completeStage()
|
2016-07-08 14:22:18 +02:00
|
|
|
}
|
|
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
this.setHandlers(in, out, this)
|
|
|
|
|
}
|
2015-07-27 11:39:54 +02:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-08 18:16:57 +02:00
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2019-03-11 10:38:24 +01:00
|
|
|
@InternalApi private[akka] final case class Buffer[T](size: Int, overflowStrategy: OverflowStrategy)
|
|
|
|
|
extends SimpleLinearGraphStage[T] {
|
2015-04-09 12:21:12 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
|
|
|
|
|
new GraphStageLogic(shape) with InHandler with OutHandler with StageLogging {
|
|
|
|
|
override protected def logSource: Class[_] = classOf[Buffer[_]]
|
|
|
|
|
|
2019-09-04 13:37:06 +02:00
|
|
|
private val buffer: BufferImpl[T] = BufferImpl(size, inheritedAttributes)
|
2019-03-11 10:38:24 +01:00
|
|
|
|
|
|
|
|
val enqueueAction: T => Unit =
|
|
|
|
|
overflowStrategy match {
|
|
|
|
|
case s: DropHead =>
|
|
|
|
|
elem =>
|
|
|
|
|
if (buffer.isFull) {
|
2019-03-13 10:56:20 +01:00
|
|
|
log.log(
|
|
|
|
|
s.logLevel,
|
|
|
|
|
"Dropping the head element because buffer is full and overflowStrategy is: [DropHead]")
|
2019-03-11 10:38:24 +01:00
|
|
|
buffer.dropHead()
|
|
|
|
|
}
|
|
|
|
|
buffer.enqueue(elem)
|
|
|
|
|
pull(in)
|
|
|
|
|
case s: DropTail =>
|
|
|
|
|
elem =>
|
|
|
|
|
if (buffer.isFull) {
|
2019-03-13 10:56:20 +01:00
|
|
|
log.log(
|
|
|
|
|
s.logLevel,
|
|
|
|
|
"Dropping the tail element because buffer is full and overflowStrategy is: [DropTail]")
|
2019-03-11 10:38:24 +01:00
|
|
|
buffer.dropTail()
|
|
|
|
|
}
|
|
|
|
|
buffer.enqueue(elem)
|
|
|
|
|
pull(in)
|
|
|
|
|
case s: DropBuffer =>
|
|
|
|
|
elem =>
|
|
|
|
|
if (buffer.isFull) {
|
|
|
|
|
log.log(
|
|
|
|
|
s.logLevel,
|
|
|
|
|
"Dropping all the buffered elements because buffer is full and overflowStrategy is: [DropBuffer]")
|
|
|
|
|
buffer.clear()
|
|
|
|
|
}
|
|
|
|
|
buffer.enqueue(elem)
|
|
|
|
|
pull(in)
|
|
|
|
|
case s: DropNew =>
|
|
|
|
|
elem =>
|
|
|
|
|
if (!buffer.isFull) buffer.enqueue(elem)
|
|
|
|
|
else
|
2019-03-13 10:56:20 +01:00
|
|
|
log.log(
|
|
|
|
|
s.logLevel,
|
|
|
|
|
"Dropping the new element because buffer is full and overflowStrategy is: [DropNew]")
|
2019-03-11 10:38:24 +01:00
|
|
|
pull(in)
|
|
|
|
|
case s: Backpressure =>
|
|
|
|
|
elem =>
|
|
|
|
|
buffer.enqueue(elem)
|
|
|
|
|
if (!buffer.isFull) pull(in)
|
|
|
|
|
else log.log(s.logLevel, "Backpressuring because buffer is full and overflowStrategy is: [Backpressure]")
|
|
|
|
|
case s: Fail =>
|
|
|
|
|
elem =>
|
|
|
|
|
if (buffer.isFull) {
|
|
|
|
|
log.log(s.logLevel, "Failing because buffer is full and overflowStrategy is: [Fail]")
|
|
|
|
|
failStage(BufferOverflowException(s"Buffer overflow (max capacity was: $size)!"))
|
|
|
|
|
} else {
|
|
|
|
|
buffer.enqueue(elem)
|
|
|
|
|
pull(in)
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-07 14:54:48 +01:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def preStart(): Unit = {
|
|
|
|
|
pull(in)
|
|
|
|
|
}
|
2014-10-08 18:16:57 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def onPush(): Unit = {
|
|
|
|
|
val elem = grab(in)
|
|
|
|
|
// If out is available, then it has been pulled but no dequeued element has been delivered.
|
|
|
|
|
// It means the buffer at this moment is definitely empty,
|
|
|
|
|
// so we just push the current element to out, then pull.
|
|
|
|
|
if (isAvailable(out)) {
|
|
|
|
|
push(out, elem)
|
2016-11-16 01:48:33 +08:00
|
|
|
pull(in)
|
2019-03-11 10:38:24 +01:00
|
|
|
} else {
|
|
|
|
|
enqueueAction(elem)
|
|
|
|
|
}
|
2016-11-16 01:48:33 +08:00
|
|
|
}
|
|
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def onPull(): Unit = {
|
|
|
|
|
if (buffer.nonEmpty) push(out, buffer.dequeue())
|
|
|
|
|
if (isClosed(in)) {
|
|
|
|
|
if (buffer.isEmpty) completeStage()
|
|
|
|
|
} else if (!hasBeenPulled(in)) {
|
|
|
|
|
pull(in)
|
|
|
|
|
}
|
2016-11-16 01:48:33 +08:00
|
|
|
}
|
|
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def onUpstreamFinish(): Unit = {
|
2016-11-16 01:48:33 +08:00
|
|
|
if (buffer.isEmpty) completeStage()
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
setHandlers(in, out, this)
|
2016-11-16 01:48:33 +08:00
|
|
|
}
|
|
|
|
|
|
2014-10-08 18:16:57 +02:00
|
|
|
}
|
|
|
|
|
|
2016-01-20 19:29:50 +02:00
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2019-03-13 10:56:20 +01:00
|
|
|
@InternalApi private[akka] final case class Batch[In, Out](
|
|
|
|
|
val max: Long,
|
|
|
|
|
val costFn: In => Long,
|
|
|
|
|
val seed: In => Out,
|
|
|
|
|
val aggregate: (Out, In) => Out)
|
2019-03-11 10:38:24 +01:00
|
|
|
extends GraphStage[FlowShape[In, Out]] {
|
2016-01-20 18:20:12 +02:00
|
|
|
|
|
|
|
|
val in = Inlet[In]("Batch.in")
|
|
|
|
|
val out = Outlet[Out]("Batch.out")
|
2016-01-19 23:03:36 +02:00
|
|
|
|
|
|
|
|
override val shape: FlowShape[In, Out] = FlowShape.of(in, out)
|
|
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
|
|
|
|
|
new GraphStageLogic(shape) with InHandler with OutHandler {
|
2016-01-21 16:52:44 +01:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
lazy val decider = inheritedAttributes.mandatoryAttribute[SupervisionStrategy].decider
|
2016-01-21 16:52:44 +01:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
private var agg: Out = null.asInstanceOf[Out]
|
|
|
|
|
private var left: Long = max
|
|
|
|
|
private var pending: In = null.asInstanceOf[In]
|
2016-01-19 23:03:36 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
private def flush(): Unit = {
|
|
|
|
|
if (agg != null) {
|
|
|
|
|
push(out, agg)
|
|
|
|
|
left = max
|
|
|
|
|
}
|
|
|
|
|
if (pending != null) {
|
|
|
|
|
try {
|
|
|
|
|
agg = seed(pending)
|
|
|
|
|
left -= costFn(pending)
|
|
|
|
|
pending = null.asInstanceOf[In]
|
|
|
|
|
} catch {
|
|
|
|
|
case NonFatal(ex) =>
|
|
|
|
|
decider(ex) match {
|
|
|
|
|
case Supervision.Stop => failStage(ex)
|
|
|
|
|
case Supervision.Restart => restartState()
|
|
|
|
|
case Supervision.Resume =>
|
|
|
|
|
pending = null.asInstanceOf[In]
|
|
|
|
|
}
|
2016-01-21 16:52:44 +01:00
|
|
|
}
|
2019-03-11 10:38:24 +01:00
|
|
|
} else {
|
|
|
|
|
agg = null.asInstanceOf[Out]
|
2016-01-21 16:52:44 +01:00
|
|
|
}
|
2016-01-19 23:03:36 +02:00
|
|
|
}
|
|
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def preStart() = pull(in)
|
2016-01-19 23:03:36 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
def onPush(): Unit = {
|
|
|
|
|
val elem = grab(in)
|
|
|
|
|
val cost = costFn(elem)
|
2016-01-19 23:03:36 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
if (agg == null) {
|
|
|
|
|
try {
|
|
|
|
|
agg = seed(elem)
|
|
|
|
|
left -= cost
|
|
|
|
|
} catch {
|
|
|
|
|
case NonFatal(ex) =>
|
|
|
|
|
decider(ex) match {
|
|
|
|
|
case Supervision.Stop => failStage(ex)
|
|
|
|
|
case Supervision.Restart =>
|
|
|
|
|
restartState()
|
|
|
|
|
case Supervision.Resume =>
|
|
|
|
|
}
|
2016-08-29 14:00:48 +02:00
|
|
|
}
|
2019-03-11 10:38:24 +01:00
|
|
|
} else if (left < cost) {
|
|
|
|
|
pending = elem
|
|
|
|
|
} else {
|
|
|
|
|
try {
|
|
|
|
|
agg = aggregate(agg, elem)
|
|
|
|
|
left -= cost
|
|
|
|
|
} catch {
|
|
|
|
|
case NonFatal(ex) =>
|
|
|
|
|
decider(ex) match {
|
|
|
|
|
case Supervision.Stop => failStage(ex)
|
|
|
|
|
case Supervision.Restart =>
|
|
|
|
|
restartState()
|
|
|
|
|
case Supervision.Resume =>
|
|
|
|
|
}
|
2016-08-29 14:00:48 +02:00
|
|
|
}
|
|
|
|
|
}
|
2016-01-21 16:52:44 +01:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
if (isAvailable(out)) flush()
|
|
|
|
|
if (pending == null) pull(in)
|
|
|
|
|
}
|
2016-08-29 14:00:48 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def onUpstreamFinish(): Unit = {
|
|
|
|
|
if (agg == null) completeStage()
|
|
|
|
|
}
|
2016-08-29 14:00:48 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
def onPull(): Unit = {
|
|
|
|
|
if (agg == null) {
|
|
|
|
|
if (isClosed(in)) completeStage()
|
|
|
|
|
else if (!hasBeenPulled(in)) pull(in)
|
|
|
|
|
} else if (isClosed(in)) {
|
|
|
|
|
push(out, agg)
|
|
|
|
|
if (pending == null) completeStage()
|
|
|
|
|
else {
|
|
|
|
|
try {
|
|
|
|
|
agg = seed(pending)
|
|
|
|
|
} catch {
|
|
|
|
|
case NonFatal(ex) =>
|
|
|
|
|
decider(ex) match {
|
|
|
|
|
case Supervision.Stop => failStage(ex)
|
|
|
|
|
case Supervision.Resume =>
|
|
|
|
|
case Supervision.Restart =>
|
|
|
|
|
restartState()
|
|
|
|
|
if (!hasBeenPulled(in)) pull(in)
|
|
|
|
|
}
|
2016-01-21 16:52:44 +01:00
|
|
|
}
|
2019-03-11 10:38:24 +01:00
|
|
|
pending = null.asInstanceOf[In]
|
2016-01-21 16:52:44 +01:00
|
|
|
}
|
2019-03-11 10:38:24 +01:00
|
|
|
} else {
|
|
|
|
|
flush()
|
|
|
|
|
if (!hasBeenPulled(in)) pull(in)
|
2016-01-19 23:03:36 +02:00
|
|
|
}
|
2019-03-11 10:38:24 +01:00
|
|
|
|
2016-01-19 23:03:36 +02:00
|
|
|
}
|
|
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
private def restartState(): Unit = {
|
|
|
|
|
agg = null.asInstanceOf[Out]
|
|
|
|
|
left = max
|
|
|
|
|
pending = null.asInstanceOf[In]
|
|
|
|
|
}
|
2016-01-21 16:52:44 +01:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
setHandlers(in, out, this)
|
2016-01-21 16:52:44 +01:00
|
|
|
}
|
2016-01-19 23:03:36 +02:00
|
|
|
}
|
|
|
|
|
|
2014-10-08 18:16:57 +02:00
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2019-03-11 10:38:24 +01:00
|
|
|
@InternalApi private[akka] final class Expand[In, Out](val extrapolate: In => Iterator[Out])
|
|
|
|
|
extends GraphStage[FlowShape[In, Out]] {
|
2016-01-18 11:29:14 +01:00
|
|
|
private val in = Inlet[In]("expand.in")
|
|
|
|
|
private val out = Outlet[Out]("expand.out")
|
2014-10-08 18:16:57 +02:00
|
|
|
|
2016-01-18 11:29:14 +01:00
|
|
|
override def initialAttributes = DefaultAttributes.expand
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2016-01-18 11:29:14 +01:00
|
|
|
override val shape = FlowShape(in, out)
|
2014-10-08 18:16:57 +02:00
|
|
|
|
2016-08-29 14:00:48 +02:00
|
|
|
override def createLogic(attr: Attributes) = new GraphStageLogic(shape) with InHandler with OutHandler {
|
2016-01-18 11:29:14 +01:00
|
|
|
private var iterator: Iterator[Out] = Iterator.empty
|
|
|
|
|
private var expanded = false
|
2014-10-08 18:16:57 +02:00
|
|
|
|
2016-01-18 11:29:14 +01:00
|
|
|
override def preStart(): Unit = pull(in)
|
2014-11-25 12:26:24 +01:00
|
|
|
|
2016-08-29 14:00:48 +02:00
|
|
|
def onPush(): Unit = {
|
|
|
|
|
iterator = extrapolate(grab(in))
|
|
|
|
|
if (iterator.hasNext) {
|
|
|
|
|
if (isAvailable(out)) {
|
|
|
|
|
expanded = true
|
|
|
|
|
pull(in)
|
|
|
|
|
push(out, iterator.next())
|
|
|
|
|
} else expanded = false
|
|
|
|
|
} else pull(in)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def onUpstreamFinish(): Unit = {
|
|
|
|
|
if (iterator.hasNext && !expanded) () // need to wait
|
|
|
|
|
else completeStage()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def onPull(): Unit = {
|
|
|
|
|
if (iterator.hasNext) {
|
|
|
|
|
if (!expanded) {
|
|
|
|
|
expanded = true
|
|
|
|
|
if (isClosed(in)) {
|
|
|
|
|
push(out, iterator.next())
|
|
|
|
|
completeStage()
|
|
|
|
|
} else {
|
|
|
|
|
// expand needs to pull first to be “fair” when upstream is not actually slow
|
2016-01-18 11:29:14 +01:00
|
|
|
pull(in)
|
|
|
|
|
push(out, iterator.next())
|
2016-08-29 14:00:48 +02:00
|
|
|
}
|
|
|
|
|
} else push(out, iterator.next())
|
2016-01-18 11:29:14 +01:00
|
|
|
}
|
2016-08-29 14:00:48 +02:00
|
|
|
}
|
2016-01-18 11:29:14 +01:00
|
|
|
|
2016-08-29 14:00:48 +02:00
|
|
|
setHandler(in, this)
|
|
|
|
|
setHandler(out, this)
|
2014-11-25 12:26:24 +01:00
|
|
|
}
|
2014-11-19 19:50:23 +01:00
|
|
|
}
|
2015-04-09 22:28:16 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2017-03-16 21:04:07 +02:00
|
|
|
@InternalApi private[akka] object MapAsync {
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
final class Holder[T](var elem: Try[T], val cb: AsyncCallback[Holder[T]]) extends (Try[T] => Unit) {
|
2017-12-05 18:51:58 +01:00
|
|
|
|
|
|
|
|
// To support both fail-fast when the supervision directive is Stop
|
|
|
|
|
// and not calling the decider multiple times (#23888) we need to cache the decider result and re-use that
|
|
|
|
|
private var cachedSupervisionDirective: OptionVal[Supervision.Directive] = OptionVal.None
|
|
|
|
|
|
|
|
|
|
def supervisionDirectiveFor(decider: Supervision.Decider, ex: Throwable): Supervision.Directive = {
|
|
|
|
|
cachedSupervisionDirective match {
|
2019-02-09 15:25:39 +01:00
|
|
|
case OptionVal.Some(d) => d
|
|
|
|
|
case OptionVal.None =>
|
2017-12-05 18:51:58 +01:00
|
|
|
val d = decider(ex)
|
|
|
|
|
cachedSupervisionDirective = OptionVal.Some(d)
|
|
|
|
|
d
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-11 14:17:13 +02:00
|
|
|
def setElem(t: Try[T]): Unit = {
|
2019-04-16 09:08:05 +02:00
|
|
|
elem = t
|
2017-09-11 14:17:13 +02:00
|
|
|
}
|
2016-08-30 19:30:08 +02:00
|
|
|
|
|
|
|
|
override def apply(t: Try[T]): Unit = {
|
|
|
|
|
setElem(t)
|
2016-04-04 13:11:22 +02:00
|
|
|
cb.invoke(this)
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2017-12-05 18:51:58 +01:00
|
|
|
val NotYetThere = Failure(new Exception with NoStackTrace)
|
2015-04-09 22:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2019-02-09 15:25:39 +01:00
|
|
|
@InternalApi private[akka] final case class MapAsync[In, Out](parallelism: Int, f: In => Future[Out])
|
2019-03-11 10:38:24 +01:00
|
|
|
extends GraphStage[FlowShape[In, Out]] {
|
2015-04-09 12:21:12 +02:00
|
|
|
|
2015-04-09 22:28:16 +02:00
|
|
|
import MapAsync._
|
|
|
|
|
|
2016-04-04 13:11:22 +02:00
|
|
|
private val in = Inlet[In]("MapAsync.in")
|
|
|
|
|
private val out = Outlet[Out]("MapAsync.out")
|
2015-04-09 22:28:16 +02:00
|
|
|
|
2016-01-18 17:49:32 +01:00
|
|
|
override def initialAttributes = DefaultAttributes.mapAsync
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2015-10-31 14:46:10 +01:00
|
|
|
override val shape = FlowShape(in, out)
|
2015-04-09 22:28:16 +02:00
|
|
|
|
2016-04-04 13:11:22 +02:00
|
|
|
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
|
|
|
|
|
new GraphStageLogic(shape) with InHandler with OutHandler {
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2017-11-22 13:51:24 +01:00
|
|
|
lazy val decider = inheritedAttributes.mandatoryAttribute[SupervisionStrategy].decider
|
|
|
|
|
var buffer: BufferImpl[Holder[Out]] = _
|
2017-12-05 18:51:58 +01:00
|
|
|
|
2019-02-09 15:25:39 +01:00
|
|
|
private val futureCB = getAsyncCallback[Holder[Out]](holder =>
|
2017-12-05 18:51:58 +01:00
|
|
|
holder.elem match {
|
2019-02-09 15:25:39 +01:00
|
|
|
case Success(_) => pushNextIfPossible()
|
|
|
|
|
case Failure(ex) =>
|
2017-12-05 18:51:58 +01:00
|
|
|
holder.supervisionDirectiveFor(decider, ex) match {
|
|
|
|
|
// fail fast as if supervision says so
|
2019-02-09 15:25:39 +01:00
|
|
|
case Supervision.Stop => failStage(ex)
|
|
|
|
|
case _ => pushNextIfPossible()
|
2017-12-05 18:51:58 +01:00
|
|
|
}
|
|
|
|
|
})
|
2015-04-09 22:28:16 +02:00
|
|
|
|
2019-09-04 13:37:06 +02:00
|
|
|
override def preStart(): Unit = buffer = BufferImpl(parallelism, inheritedAttributes)
|
2016-02-07 14:54:48 +01:00
|
|
|
|
2017-12-05 18:51:58 +01:00
|
|
|
override def onPull(): Unit = pushNextIfPossible()
|
2015-10-31 14:46:10 +01:00
|
|
|
|
|
|
|
|
override def onPush(): Unit = {
|
|
|
|
|
try {
|
|
|
|
|
val future = f(grab(in))
|
2016-04-04 13:11:22 +02:00
|
|
|
val holder = new Holder[Out](NotYetThere, futureCB)
|
2015-12-20 12:54:05 +01:00
|
|
|
buffer.enqueue(holder)
|
2016-04-28 13:23:03 -07:00
|
|
|
|
|
|
|
|
future.value match {
|
2019-03-11 10:38:24 +01:00
|
|
|
case None => future.onComplete(holder)(akka.dispatch.ExecutionContexts.sameThreadExecutionContext)
|
2019-02-09 15:25:39 +01:00
|
|
|
case Some(v) =>
|
2017-12-07 10:09:15 +01:00
|
|
|
// #20217 the future is already here, optimization: avoid scheduling it on the dispatcher and
|
|
|
|
|
// run the logic directly on this thread
|
2016-08-30 19:30:08 +02:00
|
|
|
holder.setElem(v)
|
2017-12-07 10:09:15 +01:00
|
|
|
v match {
|
|
|
|
|
// this optimization also requires us to stop the stage to fail fast if the decider says so:
|
2019-02-09 15:25:39 +01:00
|
|
|
case Failure(ex) if holder.supervisionDirectiveFor(decider, ex) == Supervision.Stop => failStage(ex)
|
2019-03-11 10:38:24 +01:00
|
|
|
case _ => pushNextIfPossible()
|
2017-12-07 10:09:15 +01:00
|
|
|
}
|
2016-04-28 13:23:03 -07:00
|
|
|
}
|
|
|
|
|
|
2015-04-09 22:28:16 +02:00
|
|
|
} catch {
|
2017-12-05 18:51:58 +01:00
|
|
|
// this logic must only be executed if f throws, not if the future is failed
|
2019-02-09 15:25:39 +01:00
|
|
|
case NonFatal(ex) => if (decider(ex) == Supervision.Stop) failStage(ex)
|
2015-04-09 22:28:16 +02:00
|
|
|
}
|
2017-12-05 18:51:58 +01:00
|
|
|
|
|
|
|
|
pullIfNeeded()
|
2015-10-31 14:46:10 +01:00
|
|
|
}
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2017-12-05 18:51:58 +01:00
|
|
|
override def onUpstreamFinish(): Unit = if (buffer.isEmpty) completeStage()
|
|
|
|
|
|
|
|
|
|
@tailrec
|
|
|
|
|
private def pushNextIfPossible(): Unit =
|
|
|
|
|
if (buffer.isEmpty) {
|
|
|
|
|
if (isClosed(in)) completeStage()
|
|
|
|
|
else pullIfNeeded()
|
|
|
|
|
} else if (buffer.peek().elem eq NotYetThere) pullIfNeeded() // ahead of line blocking to keep order
|
|
|
|
|
else if (isAvailable(out)) {
|
|
|
|
|
val holder = buffer.dequeue()
|
|
|
|
|
holder.elem match {
|
2019-04-16 09:08:05 +02:00
|
|
|
case Success(elem) if elem != null =>
|
2017-12-05 18:51:58 +01:00
|
|
|
push(out, elem)
|
|
|
|
|
pullIfNeeded()
|
|
|
|
|
|
2019-04-16 09:08:05 +02:00
|
|
|
case Success(null) =>
|
|
|
|
|
pullIfNeeded()
|
|
|
|
|
pushNextIfPossible()
|
|
|
|
|
|
2019-02-09 15:25:39 +01:00
|
|
|
case Failure(NonFatal(ex)) =>
|
2017-12-05 18:51:58 +01:00
|
|
|
holder.supervisionDirectiveFor(decider, ex) match {
|
2017-12-07 10:09:15 +01:00
|
|
|
// this could happen if we are looping in pushNextIfPossible and end up on a failed future before the
|
|
|
|
|
// onComplete callback has run
|
2019-02-09 15:25:39 +01:00
|
|
|
case Supervision.Stop => failStage(ex)
|
2019-03-11 10:38:24 +01:00
|
|
|
case _ =>
|
2017-12-05 18:51:58 +01:00
|
|
|
// try next element
|
|
|
|
|
pushNextIfPossible()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-04-09 22:28:16 +02:00
|
|
|
|
2017-12-05 18:51:58 +01:00
|
|
|
private def pullIfNeeded(): Unit = {
|
|
|
|
|
if (buffer.used < parallelism && !hasBeenPulled(in)) tryPull(in)
|
|
|
|
|
}
|
2016-04-04 13:11:22 +02:00
|
|
|
|
|
|
|
|
setHandlers(in, out, this)
|
|
|
|
|
}
|
2015-04-09 22:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2019-02-09 15:25:39 +01:00
|
|
|
@InternalApi private[akka] final case class MapAsyncUnordered[In, Out](parallelism: Int, f: In => Future[Out])
|
2019-03-11 10:38:24 +01:00
|
|
|
extends GraphStage[FlowShape[In, Out]] {
|
2015-10-31 14:46:10 +01:00
|
|
|
|
2016-04-04 13:11:22 +02:00
|
|
|
private val in = Inlet[In]("MapAsyncUnordered.in")
|
|
|
|
|
private val out = Outlet[Out]("MapAsyncUnordered.out")
|
2015-10-31 14:46:10 +01:00
|
|
|
|
2016-01-18 17:49:32 +01:00
|
|
|
override def initialAttributes = DefaultAttributes.mapAsyncUnordered
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2015-10-31 14:46:10 +01:00
|
|
|
override val shape = FlowShape(in, out)
|
|
|
|
|
|
2016-04-04 13:11:22 +02:00
|
|
|
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
|
|
|
|
|
new GraphStageLogic(shape) with InHandler with OutHandler {
|
|
|
|
|
override def toString = s"MapAsyncUnordered.Logic(inFlight=$inFlight, buffer=$buffer)"
|
|
|
|
|
|
|
|
|
|
val decider =
|
2017-11-22 13:51:24 +01:00
|
|
|
inheritedAttributes.mandatoryAttribute[SupervisionStrategy].decider
|
2016-04-04 13:11:22 +02:00
|
|
|
|
2016-08-24 21:02:32 +02:00
|
|
|
private var inFlight = 0
|
|
|
|
|
private var buffer: BufferImpl[Out] = _
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2016-04-04 13:11:22 +02:00
|
|
|
private[this] def todo = inFlight + buffer.used
|
|
|
|
|
|
2019-09-04 13:37:06 +02:00
|
|
|
override def preStart(): Unit = buffer = BufferImpl(parallelism, inheritedAttributes)
|
2016-04-04 13:11:22 +02:00
|
|
|
|
2016-08-30 19:30:08 +02:00
|
|
|
def futureCompleted(result: Try[Out]): Unit = {
|
|
|
|
|
inFlight -= 1
|
|
|
|
|
result match {
|
2019-02-09 15:25:39 +01:00
|
|
|
case Success(elem) if elem != null =>
|
2016-08-30 19:30:08 +02:00
|
|
|
if (isAvailable(out)) {
|
|
|
|
|
if (!hasBeenPulled(in)) tryPull(in)
|
|
|
|
|
push(out, elem)
|
|
|
|
|
} else buffer.enqueue(elem)
|
2019-04-16 09:08:05 +02:00
|
|
|
case Success(null) =>
|
|
|
|
|
if (isClosed(in) && todo == 0) completeStage()
|
|
|
|
|
else if (!hasBeenPulled(in)) tryPull(in)
|
|
|
|
|
case Failure(ex) =>
|
2016-08-30 19:30:08 +02:00
|
|
|
if (decider(ex) == Supervision.Stop) failStage(ex)
|
|
|
|
|
else if (isClosed(in) && todo == 0) completeStage()
|
|
|
|
|
else if (!hasBeenPulled(in)) tryPull(in)
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2016-08-30 19:30:08 +02:00
|
|
|
private val futureCB = getAsyncCallback(futureCompleted)
|
2019-02-09 15:25:39 +01:00
|
|
|
private val invokeFutureCB: Try[Out] => Unit = futureCB.invoke
|
2015-04-09 22:28:16 +02:00
|
|
|
|
2015-10-31 14:46:10 +01:00
|
|
|
override def onPush(): Unit = {
|
|
|
|
|
try {
|
|
|
|
|
val future = f(grab(in))
|
|
|
|
|
inFlight += 1
|
2016-04-28 13:23:03 -07:00
|
|
|
future.value match {
|
2019-02-09 15:25:39 +01:00
|
|
|
case None => future.onComplete(invokeFutureCB)(akka.dispatch.ExecutionContexts.sameThreadExecutionContext)
|
|
|
|
|
case Some(v) => futureCompleted(v)
|
2016-04-28 13:23:03 -07:00
|
|
|
}
|
2015-10-31 14:46:10 +01:00
|
|
|
} catch {
|
2019-02-09 15:25:39 +01:00
|
|
|
case NonFatal(ex) => if (decider(ex) == Supervision.Stop) failStage(ex)
|
2015-04-09 22:28:16 +02:00
|
|
|
}
|
2016-08-30 19:30:08 +02:00
|
|
|
if (todo < parallelism && !hasBeenPulled(in)) tryPull(in)
|
2015-10-31 14:46:10 +01:00
|
|
|
}
|
2016-08-24 21:02:32 +02:00
|
|
|
|
2015-10-31 14:46:10 +01:00
|
|
|
override def onUpstreamFinish(): Unit = {
|
|
|
|
|
if (todo == 0) completeStage()
|
|
|
|
|
}
|
2015-04-09 22:28:16 +02:00
|
|
|
|
2015-10-31 14:46:10 +01:00
|
|
|
override def onPull(): Unit = {
|
|
|
|
|
if (!buffer.isEmpty) push(out, buffer.dequeue())
|
|
|
|
|
else if (isClosed(in) && todo == 0) completeStage()
|
2016-08-24 21:02:32 +02:00
|
|
|
|
2015-10-31 14:46:10 +01:00
|
|
|
if (todo < parallelism && !hasBeenPulled(in)) tryPull(in)
|
|
|
|
|
}
|
2016-04-04 13:11:22 +02:00
|
|
|
|
|
|
|
|
setHandlers(in, out, this)
|
|
|
|
|
}
|
2015-04-09 22:28:16 +02:00
|
|
|
}
|
2015-04-09 12:21:12 +02:00
|
|
|
|
2018-01-14 00:21:00 +09:00
|
|
|
@InternalApi private[akka] final case class Watch[T](targetRef: ActorRef) extends SimpleLinearGraphStage[T] {
|
|
|
|
|
|
|
|
|
|
override def initialAttributes = DefaultAttributes.watch
|
|
|
|
|
|
|
|
|
|
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
|
|
|
|
|
new GraphStageLogic(shape) with InHandler with OutHandler with StageLogging {
|
2019-10-14 17:55:12 +02:00
|
|
|
override protected def logSource: Class[_] = classOf[Watch[_]]
|
2018-01-14 00:21:00 +09:00
|
|
|
|
|
|
|
|
override def preStart(): Unit = {
|
2019-11-22 13:38:06 +01:00
|
|
|
val self = getStageActor {
|
|
|
|
|
case (_, Terminated(`targetRef`)) =>
|
|
|
|
|
failStage(new WatchedActorTerminatedException("Watch", targetRef))
|
|
|
|
|
case (_, _) => // keep the compiler happy (stage actor receive is total)
|
|
|
|
|
}
|
2018-01-14 00:21:00 +09:00
|
|
|
self.watch(targetRef)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def onPull(): Unit =
|
|
|
|
|
pull(in)
|
|
|
|
|
|
|
|
|
|
override def onPush(): Unit =
|
|
|
|
|
push(out, grab(in))
|
|
|
|
|
|
|
|
|
|
setHandlers(in, out, this)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-09 12:21:12 +02:00
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2019-03-11 10:38:24 +01:00
|
|
|
@InternalApi private[akka] final case class Log[T](name: String, extract: T => Any, logAdapter: Option[LoggingAdapter])
|
|
|
|
|
extends SimpleLinearGraphStage[T] {
|
2015-04-09 12:21:12 +02:00
|
|
|
|
2016-06-22 16:36:18 +02:00
|
|
|
override def toString = "Log"
|
2015-04-09 12:21:12 +02:00
|
|
|
|
2015-05-11 00:09:59 +02:00
|
|
|
// TODO more optimisations can be done here - prepare logOnPush function etc
|
2016-06-22 16:36:18 +02:00
|
|
|
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
|
|
|
|
|
new GraphStageLogic(shape) with OutHandler with InHandler {
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2016-06-22 16:36:18 +02:00
|
|
|
import Log._
|
2015-05-11 00:09:59 +02:00
|
|
|
|
2016-06-22 16:36:18 +02:00
|
|
|
private var logLevels: LogLevels = _
|
|
|
|
|
private var log: LoggingAdapter = _
|
2015-05-27 00:27:05 +02:00
|
|
|
|
2017-11-22 13:51:24 +01:00
|
|
|
def decider = inheritedAttributes.mandatoryAttribute[SupervisionStrategy].decider
|
2015-04-09 12:21:12 +02:00
|
|
|
|
2016-06-22 16:36:18 +02:00
|
|
|
override def preStart(): Unit = {
|
|
|
|
|
logLevels = inheritedAttributes.get[LogLevels](DefaultLogLevels)
|
|
|
|
|
log = logAdapter match {
|
2019-02-09 15:25:39 +01:00
|
|
|
case Some(l) => l
|
|
|
|
|
case _ =>
|
2019-09-05 16:08:37 +02:00
|
|
|
Logging(materializer.system, materializer)(fromMaterializer)
|
2016-06-22 16:36:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
2015-04-09 12:21:12 +02:00
|
|
|
|
2016-06-22 16:36:18 +02:00
|
|
|
override def onPush(): Unit = {
|
|
|
|
|
try {
|
|
|
|
|
val elem = grab(in)
|
|
|
|
|
if (isEnabled(logLevels.onElement))
|
|
|
|
|
log.log(logLevels.onElement, "[{}] Element: {}", name, extract(elem))
|
|
|
|
|
|
|
|
|
|
push(out, elem)
|
|
|
|
|
} catch {
|
2019-03-11 10:38:24 +01:00
|
|
|
case NonFatal(ex) =>
|
|
|
|
|
decider(ex) match {
|
|
|
|
|
case Supervision.Stop => failStage(ex)
|
|
|
|
|
case _ => pull(in)
|
|
|
|
|
}
|
2016-06-22 16:36:18 +02:00
|
|
|
}
|
2015-04-09 12:21:12 +02:00
|
|
|
}
|
|
|
|
|
|
2016-06-22 16:36:18 +02:00
|
|
|
override def onPull(): Unit = pull(in)
|
2015-04-09 12:21:12 +02:00
|
|
|
|
2016-06-22 16:36:18 +02:00
|
|
|
override def onUpstreamFailure(cause: Throwable): Unit = {
|
|
|
|
|
if (isEnabled(logLevels.onFailure))
|
|
|
|
|
logLevels.onFailure match {
|
2019-02-09 15:25:39 +01:00
|
|
|
case Logging.ErrorLevel => log.error(cause, "[{}] Upstream failed.", name)
|
2019-03-11 10:38:24 +01:00
|
|
|
case level =>
|
2019-03-13 10:56:20 +01:00
|
|
|
log.log(
|
|
|
|
|
level,
|
|
|
|
|
"[{}] Upstream failed, cause: {}: {}",
|
|
|
|
|
name,
|
|
|
|
|
Logging.simpleName(cause.getClass),
|
|
|
|
|
cause.getMessage)
|
2016-06-22 16:36:18 +02:00
|
|
|
}
|
2015-04-09 12:21:12 +02:00
|
|
|
|
2016-06-22 16:36:18 +02:00
|
|
|
super.onUpstreamFailure(cause)
|
|
|
|
|
}
|
2015-04-09 12:21:12 +02:00
|
|
|
|
2016-06-22 16:36:18 +02:00
|
|
|
override def onUpstreamFinish(): Unit = {
|
|
|
|
|
if (isEnabled(logLevels.onFinish))
|
|
|
|
|
log.log(logLevels.onFinish, "[{}] Upstream finished.", name)
|
2015-04-09 12:21:12 +02:00
|
|
|
|
2016-06-22 16:36:18 +02:00
|
|
|
super.onUpstreamFinish()
|
|
|
|
|
}
|
2015-04-09 12:21:12 +02:00
|
|
|
|
2019-08-16 10:53:14 +02:00
|
|
|
override def onDownstreamFinish(cause: Throwable): Unit = {
|
2016-06-22 16:36:18 +02:00
|
|
|
if (isEnabled(logLevels.onFinish))
|
2019-08-16 10:53:14 +02:00
|
|
|
log.log(
|
|
|
|
|
logLevels.onFinish,
|
|
|
|
|
"[{}] Downstream finished, cause: {}: {}",
|
|
|
|
|
name,
|
|
|
|
|
Logging.simpleName(cause.getClass),
|
|
|
|
|
cause.getMessage)
|
2015-04-09 12:21:12 +02:00
|
|
|
|
2019-08-16 10:53:14 +02:00
|
|
|
super.onDownstreamFinish(cause: Throwable)
|
2016-06-22 16:36:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private def isEnabled(l: LogLevel): Boolean = l.asInt != OffInt
|
|
|
|
|
|
|
|
|
|
setHandlers(in, out, this)
|
|
|
|
|
}
|
2015-04-09 12:21:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2017-03-16 21:04:07 +02:00
|
|
|
@InternalApi private[akka] object Log {
|
2015-04-09 12:21:12 +02:00
|
|
|
|
2015-05-27 00:27:05 +02:00
|
|
|
/**
|
2016-06-22 16:36:18 +02:00
|
|
|
* Must be located here to be visible for implicit resolution, when [[Materializer]] is passed to [[Logging]]
|
2015-05-27 00:27:05 +02:00
|
|
|
* More specific LogSource than `fromString`, which would add the ActorSystem name in addition to the supervision to the log source.
|
|
|
|
|
*/
|
2016-06-22 16:36:18 +02:00
|
|
|
final val fromMaterializer = new LogSource[Materializer] {
|
2015-05-27 00:27:05 +02:00
|
|
|
|
|
|
|
|
// do not expose private context classes (of OneBoundedInterpreter)
|
2016-06-22 16:36:18 +02:00
|
|
|
override def getClazz(t: Materializer): Class[_] = classOf[Materializer]
|
2015-05-27 00:27:05 +02:00
|
|
|
|
2016-06-22 16:36:18 +02:00
|
|
|
override def genString(t: Materializer): String = {
|
2019-09-05 16:08:37 +02:00
|
|
|
try s"$DefaultLoggerName(${t.supervisor.path})"
|
2015-05-27 00:27:05 +02:00
|
|
|
catch {
|
2019-02-09 15:25:39 +01:00
|
|
|
case _: Exception => LogSource.fromString.genString(DefaultLoggerName)
|
2015-05-27 00:27:05 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private final val DefaultLoggerName = "akka.stream.Log"
|
2015-04-09 12:21:12 +02:00
|
|
|
private final val OffInt = LogLevels.Off.asInt
|
2019-03-11 10:38:24 +01:00
|
|
|
private final val DefaultLogLevels =
|
|
|
|
|
LogLevels(onElement = Logging.DebugLevel, onFinish = Logging.DebugLevel, onFailure = Logging.ErrorLevel)
|
2015-06-01 18:08:13 +03:00
|
|
|
}
|
2015-09-11 15:50:17 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2017-03-16 21:04:07 +02:00
|
|
|
@InternalApi private[stream] object TimerKeys {
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2015-09-11 15:50:17 +02:00
|
|
|
case object TakeWithinTimerKey
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2015-09-11 15:50:17 +02:00
|
|
|
case object DropWithinTimerKey
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2015-09-11 15:50:17 +02:00
|
|
|
case object GroupedWithinTimerKey
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2015-09-11 15:50:17 +02:00
|
|
|
}
|
|
|
|
|
|
2017-04-28 16:07:06 +03:00
|
|
|
@InternalApi private[akka] object GroupedWeightedWithin {
|
|
|
|
|
val groupedWeightedWithinTimer = "GroupedWeightedWithinTimer"
|
|
|
|
|
}
|
2018-03-19 14:42:37 +01:00
|
|
|
|
2017-03-16 21:04:07 +02:00
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2019-03-13 10:56:20 +01:00
|
|
|
@InternalApi private[akka] final class GroupedWeightedWithin[T](
|
|
|
|
|
val maxWeight: Long,
|
|
|
|
|
costFn: T => Long,
|
|
|
|
|
val interval: FiniteDuration)
|
2019-03-11 10:38:24 +01:00
|
|
|
extends GraphStage[FlowShape[T, immutable.Seq[T]]] {
|
2017-04-28 16:07:06 +03:00
|
|
|
require(maxWeight > 0, "maxWeight must be greater than 0")
|
|
|
|
|
require(interval > Duration.Zero)
|
2016-01-18 17:49:32 +01:00
|
|
|
|
2015-09-11 15:50:17 +02:00
|
|
|
val in = Inlet[T]("in")
|
|
|
|
|
val out = Outlet[immutable.Seq[T]]("out")
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2017-04-28 16:07:06 +03:00
|
|
|
override def initialAttributes = DefaultAttributes.groupedWeightedWithin
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2015-09-11 15:50:17 +02:00
|
|
|
val shape = FlowShape(in, out)
|
|
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
|
|
|
|
|
new TimerGraphStageLogic(shape) with InHandler with OutHandler {
|
|
|
|
|
|
|
|
|
|
private val buf: VectorBuilder[T] = new VectorBuilder
|
|
|
|
|
private var pending: T = null.asInstanceOf[T]
|
|
|
|
|
private var pendingWeight: Long = 0L
|
|
|
|
|
// True if:
|
|
|
|
|
// - buf is nonEmpty
|
|
|
|
|
// AND
|
|
|
|
|
// - (timer fired
|
|
|
|
|
// OR
|
|
|
|
|
// totalWeight >= maxWeight
|
|
|
|
|
// OR
|
|
|
|
|
// pending != null
|
|
|
|
|
// OR
|
|
|
|
|
// upstream completed)
|
|
|
|
|
private var pushEagerly = false
|
|
|
|
|
private var groupEmitted = true
|
|
|
|
|
private var finished = false
|
|
|
|
|
private var totalWeight = 0L
|
|
|
|
|
private var hasElements = false
|
|
|
|
|
|
|
|
|
|
override def preStart() = {
|
2019-05-27 11:53:26 +02:00
|
|
|
scheduleWithFixedDelay(GroupedWeightedWithin.groupedWeightedWithinTimer, interval, interval)
|
2019-03-11 10:38:24 +01:00
|
|
|
pull(in)
|
|
|
|
|
}
|
2015-09-11 15:50:17 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
private def nextElement(elem: T): Unit = {
|
|
|
|
|
groupEmitted = false
|
|
|
|
|
val cost = costFn(elem)
|
|
|
|
|
if (cost < 0L)
|
|
|
|
|
failStage(new IllegalArgumentException(s"Negative weight [$cost] for element [$elem] is not allowed"))
|
|
|
|
|
else {
|
|
|
|
|
hasElements = true
|
|
|
|
|
if (totalWeight + cost <= maxWeight) {
|
2017-04-28 16:07:06 +03:00
|
|
|
buf += elem
|
|
|
|
|
totalWeight += cost
|
2019-03-11 10:38:24 +01:00
|
|
|
|
|
|
|
|
if (totalWeight < maxWeight) pull(in)
|
|
|
|
|
else {
|
|
|
|
|
// `totalWeight >= maxWeight` which means that downstream can get the next group.
|
|
|
|
|
if (!isAvailable(out)) {
|
|
|
|
|
// We should emit group when downstream becomes available
|
|
|
|
|
pushEagerly = true
|
|
|
|
|
// we want to pull anyway, since we allow for zero weight elements
|
|
|
|
|
// but since `emitGroup()` will pull internally (by calling `startNewGroup()`)
|
|
|
|
|
// we also have to pull if downstream hasn't yet requested an element.
|
|
|
|
|
pull(in)
|
|
|
|
|
} else {
|
2019-05-27 11:53:26 +02:00
|
|
|
scheduleWithFixedDelay(GroupedWeightedWithin.groupedWeightedWithinTimer, interval, interval)
|
2019-03-11 10:38:24 +01:00
|
|
|
emitGroup()
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-28 16:07:06 +03:00
|
|
|
} else {
|
2019-03-11 10:38:24 +01:00
|
|
|
//we have a single heavy element that weighs more than the limit
|
|
|
|
|
if (totalWeight == 0L) {
|
|
|
|
|
buf += elem
|
|
|
|
|
totalWeight += cost
|
|
|
|
|
pushEagerly = true
|
|
|
|
|
} else {
|
|
|
|
|
pending = elem
|
|
|
|
|
pendingWeight = cost
|
|
|
|
|
}
|
2019-05-27 11:53:26 +02:00
|
|
|
scheduleWithFixedDelay(GroupedWeightedWithin.groupedWeightedWithinTimer, interval, interval)
|
2019-03-11 10:38:24 +01:00
|
|
|
tryCloseGroup()
|
2017-04-28 16:07:06 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-09-11 15:50:17 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
private def tryCloseGroup(): Unit = {
|
|
|
|
|
if (isAvailable(out)) emitGroup()
|
|
|
|
|
else if (pending != null || finished) pushEagerly = true
|
|
|
|
|
}
|
2015-09-11 15:50:17 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
private def emitGroup(): Unit = {
|
|
|
|
|
groupEmitted = true
|
|
|
|
|
push(out, buf.result())
|
|
|
|
|
buf.clear()
|
|
|
|
|
if (!finished) startNewGroup()
|
|
|
|
|
else if (pending != null) emit(out, Vector(pending), () => completeStage())
|
|
|
|
|
else completeStage()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private def startNewGroup(): Unit = {
|
|
|
|
|
if (pending != null) {
|
|
|
|
|
totalWeight = pendingWeight
|
|
|
|
|
pendingWeight = 0L
|
|
|
|
|
buf += pending
|
|
|
|
|
pending = null.asInstanceOf[T]
|
|
|
|
|
groupEmitted = false
|
|
|
|
|
} else {
|
|
|
|
|
totalWeight = 0L
|
|
|
|
|
hasElements = false
|
|
|
|
|
}
|
|
|
|
|
pushEagerly = false
|
|
|
|
|
if (isAvailable(in)) nextElement(grab(in))
|
|
|
|
|
else if (!hasBeenPulled(in)) pull(in)
|
|
|
|
|
}
|
2015-09-11 15:50:17 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def onPush(): Unit = {
|
|
|
|
|
if (pending == null) nextElement(grab(in)) // otherwise keep the element for next round
|
2017-04-28 16:07:06 +03:00
|
|
|
}
|
2015-09-11 15:50:17 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def onPull(): Unit = if (pushEagerly) emitGroup()
|
2015-09-11 15:50:17 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def onUpstreamFinish(): Unit = {
|
|
|
|
|
finished = true
|
|
|
|
|
if (groupEmitted) completeStage()
|
|
|
|
|
else tryCloseGroup()
|
|
|
|
|
}
|
2015-09-11 15:50:17 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override protected def onTimer(timerKey: Any) = if (hasElements) {
|
|
|
|
|
if (isAvailable(out)) emitGroup()
|
|
|
|
|
else pushEagerly = true
|
|
|
|
|
}
|
2016-04-05 12:46:31 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
setHandlers(in, out, this)
|
2017-04-28 16:07:06 +03:00
|
|
|
}
|
2015-09-11 15:50:17 +02:00
|
|
|
}
|
|
|
|
|
|
2017-03-16 21:04:07 +02:00
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2019-03-11 10:38:24 +01:00
|
|
|
@InternalApi private[akka] final class Delay[T](val d: FiniteDuration, val strategy: DelayOverflowStrategy)
|
|
|
|
|
extends SimpleLinearGraphStage[T] {
|
2016-01-18 17:49:32 +01:00
|
|
|
private[this] def timerName = "DelayedTimer"
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2017-03-30 09:27:01 -05:00
|
|
|
final val DelayPrecisionMS = 10
|
|
|
|
|
|
2016-01-18 17:49:32 +01:00
|
|
|
override def initialAttributes: Attributes = DefaultAttributes.delay
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
|
|
|
|
|
new TimerGraphStageLogic(shape) with InHandler with OutHandler {
|
|
|
|
|
val size = inheritedAttributes.mandatoryAttribute[InputBuffer].max
|
2017-11-22 13:51:24 +01:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
val delayMillis = d.toMillis
|
2016-01-18 17:49:32 +01:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
var buffer: BufferImpl[(Long, T)] = _ // buffer has pairs timestamp with upstream element
|
2015-11-21 13:48:10 -05:00
|
|
|
|
2019-09-04 13:37:06 +02:00
|
|
|
override def preStart(): Unit = buffer = BufferImpl(size, inheritedAttributes)
|
2016-02-07 14:54:48 +01:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
val onPushWhenBufferFull: () => Unit = strategy match {
|
|
|
|
|
case EmitEarly =>
|
|
|
|
|
() => {
|
|
|
|
|
if (!isTimerActive(timerName))
|
|
|
|
|
push(out, buffer.dequeue()._2)
|
|
|
|
|
else {
|
|
|
|
|
cancelTimer(timerName)
|
|
|
|
|
onTimer(timerName)
|
|
|
|
|
}
|
|
|
|
|
grabAndPull()
|
2016-08-29 14:00:48 +02:00
|
|
|
}
|
2019-03-11 10:38:24 +01:00
|
|
|
case _: DropHead =>
|
|
|
|
|
() => {
|
|
|
|
|
buffer.dropHead()
|
|
|
|
|
grabAndPull()
|
|
|
|
|
}
|
|
|
|
|
case _: DropTail =>
|
|
|
|
|
() => {
|
|
|
|
|
buffer.dropTail()
|
|
|
|
|
grabAndPull()
|
|
|
|
|
}
|
|
|
|
|
case _: DropNew =>
|
|
|
|
|
() => {
|
|
|
|
|
grab(in)
|
2019-07-05 03:07:16 -04:00
|
|
|
pull(in)
|
2019-03-11 10:38:24 +01:00
|
|
|
}
|
|
|
|
|
case _: DropBuffer =>
|
|
|
|
|
() => {
|
|
|
|
|
buffer.clear()
|
|
|
|
|
grabAndPull()
|
|
|
|
|
}
|
|
|
|
|
case _: Fail =>
|
|
|
|
|
() => {
|
|
|
|
|
failStage(BufferOverflowException(s"Buffer overflow for delay operator (max capacity was: $size)!"))
|
|
|
|
|
}
|
|
|
|
|
case _: Backpressure =>
|
|
|
|
|
() => {
|
|
|
|
|
throw new IllegalStateException("Delay buffer must never overflow in Backpressure mode")
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-13 16:10:49 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
def onPush(): Unit = {
|
|
|
|
|
if (buffer.isFull)
|
|
|
|
|
onPushWhenBufferFull()
|
|
|
|
|
else {
|
|
|
|
|
grabAndPull()
|
|
|
|
|
if (!isTimerActive(timerName)) {
|
2019-07-05 03:07:16 -04:00
|
|
|
// schedule a timer for the full-delay `d` only if the buffer is empty, because otherwise a
|
|
|
|
|
// full-length timer will starve subsequent `onPull` callbacks, preventing overdue elements
|
|
|
|
|
// to be discharged.
|
|
|
|
|
if (buffer.isEmpty)
|
|
|
|
|
scheduleOnce(timerName, d)
|
|
|
|
|
else
|
|
|
|
|
scheduleOnce(timerName, Math.max(DelayPrecisionMS, nextElementWaitTime()).millis)
|
2019-03-11 10:38:24 +01:00
|
|
|
}
|
2016-09-13 16:10:49 +02:00
|
|
|
}
|
2015-11-25 21:29:35 -05:00
|
|
|
}
|
|
|
|
|
|
2019-07-04 11:06:24 +02:00
|
|
|
def pullCondition: Boolean = strategy match {
|
|
|
|
|
case EmitEarly =>
|
|
|
|
|
// when buffer is full we can only emit early if out is available
|
|
|
|
|
buffer.used < size || isAvailable(out)
|
|
|
|
|
case _ =>
|
|
|
|
|
!strategy.isBackpressure || buffer.used < size
|
|
|
|
|
}
|
2016-09-13 16:10:49 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
def grabAndPull(): Unit = {
|
2019-07-04 11:06:24 +02:00
|
|
|
if (buffer.used == size) throw new IllegalStateException("Trying to enqueue but buffer is full")
|
2019-03-11 10:38:24 +01:00
|
|
|
buffer.enqueue((System.nanoTime(), grab(in)))
|
|
|
|
|
if (pullCondition) pull(in)
|
|
|
|
|
}
|
2015-11-21 13:48:10 -05:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def onUpstreamFinish(): Unit =
|
|
|
|
|
completeIfReady()
|
2015-11-25 21:29:35 -05:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
def onPull(): Unit = {
|
|
|
|
|
if (!isTimerActive(timerName) && !buffer.isEmpty) {
|
|
|
|
|
val waitTime = nextElementWaitTime()
|
|
|
|
|
if (waitTime < 0) {
|
|
|
|
|
push(out, buffer.dequeue()._2)
|
|
|
|
|
} else {
|
|
|
|
|
scheduleOnce(timerName, Math.max(DelayPrecisionMS, waitTime).millis)
|
|
|
|
|
}
|
2017-03-30 09:27:01 -05:00
|
|
|
}
|
2016-08-29 14:00:48 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
if (!isClosed(in) && !hasBeenPulled(in) && pullCondition)
|
|
|
|
|
pull(in)
|
2016-09-13 16:10:49 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
completeIfReady()
|
|
|
|
|
}
|
2016-08-29 14:00:48 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
setHandler(in, this)
|
|
|
|
|
setHandler(out, this)
|
2015-11-21 13:48:10 -05:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
def completeIfReady(): Unit = if (isClosed(in) && buffer.isEmpty) completeStage()
|
2015-11-25 21:29:35 -05:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
def nextElementWaitTime(): Long = {
|
|
|
|
|
delayMillis - NANOSECONDS.toMillis(System.nanoTime() - buffer.peek()._1)
|
|
|
|
|
}
|
2015-11-25 21:29:35 -05:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
final override protected def onTimer(key: Any): Unit = {
|
|
|
|
|
if (isAvailable(out))
|
|
|
|
|
push(out, buffer.dequeue()._2)
|
2016-09-13 16:10:49 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
if (!buffer.isEmpty) {
|
|
|
|
|
val waitTime = nextElementWaitTime()
|
|
|
|
|
if (waitTime > DelayPrecisionMS)
|
|
|
|
|
scheduleOnce(timerName, waitTime.millis)
|
|
|
|
|
}
|
|
|
|
|
completeIfReady()
|
2015-11-25 21:29:35 -05:00
|
|
|
}
|
2015-11-21 13:48:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def toString = "Delay"
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-16 21:04:07 +02:00
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
|
|
|
|
@InternalApi private[akka] final class TakeWithin[T](val timeout: FiniteDuration) extends SimpleLinearGraphStage[T] {
|
2015-09-11 15:50:17 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
|
|
|
|
|
new TimerGraphStageLogic(shape) with InHandler with OutHandler {
|
|
|
|
|
def onPush(): Unit = push(out, grab(in))
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
def onPull(): Unit = pull(in)
|
2015-09-11 15:50:17 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
setHandlers(in, out, this)
|
2015-10-31 14:46:10 +01:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
final override protected def onTimer(key: Any): Unit = completeStage()
|
2015-09-11 15:50:17 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def preStart(): Unit = scheduleOnce("TakeWithinTimer", timeout)
|
|
|
|
|
}
|
2015-09-11 15:50:17 +02:00
|
|
|
|
|
|
|
|
override def toString = "TakeWithin"
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-16 21:04:07 +02:00
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
|
|
|
|
@InternalApi private[akka] final class DropWithin[T](val timeout: FiniteDuration) extends SimpleLinearGraphStage[T] {
|
2019-03-11 10:38:24 +01:00
|
|
|
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
|
|
|
|
|
new GraphStageLogic(shape) with InHandler with OutHandler {
|
2015-12-07 12:41:38 +01:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
private val startNanoTime = System.nanoTime()
|
|
|
|
|
private val timeoutInNano = timeout.toNanos
|
2015-12-07 12:41:38 +01:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
def onPush(): Unit = {
|
|
|
|
|
if (System.nanoTime() - startNanoTime <= timeoutInNano) {
|
|
|
|
|
pull(in)
|
|
|
|
|
} else {
|
|
|
|
|
push(out, grab(in))
|
|
|
|
|
// change the in handler to avoid System.nanoTime call after timeout
|
|
|
|
|
setHandler(in, new InHandler {
|
|
|
|
|
def onPush() = push(out, grab(in))
|
|
|
|
|
})
|
|
|
|
|
}
|
2017-02-14 19:24:28 +08:00
|
|
|
}
|
2015-09-11 15:50:17 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
def onPull(): Unit = pull(in)
|
2016-08-29 14:00:48 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
setHandlers(in, out, this)
|
2015-09-11 15:50:17 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
}
|
2015-09-11 15:50:17 +02:00
|
|
|
|
|
|
|
|
override def toString = "DropWithin"
|
2016-01-18 17:21:14 +02:00
|
|
|
}
|
2016-01-15 22:51:26 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2019-02-09 15:25:39 +01:00
|
|
|
@InternalApi private[akka] final class Reduce[T](val f: (T, T) => T) extends SimpleLinearGraphStage[T] {
|
2016-01-15 22:51:26 -05:00
|
|
|
override def initialAttributes: Attributes = DefaultAttributes.reduce
|
|
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
|
|
|
|
|
new GraphStageLogic(shape) with InHandler with OutHandler { self =>
|
|
|
|
|
override def toString = s"Reduce.Logic(aggregator=$aggregator)"
|
2016-04-11 15:36:10 +02:00
|
|
|
|
2019-04-05 13:06:33 +02:00
|
|
|
private var aggregator: T = _
|
2016-01-15 22:51:26 -05:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
private def decider =
|
|
|
|
|
inheritedAttributes.mandatoryAttribute[SupervisionStrategy].decider
|
2016-04-11 15:36:10 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
def setInitialInHandler(): Unit = {
|
|
|
|
|
// Initial input handler
|
|
|
|
|
setHandler(in, new InHandler {
|
|
|
|
|
override def onPush(): Unit = {
|
|
|
|
|
aggregator = grab(in)
|
|
|
|
|
pull(in)
|
|
|
|
|
setHandler(in, self)
|
|
|
|
|
}
|
2016-11-04 10:38:35 +00:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def onUpstreamFinish(): Unit =
|
|
|
|
|
failStage(new NoSuchElementException("reduce over empty stream"))
|
|
|
|
|
})
|
|
|
|
|
}
|
2016-04-11 15:36:10 +02:00
|
|
|
|
2019-04-05 13:06:33 +02:00
|
|
|
@silent // compiler complaining about aggregator = _: T
|
2019-03-11 10:38:24 +01:00
|
|
|
override def onPush(): Unit = {
|
|
|
|
|
val elem = grab(in)
|
|
|
|
|
try {
|
|
|
|
|
aggregator = f(aggregator, elem)
|
|
|
|
|
} catch {
|
|
|
|
|
case NonFatal(ex) =>
|
|
|
|
|
decider(ex) match {
|
|
|
|
|
case Supervision.Stop => failStage(ex)
|
|
|
|
|
case Supervision.Restart =>
|
|
|
|
|
aggregator = _: T
|
|
|
|
|
setInitialInHandler()
|
|
|
|
|
case _ => ()
|
2016-11-04 16:13:11 +00:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
if (!isClosed(in)) pull(in)
|
2016-11-04 10:38:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
2016-01-15 22:51:26 -05:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def onPull(): Unit = pull(in)
|
2016-04-11 15:36:10 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
override def onUpstreamFinish(): Unit = {
|
|
|
|
|
push(out, aggregator)
|
|
|
|
|
completeStage()
|
|
|
|
|
}
|
2016-04-11 15:36:10 +02:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
setInitialInHandler()
|
|
|
|
|
setHandler(out, self)
|
|
|
|
|
}
|
2016-08-29 14:00:48 +02:00
|
|
|
|
2016-01-15 22:51:26 -05:00
|
|
|
override def toString = "Reduce"
|
|
|
|
|
}
|
2016-01-29 22:06:36 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2017-06-13 18:18:46 +09:00
|
|
|
@InternalApi private[stream] object RecoverWith
|
2016-04-20 06:24:12 -07:00
|
|
|
|
2019-03-13 10:56:20 +01:00
|
|
|
@InternalApi private[akka] final class RecoverWith[T, M](
|
|
|
|
|
val maximumRetries: Int,
|
|
|
|
|
val pf: PartialFunction[Throwable, Graph[SourceShape[T], M]])
|
2019-03-11 10:38:24 +01:00
|
|
|
extends SimpleLinearGraphStage[T] {
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2016-01-29 22:06:36 -05:00
|
|
|
override def initialAttributes = DefaultAttributes.recoverWith
|
|
|
|
|
|
|
|
|
|
override def createLogic(attr: Attributes) = new GraphStageLogic(shape) {
|
2016-04-20 06:24:12 -07:00
|
|
|
var attempt = 0
|
|
|
|
|
|
2016-01-29 22:06:36 -05:00
|
|
|
setHandler(in, new InHandler {
|
|
|
|
|
override def onPush(): Unit = push(out, grab(in))
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2016-01-29 22:06:36 -05:00
|
|
|
override def onUpstreamFailure(ex: Throwable) = onFailure(ex)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
setHandler(out, new OutHandler {
|
|
|
|
|
override def onPull(): Unit = pull(in)
|
|
|
|
|
})
|
|
|
|
|
|
2016-04-20 06:24:12 -07:00
|
|
|
def onFailure(ex: Throwable) =
|
2017-06-13 18:18:46 +09:00
|
|
|
if ((maximumRetries < 0 || attempt < maximumRetries) && pf.isDefinedAt(ex)) {
|
2016-04-20 06:24:12 -07:00
|
|
|
switchTo(pf(ex))
|
|
|
|
|
attempt += 1
|
2016-04-25 12:01:03 +02:00
|
|
|
} else
|
2016-04-20 06:24:12 -07:00
|
|
|
failStage(ex)
|
2016-01-29 22:06:36 -05:00
|
|
|
|
|
|
|
|
def switchTo(source: Graph[SourceShape[T], M]): Unit = {
|
|
|
|
|
val sinkIn = new SubSinkInlet[T]("RecoverWithSink")
|
2016-08-23 13:13:11 +02:00
|
|
|
|
2016-01-29 22:06:36 -05:00
|
|
|
sinkIn.setHandler(new InHandler {
|
2016-08-23 13:13:11 +02:00
|
|
|
override def onPush(): Unit = push(out, sinkIn.grab())
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2016-08-23 13:13:11 +02:00
|
|
|
override def onUpstreamFinish(): Unit = completeStage()
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2016-01-29 22:06:36 -05:00
|
|
|
override def onUpstreamFailure(ex: Throwable) = onFailure(ex)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
val outHandler = new OutHandler {
|
2016-08-23 13:13:11 +02:00
|
|
|
override def onPull(): Unit = sinkIn.pull()
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2019-09-05 20:55:48 +02:00
|
|
|
override def onDownstreamFinish(cause: Throwable): Unit = sinkIn.cancel(cause)
|
2016-01-29 22:06:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Source.fromGraph(source).runWith(sinkIn.sink)(interpreter.subFusingMaterializer)
|
|
|
|
|
setHandler(out, outHandler)
|
2016-08-23 13:13:11 +02:00
|
|
|
if (isAvailable(out)) sinkIn.pull()
|
2016-01-29 22:06:36 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def toString: String = "RecoverWith"
|
2016-01-27 00:00:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2019-03-11 10:38:24 +01:00
|
|
|
@InternalApi private[akka] final class StatefulMapConcat[In, Out](val f: () => In => immutable.Iterable[Out])
|
|
|
|
|
extends GraphStage[FlowShape[In, Out]] {
|
2016-01-27 00:00:39 -05:00
|
|
|
val in = Inlet[In]("StatefulMapConcat.in")
|
|
|
|
|
val out = Outlet[Out]("StatefulMapConcat.out")
|
|
|
|
|
override val shape = FlowShape(in, out)
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2016-01-27 00:00:39 -05:00
|
|
|
override def initialAttributes: Attributes = DefaultAttributes.statefulMapConcat
|
|
|
|
|
|
|
|
|
|
def createLogic(inheritedAttributes: Attributes) = new GraphStageLogic(shape) with InHandler with OutHandler {
|
2017-11-22 13:51:24 +01:00
|
|
|
lazy val decider = inheritedAttributes.mandatoryAttribute[SupervisionStrategy].decider
|
2016-01-27 00:00:39 -05:00
|
|
|
var currentIterator: Iterator[Out] = _
|
|
|
|
|
var plainFun = f()
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2016-01-27 00:00:39 -05:00
|
|
|
def hasNext = if (currentIterator != null) currentIterator.hasNext else false
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2016-01-27 00:00:39 -05:00
|
|
|
setHandlers(in, out, this)
|
|
|
|
|
|
|
|
|
|
def pushPull(): Unit =
|
|
|
|
|
if (hasNext) {
|
|
|
|
|
push(out, currentIterator.next())
|
|
|
|
|
if (!hasNext && isClosed(in)) completeStage()
|
|
|
|
|
} else if (!isClosed(in))
|
|
|
|
|
pull(in)
|
|
|
|
|
else completeStage()
|
|
|
|
|
|
|
|
|
|
def onFinish(): Unit = if (!hasNext) completeStage()
|
|
|
|
|
|
|
|
|
|
override def onPush(): Unit =
|
|
|
|
|
try {
|
|
|
|
|
currentIterator = plainFun(grab(in)).iterator
|
|
|
|
|
pushPull()
|
2018-09-19 11:06:24 +02:00
|
|
|
} catch handleException
|
2016-01-27 00:00:39 -05:00
|
|
|
|
|
|
|
|
override def onUpstreamFinish(): Unit = onFinish()
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2018-09-19 11:06:24 +02:00
|
|
|
override def onPull(): Unit =
|
|
|
|
|
try pushPull()
|
|
|
|
|
catch handleException
|
|
|
|
|
|
|
|
|
|
private def handleException: Catcher[Unit] = {
|
2019-03-11 10:38:24 +01:00
|
|
|
case NonFatal(ex) =>
|
|
|
|
|
decider(ex) match {
|
|
|
|
|
case Supervision.Stop => failStage(ex)
|
|
|
|
|
case Supervision.Resume =>
|
|
|
|
|
if (isClosed(in)) completeStage()
|
|
|
|
|
else if (!hasBeenPulled(in)) pull(in)
|
|
|
|
|
case Supervision.Restart =>
|
|
|
|
|
if (isClosed(in)) completeStage()
|
|
|
|
|
else {
|
|
|
|
|
restartState()
|
|
|
|
|
if (!hasBeenPulled(in)) pull(in)
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-19 11:06:24 +02:00
|
|
|
}
|
2016-01-27 00:00:39 -05:00
|
|
|
|
|
|
|
|
private def restartState(): Unit = {
|
|
|
|
|
plainFun = f()
|
|
|
|
|
currentIterator = null
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-17 08:02:54 +01:00
|
|
|
|
2016-01-27 00:00:39 -05:00
|
|
|
override def toString = "StatefulMapConcat"
|
|
|
|
|
|
2016-02-22 20:18:15 +01:00
|
|
|
}
|
2018-02-22 08:11:31 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2019-10-16 17:02:12 +02:00
|
|
|
@InternalApi private[akka] final class LazyFlow[I, O, M](flowFactory: I => Future[Flow[I, O, M]])
|
|
|
|
|
extends GraphStageWithMaterializedValue[FlowShape[I, O], Future[M]] {
|
|
|
|
|
|
|
|
|
|
// FIXME: when removing the deprecated I => Flow factories we can remove that complication from this stage
|
|
|
|
|
|
|
|
|
|
val in = Inlet[I]("LazyFlow.in")
|
|
|
|
|
val out = Outlet[O]("LazyFlow.out")
|
2018-03-19 14:42:37 +01:00
|
|
|
|
2018-02-22 08:11:31 +01:00
|
|
|
override def initialAttributes = DefaultAttributes.lazyFlow
|
2018-03-19 14:42:37 +01:00
|
|
|
|
2018-02-22 08:11:31 +01:00
|
|
|
override val shape: FlowShape[I, O] = FlowShape.of(in, out)
|
|
|
|
|
|
|
|
|
|
override def toString: String = "LazyFlow"
|
|
|
|
|
|
2019-10-16 17:02:12 +02:00
|
|
|
override def createLogicAndMaterializedValue(inheritedAttributes: Attributes): (GraphStageLogic, Future[M]) = {
|
|
|
|
|
val matPromise = Promise[M]()
|
2018-02-22 08:11:31 +01:00
|
|
|
val stageLogic = new GraphStageLogic(shape) with InHandler with OutHandler {
|
2018-03-19 14:42:37 +01:00
|
|
|
var switching = false
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// implementation of handler methods in initial state
|
|
|
|
|
//
|
2019-10-16 17:02:12 +02:00
|
|
|
private def onFlowFutureComplete(firstElement: I)(result: Try[Flow[I, O, M]]) = result match {
|
|
|
|
|
case Success(flow) =>
|
|
|
|
|
// check if the stage is still in need for the lazy flow
|
|
|
|
|
// (there could have been an onUpstreamFailure or onDownstreamFinish in the meantime that has completed the promise)
|
|
|
|
|
if (!matPromise.isCompleted) {
|
|
|
|
|
try {
|
|
|
|
|
val mat = switchTo(flow, firstElement)
|
|
|
|
|
matPromise.success(mat)
|
|
|
|
|
} catch {
|
|
|
|
|
case NonFatal(e) =>
|
|
|
|
|
matPromise.failure(e)
|
|
|
|
|
failStage(e)
|
2018-02-22 08:11:31 +01:00
|
|
|
}
|
2019-10-16 17:02:12 +02:00
|
|
|
}
|
|
|
|
|
case Failure(e) =>
|
|
|
|
|
matPromise.failure(e)
|
|
|
|
|
failStage(e)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def onPush(): Unit =
|
2018-03-19 14:42:37 +01:00
|
|
|
try {
|
2019-10-16 17:02:12 +02:00
|
|
|
val element = grab(in)
|
|
|
|
|
switching = true
|
|
|
|
|
val futureFlow = flowFactory(element)
|
|
|
|
|
|
|
|
|
|
// optimization avoid extra scheduling if already completed
|
|
|
|
|
futureFlow.value match {
|
|
|
|
|
case Some(completed) =>
|
|
|
|
|
onFlowFutureComplete(element)(completed)
|
|
|
|
|
case None =>
|
|
|
|
|
val cb = getAsyncCallback[Try[Flow[I, O, M]]](onFlowFutureComplete(element))
|
|
|
|
|
futureFlow.onComplete(cb.invoke)(ExecutionContexts.sameThreadExecutionContext)
|
|
|
|
|
}
|
2018-02-22 08:11:31 +01:00
|
|
|
} catch {
|
2019-02-09 15:25:39 +01:00
|
|
|
case NonFatal(e) =>
|
2018-03-19 14:42:37 +01:00
|
|
|
matPromise.failure(e)
|
|
|
|
|
failStage(e)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def onUpstreamFinish(): Unit = {
|
2019-10-16 17:02:12 +02:00
|
|
|
if (!matPromise.isCompleted)
|
|
|
|
|
matPromise.tryFailure(new NeverMaterializedException)
|
2018-03-19 14:42:37 +01:00
|
|
|
// ignore onUpstreamFinish while the stage is switching but setKeepGoing
|
|
|
|
|
if (switching) {
|
|
|
|
|
setKeepGoing(true)
|
|
|
|
|
} else {
|
|
|
|
|
super.onUpstreamFinish()
|
2018-02-22 08:11:31 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-19 14:42:37 +01:00
|
|
|
override def onUpstreamFailure(ex: Throwable): Unit = {
|
|
|
|
|
super.onUpstreamFailure(ex)
|
2019-10-16 17:02:12 +02:00
|
|
|
if (!matPromise.isCompleted)
|
|
|
|
|
matPromise.tryFailure(new NeverMaterializedException(ex))
|
2018-03-19 14:42:37 +01:00
|
|
|
}
|
|
|
|
|
|
2018-02-22 08:11:31 +01:00
|
|
|
override def onPull(): Unit = {
|
|
|
|
|
pull(in)
|
2018-03-19 14:42:37 +01:00
|
|
|
}
|
2018-02-22 08:11:31 +01:00
|
|
|
|
2019-10-16 17:02:12 +02:00
|
|
|
override def postStop(): Unit = {
|
|
|
|
|
if (!matPromise.isCompleted)
|
|
|
|
|
matPromise.tryFailure(new AbruptStageTerminationException(this))
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-19 14:42:37 +01:00
|
|
|
setHandler(in, this)
|
|
|
|
|
setHandler(out, this)
|
2018-02-22 08:11:31 +01:00
|
|
|
|
2018-03-19 14:42:37 +01:00
|
|
|
private def switchTo(flow: Flow[I, O, M], firstElement: I): M = {
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// ports are wired in the following way:
|
|
|
|
|
//
|
|
|
|
|
// in ~> subOutlet ~> lazyFlow ~> subInlet ~> out
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
val subInlet = new SubSinkInlet[O]("LazyFlowSubSink")
|
|
|
|
|
val subOutlet = new SubSourceOutlet[I]("LazyFlowSubSource")
|
|
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
val matVal = Source
|
|
|
|
|
.fromGraph(subOutlet.source)
|
2019-10-16 17:02:12 +02:00
|
|
|
.prepend(Source.single(firstElement))
|
2018-03-19 14:42:37 +01:00
|
|
|
.viaMat(flow)(Keep.right)
|
|
|
|
|
.toMat(subInlet.sink)(Keep.left)
|
|
|
|
|
.run()(interpreter.subFusingMaterializer)
|
|
|
|
|
|
|
|
|
|
// The lazily materialized flow may be constructed from a sink and a source. Therefore termination
|
|
|
|
|
// signals (completion, cancellation, and errors) are not guaranteed to pass through the flow. This
|
|
|
|
|
// means that this stage must not be completed as soon as one side of the flow is finished.
|
|
|
|
|
//
|
|
|
|
|
// Invariant: isClosed(out) == subInlet.isClosed after each event because termination signals (i.e.
|
|
|
|
|
// completion, cancellation, and failure) between these two ports are always forwarded.
|
|
|
|
|
//
|
|
|
|
|
// However, isClosed(in) and subOutlet.isClosed may be different. This happens if upstream completes before
|
|
|
|
|
// the cached element was pushed.
|
|
|
|
|
def maybeCompleteStage(): Unit = {
|
|
|
|
|
if (isClosed(in) && subOutlet.isClosed && isClosed(out)) {
|
2018-02-22 08:11:31 +01:00
|
|
|
completeStage()
|
|
|
|
|
}
|
2018-03-19 14:42:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The stage must not be shut down automatically; it is completed when maybeCompleteStage decides
|
|
|
|
|
setKeepGoing(true)
|
2018-02-22 08:11:31 +01:00
|
|
|
|
2019-03-13 10:56:20 +01:00
|
|
|
setHandler(
|
|
|
|
|
in,
|
|
|
|
|
new InHandler {
|
|
|
|
|
override def onPush(): Unit = {
|
|
|
|
|
subOutlet.push(grab(in))
|
|
|
|
|
}
|
|
|
|
|
override def onUpstreamFinish(): Unit = {
|
2019-10-16 17:02:12 +02:00
|
|
|
subOutlet.complete()
|
|
|
|
|
maybeCompleteStage()
|
2019-03-13 10:56:20 +01:00
|
|
|
}
|
|
|
|
|
override def onUpstreamFailure(ex: Throwable): Unit = {
|
|
|
|
|
// propagate exception irrespective if the cached element has been pushed or not
|
|
|
|
|
subOutlet.fail(ex)
|
|
|
|
|
maybeCompleteStage()
|
|
|
|
|
}
|
|
|
|
|
})
|
2018-02-22 08:11:31 +01:00
|
|
|
|
2018-03-19 14:42:37 +01:00
|
|
|
setHandler(out, new OutHandler {
|
|
|
|
|
override def onPull(): Unit = {
|
|
|
|
|
subInlet.pull()
|
|
|
|
|
}
|
2019-09-05 20:55:48 +02:00
|
|
|
override def onDownstreamFinish(cause: Throwable): Unit = {
|
|
|
|
|
subInlet.cancel(cause)
|
2018-03-19 14:42:37 +01:00
|
|
|
maybeCompleteStage()
|
|
|
|
|
}
|
|
|
|
|
})
|
2018-02-22 08:11:31 +01:00
|
|
|
|
2018-03-19 14:42:37 +01:00
|
|
|
subOutlet.setHandler(new OutHandler {
|
|
|
|
|
override def onPull(): Unit = {
|
2019-10-16 17:02:12 +02:00
|
|
|
pull(in)
|
2018-03-19 14:42:37 +01:00
|
|
|
}
|
2019-09-05 20:55:48 +02:00
|
|
|
override def onDownstreamFinish(cause: Throwable): Unit = {
|
2018-03-19 14:42:37 +01:00
|
|
|
if (!isClosed(in)) {
|
2019-09-05 20:55:48 +02:00
|
|
|
cancel(in, cause)
|
2018-02-22 08:11:31 +01:00
|
|
|
}
|
2018-03-19 14:42:37 +01:00
|
|
|
maybeCompleteStage()
|
|
|
|
|
}
|
|
|
|
|
})
|
2018-02-22 08:11:31 +01:00
|
|
|
|
2018-03-19 14:42:37 +01:00
|
|
|
subInlet.setHandler(new InHandler {
|
|
|
|
|
override def onPush(): Unit = {
|
|
|
|
|
push(out, subInlet.grab())
|
|
|
|
|
}
|
|
|
|
|
override def onUpstreamFinish(): Unit = {
|
|
|
|
|
complete(out)
|
|
|
|
|
maybeCompleteStage()
|
|
|
|
|
}
|
|
|
|
|
override def onUpstreamFailure(ex: Throwable): Unit = {
|
|
|
|
|
fail(out, ex)
|
|
|
|
|
maybeCompleteStage()
|
|
|
|
|
}
|
|
|
|
|
})
|
2018-02-22 08:11:31 +01:00
|
|
|
|
2018-03-19 14:42:37 +01:00
|
|
|
if (isClosed(out)) {
|
|
|
|
|
// downstream may have been canceled while the stage was switching
|
|
|
|
|
subInlet.cancel()
|
|
|
|
|
} else {
|
|
|
|
|
subInlet.pull()
|
2018-02-22 08:11:31 +01:00
|
|
|
}
|
|
|
|
|
|
2018-03-19 14:42:37 +01:00
|
|
|
matVal
|
2018-02-22 08:11:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2018-03-19 14:42:37 +01:00
|
|
|
(stageLogic, matPromise.future)
|
2018-02-22 08:11:31 +01:00
|
|
|
}
|
|
|
|
|
}
|